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 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> { } public class JsonResult<T> { }
} }

View File

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

View File

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