Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 controller .Json method serialization doesn't look at DataMember Name attribure

In my class i've got:

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

After passing the object through controller's Json(obj) that retruns System.Web.Mvc.JsonResult: i've got serialized json: {Member:...} but not {jsonMemberName:...}, so it doesn't look at DataMember(Name = "jsonMemberName").

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

What can be wrong?

like image 347
WHITECOLOR Avatar asked Dec 09 '22 08:12

WHITECOLOR


2 Answers

The JsonResult action which you are returning from the controller action (using return Json(...)) internally relies on the JavaScriptSerializer class. This class doesn't take into account any DataMember attributes on your model.

You could write a custom ActionResult which uses the serializer in the System.Runtime.Serialization.Json namespace.

For example:

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 };
}
like image 65
Darin Dimitrov Avatar answered Mar 15 '23 22:03

Darin Dimitrov


System.Web.Mvc.JsonResult usea the old JavaScriptSerializer class, which doesn't know anything about the DataAnnotiations assembly. You need to use the DataContractJsonSerializer instead.

You can use this instead on JsonResult if you so desire:

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 referenced the ASP.NET MVC source code to create this. Not sure if I have to credit it in some way. Well, more than this aside already is, that is. :))

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

protected JsonResult DataContractJson(object data)
{
    return new DataContractJsonResult(data);
}
like image 37
adamjford Avatar answered Mar 15 '23 22:03

adamjford