Switching from mvc 3 json serializer to json.net

I have switched some of my jsonresult controller methods to use json.net for serialization.  Why?  Because I needed enums to be serialized by their names instead of their values.

To do this, do the following:

  1. Add json.net to your project.  You can do this through nuget or manually.
  2. Create the following class (from http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx):
        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();
                }
            }
        }
  3. Change your controller method signature from the following:
            public JsonResult GetItems(inputClass input)
    to the following:

            public ActionResult GetItems(inputClass input)
  4. Change your return statement from:
                return Json(model);
    to
                return new JSONMvc.JsonNetResult() { Data = model };
  5. Put an attribute on your property that will cause it to be serialized by name:
                [JsonConverter(typeof(StringEnumConverter))]
                public enuFilterType FilterType

That’s it.  Now, YMMV.  I did this, and everything worked 100% with no other changes on server or client.  However, because it was so easy, I haven’t dug into the code to see if there are any hidden issues I might run into later, like slightly different serialization for dates, for example.  But as of now, my problem with enum serialization is solved.

Comments (3) -

Anoop M Das
Anoop M Das
3/7/2013 8:03:54 AM #

Thanks for your post. I was trying to do the same thing. But for some reason I'm getting the following error on using  

return new JSONMvc.JsonNetResult() { Data = data};

The type or namespace name 'JSONMvc' could not be found (are you missing a using directive or an assembly reference?)

Sorry for the trouble, I have a little experience .NET environment.

Admin
Admin
3/7/2013 8:11:57 AM #

Anoop, JSONMvc is the namespace in which I created the JsonNetResult class.  Change it to whatever the namespace is in which you created the class in your project.

Hope that helps.

Anoop M Das
Anoop M Das
3/7/2013 10:29:00 AM #

I have resolved the issue, I just needed to remove the JSONMvc which was your namespace Smile. Thanks again for your post.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

John West

John West is an independent technology consultant who specializes in using technology to improve business processes.  That means that technology for its own sake isn't worth much; it's value only comes from helping people do things better, cheaper and faster.

That said, he spends way too much time testing cutting-edge gadgets that often come with promises of making things better and faster, but often fail to live up to those promises.  And they usually fail on the cheaper front as well.

Month List

Page List