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": [
"PercentGroupSizes": [
"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
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Pick of the week: Productivity 2.0: How the New Rules of Work Are Changing the Game General Is It Important To Write Good Code? : Fredrik Normén says, “absolutely yes”, and I completely agree! Mocking and Stubbing Easier Then Ever With Moq 2.6 : Daniel
Hi, great stuff.
Which version are you running of Json.NET ?
I have latest and do not show these:
JsonSerializerSettings
JsonSerializer.Create(SerializerSettings)
I am using:
using Newtonsoft.Json;
@Steve
JsonSerializerSettings is something that I have added in development since 3.0.
I included a newer DLL in the source download and the source code for the new classes and methods are in CodePlex.
This is a very nice library, it works like a charm.
Excep when running in Medium Trust. Is there any known issues when running in Medium Trust?
Also, is is possible to serialize the name of an enum name instead of its value?