Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Last active November 30, 2017 12:42
Show Gist options
  • Save Pzixel/e9847180c86828c55153cb310a398f0c to your computer and use it in GitHub Desktop.
Save Pzixel/e9847180c86828c55153cb310a398f0c to your computer and use it in GitHub Desktop.
JsonResult that uses Newtonsoft.JSON.Net for WebAPI
using System;
using System.IO;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace XXX
{
public class JsonNetResult<T> : ActionResult
{
private JsonSerializerSettings _jsonSerializerSettings;
public T Content { get; }
public Encoding Encoding { get; }
public JsonNetResult(T content, JsonSerializerSettings jsonSerializerSettings, Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
Content = content;
_jsonSerializerSettings = jsonSerializerSettings;
Encoding = encoding;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
var response = context.HttpContext.Response;
response.ContentType = "application/json";
response.ContentEncoding = Encoding;
if (Content == null)
return;
var responseBytes = Serialize();
response.OutputStream.Write(responseBytes.Array, responseBytes.Offset, responseBytes.Count);
}
private ArraySegment<byte> Serialize()
{
var jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings);
using (var memoryStream = new MemoryStream())
{
using (TextWriter textWriter = new StreamWriter(memoryStream, Encoding, 1024, true))
{
var jsonTextWriter = new JsonTextWriter(textWriter)
{
CloseOutput = false
};
using (JsonWriter jsonWriter = jsonTextWriter)
{
jsonSerializer.Serialize(jsonWriter, Content);
jsonWriter.Flush();
}
}
return new ArraySegment<byte>(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
}
}
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace XXX
{
public static class MvcControllerExtensions
{
/// <summary>Creates a <see cref="T:XXX.JsonNetResult`1" /> (200 OK) with the specified value.</summary>
/// <returns>A <see cref="T:XXX.JsonNetResult`1" /> with the specified value.</returns>
/// <param name="controller">Controller that executes an action</param>
/// <param name="content">The content value to serialize in the entity body.</param>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public static JsonNetResult<T> JsonNet<T>(this Controller controller, T content) => controller.JsonNet(content, new JsonSerializerSettings());
/// <summary>Creates a <see cref="T:XXX.JsonNetResult`1" /> (200 OK) with the specified values.</summary>
/// <returns>A <see cref="T:XXX.JsonNetResult`1" /> with the specified values.</returns>
/// <param name="controller">Controller that executes an action</param>
/// <param name="content">The content value to serialize in the entity body.</param>
/// <param name="serializerSettings">The serializer settings.</param>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public static JsonNetResult<T> JsonNet<T>(this Controller controller, T content, JsonSerializerSettings serializerSettings) => controller.JsonNet<T>(content, serializerSettings, new UTF8Encoding(false, true));
/// <summary>Creates a <see cref="T:XXX.JsonNetResult`1" /> (200 OK) with the specified values.</summary>
/// <returns>A <see cref="T:XXX.JsonNetResult`1" /> with the specified values.</returns>
/// <param name="controller">Controller that executes an action</param>
/// <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>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public static JsonNetResult<T> JsonNet<T>(this Controller controller, T content, JsonSerializerSettings serializerSettings, Encoding encoding) => new JsonNetResult<T>(content, serializerSettings, encoding);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment