Oct 16 2008

ASP.NET MVC and Json.NET

This is an ActionResult I wrote to return JSON from ASP.NET MVC to the browser using Json.NET.

The benefit of using JsonNetResult over the built in JsonResult is you get a better serializer (IMO [:)]) and all the other benefits of Json.NET like nicely formatted JSON text.

public class JsonNetResult : ActionResult { public Encoding ContentEncoding { get ; set ; } public string ContentType { get ; set ; } public object Data { get ; set ; } public JsonSerializerSettings SerializerSettings { get ; set ; } public Formatting Formatting { get ; set ; } public JsonNetResult() { SerializerSettings = new JsonSerializerSettings (); } public override void ExecuteResult( ControllerContext context) { if (context == null ) throw new ArgumentNullException ( "context" ); HttpResponseBase response = context.HttpContext.Response; response.ContentType = ! string .IsNullOrEmpty(ContentType) ? ContentType : "application/json" ; if (ContentEncoding != null ) response.ContentEncoding = ContentEncoding; if (Data != null ) { JsonTextWriter writer = new JsonTextWriter (response.Output) { Formatting = Formatting }; JsonSerializer serializer = JsonSerializer .Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } }

Using JsonNetResult within your application is pretty simple. The example below serializes the NumberFormatInfo settings for the .NET invariant culture.

public ActionResult GetNumberFormatting() { JsonNetResult jsonNetResult = new JsonNetResult (); jsonNetResult.Formatting = Formatting .Indented; jsonNetResult.Data = CultureInfo .InvariantCulture.NumberFormat; return jsonNetResult; }

And here is the nicely formatted result.

{ "CurrencyDecimalDigits": 2, "CurrencyDecimalSeparator": ".", "IsReadOnly": true, "CurrencyGroupSizes": [ 3 ], "NumberGroupSizes": [ 3 ], "PercentGroupSizes": [ 3 ], "CurrencyGroupSeparator": ",", "CurrencySymbol": "¤", "NaNSymbol": "NaN", "CurrencyNegativePattern": 0, "NumberNegativePattern": 1, "PercentPositivePattern": 0, "PercentNegativePattern": 0, "NegativeInfinitySymbol": "-Infinity", "NegativeSign": "-", "NumberDecimalDigits": 2, "NumberDecimalSeparator": ".", "NumberGroupSeparator": ",", "CurrencyPositivePattern": 0, "PositiveInfinitySymbol": "Infinity", "PositiveSign": "+", "PercentDecimalDigits": 2, "PercentDecimalSeparator": ".", "PercentGroupSeparator": ",", "PercentSymbol": "%", "PerMilleSymbol": "‰", "NativeDigits": [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ], "DigitSubstitution": 1 }

Download source