Follow up to JsonResult changes

Changing the WebAPI shim to use the new and improved JsonResult.
This commit is contained in:
Ryan Nowak 2014-10-31 15:01:51 -07:00
parent 0ccfcc4316
commit b8645eeb31
3 changed files with 21 additions and 32 deletions

View File

@ -3,6 +3,6 @@
namespace System.Web.Http
{
[Obsolete("This class is obsolete. Use Microsoft.AspNet.Mvc.ObjectResult as a replacement.", error: true)]
[Obsolete("This class is obsolete. Use Microsoft.AspNet.Mvc.JsonResult as a replacement.", error: true)]
public class JsonResult<T> { }
}

View File

@ -220,52 +220,45 @@ namespace System.Web.Http
}
/// <summary>
/// Creates an <see cref="ObjectResult"/> (200 OK) with the specified value.
/// Creates an <see cref="JsonResult"/> (200 OK) with the specified value.
/// </summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
/// <param name="content">The content value to serialize in the entity body.</param>
/// <returns>A <see cref="ObjectResult"/> with the specified value.</returns>
/// <returns>A <see cref="JsonResult"/> with the specified value.</returns>
[NonAction]
public virtual ObjectResult Json<T>([NotNull] T content)
public virtual JsonResult Json<T>([NotNull] T content)
{
var result = new ObjectResult(content);
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json"));
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json"));
return result;
return new JsonResult(content);
}
/// <summary>
/// Creates an <see cref="ObjectResult"/> (200 OK) with the specified values.
/// Creates an <see cref="JsonResult"/> (200 OK) with the specified values.
/// </summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
/// <param name="content">The content value to serialize in the entity body.</param>
/// <param name="serializerSettings">The serializer settings.</param>
/// <returns>A <see cref="ObjectResult"/> with the specified values.</returns>
/// <returns>A <see cref="JsonResult"/> with the specified values.</returns>
[NonAction]
public virtual ObjectResult Json<T>([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings)
public virtual JsonResult Json<T>([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings)
{
var formatter = new JsonOutputFormatter()
{
SerializerSettings = serializerSettings,
};
var result = new ObjectResult(content);
result.Formatters.Add(formatter);
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json"));
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json"));
return result;
return new JsonResult(content, formatter);
}
/// <summary>
/// Creates an <see cref="JsonResult{T}"/> (200 OK) with the specified values.
/// Creates an <see cref="JsonResult"/> (200 OK) with the specified values.
/// </summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
/// <param name="content">The content value to serialize in the entity body.</param>
/// <param name="serializerSettings">The serializer settings.</param>
/// <param name="encoding">The content encoding.</param>
/// <returns>A <see cref="JsonResult{T}"/> with the specified values.</returns>
/// <returns>A <see cref="JsonResult"/> with the specified values.</returns>
[NonAction]
public virtual ObjectResult Json<T>(
public virtual JsonResult Json<T>(
[NotNull] T content,
[NotNull] JsonSerializerSettings serializerSettings,
[NotNull] Encoding encoding)
@ -278,11 +271,7 @@ namespace System.Web.Http
formatter.SupportedEncodings.Clear();
formatter.SupportedEncodings.Add(encoding);
var result = new ObjectResult(content);
result.Formatters.Add(formatter);
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("application/json"));
result.ContentTypes.Add(MvcMediaTypeHeaderValue.Parse("text/json"));
return result;
return new JsonResult(content, formatter);
}
/// <summary>

View File

@ -243,8 +243,8 @@ namespace System.Web.Http
var result = controller.Json(product);
// Assert
var objectResult = Assert.IsType<ObjectResult>(result);
Assert.Same(product, objectResult.Value);
var jsonResult = Assert.IsType<JsonResult>(result);
Assert.Same(product, jsonResult.Value);
}
[Fact]
@ -259,10 +259,10 @@ namespace System.Web.Http
var result = controller.Json(product, settings);
// Assert
var objectResult = Assert.IsType<ObjectResult>(result);
Assert.Same(product, objectResult.Value);
var jsonResult = Assert.IsType<JsonResult>(result);
Assert.Same(product, jsonResult.Value);
var formatter = Assert.IsType<JsonOutputFormatter>(Assert.Single(objectResult.Formatters));
var formatter = Assert.IsType<JsonOutputFormatter>(jsonResult.Formatter);
Assert.Same(settings, formatter.SerializerSettings);
}
@ -278,10 +278,10 @@ namespace System.Web.Http
var result = controller.Json(product, settings, Encoding.UTF8);
// Assert
var objectResult = Assert.IsType<ObjectResult>(result);
Assert.Same(product, objectResult.Value);
var jsonResult = Assert.IsType<JsonResult>(result);
Assert.Same(product, jsonResult.Value);
var formatter = Assert.IsType<JsonOutputFormatter>(Assert.Single(objectResult.Formatters));
var formatter = Assert.IsType<JsonOutputFormatter>(jsonResult.Formatter);
Assert.Same(settings, formatter.SerializerSettings);
Assert.Same(Encoding.UTF8, Assert.Single(formatter.SupportedEncodings));
}