ASP.NET MVC 3. Serialization of the Json method does not apply to the Name Name Name attribute

In my class, I have:

[DataMember(Name = "jsonMemberName", EmitDefaultValue = false, IsRequired = false)] public List<string> Member { get; set; } 

After passing the object through a Json (obj) controller that retrieves System.Web.Mvc.JsonResult: I have serialized json: {Member: ...} but not {jsonMemberName: ...}, so it doesn’t on DataMember (Name = "jsonMemberName").

If I use serialization from System.Runtime.Serialization.Json everithing works fine as expected.

What could be wrong?

+4
source share
2 answers

The JsonResult action that you return from the controller action (using return Json(...) ) relies on the JavaScriptSerializer . This class does not consider any DataMember on your model.

You can write your own ActionResult that uses a serializer in the System.Runtime.Serialization.Json .

For instance:

 public class MyJsonResult : JsonResult { public override void ExecuteResult(ControllerContext context) { var response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } if (Data != null) { var serializer = new DataContractJsonSerializer(Data.GetType()); serializer.WriteObject(response.OutputStream, Data); } } } 

and then in your controller action:

 public ActionResult Foo() { var model = ... return new MyJsonResult { Data = model }; } 
+12
source

System.Web.Mvc.JsonResult use the old JavaScriptSerializer class that knows nothing about the DataAnnotiations assembly. You need to use DataContractJsonSerializer .

You can use this instead of JsonResult if you want:

 public class DataContractJsonResult : JsonResult { public DataContractJsonResult(object data) { Data = data; } public DataContractJsonResult() { } public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); } HttpResponseBase response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { var serializer = new DataContractJsonSerializer(Data.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, Data); string json = Encoding.UTF8.GetString(ms.ToArray()); response.Write(json); } } } 

(I refer to the source code of ASP.NET MVC to create this. Not sure if I need to thank him in any way. Well, more than that already exists, that is. :))

You can also add this to the base class from which your controllers inherit:

 protected JsonResult DataContractJson(object data) { return new DataContractJsonResult(data); } 
+1
source

All Articles