Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Action returning JsonResult without null

I have a action which returns an JsonResult for an object of a particular class. I have decorated the properties of this class with some attrib to avoid null fields. Class definition is:

    private class GanttEvent
    {
        public String name { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public String desc { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<GanttValue> values { get; set; }
    }

And in my Action i use an object

    var res = new List<GanttEvent>();

which I return using:

    return Json(res, JsonRequestBehavior.AllowGet);

Unfortunatelly, I'm still receiving null values at output:

    [{"name":"1.1 PREVIOS AL INICIO ","desc":null,"values":null},{"name":"F04-PGA-S10","desc":"Acta preconstrucción","values":null},{"name":"F37-PGA-S10","desc":"Plan de inversión del anticipo","values":null},{"name":"F09-PGA-S10","desc":"Acta de vecindad","values":null},{"name":"F05-PGA-S10","desc":"Acta de inicio","values":null},{"name":"F01-PGA-S10","desc":"Desembolso de anticipo","values":null}]

Am I missing something or doing something wrong?

like image 713
Farlop Avatar asked Nov 08 '12 17:11

Farlop


2 Answers

As stated by Brad Christie, MVC4 stills uses JavaScriptSerializer, so in order to get your object serialized by Json.Net, you will have to perform several steps.

First, inherit a new class JsonNetResult from JsonResult as follows (based on this solution):

public class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        this.ContentType = "application/json";
    }

    public JsonNetResult(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior jsonRequestBehavior)
    {
        this.ContentEncoding = contentEncoding;
        this.ContentType = !string.IsNullOrWhiteSpace(contentType) ? contentType : "application/json";
        this.Data = data;
        this.JsonRequestBehavior = jsonRequestBehavior;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var response = context.HttpContext.Response;

        response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

        if (ContentEncoding != null)
            response.ContentEncoding = ContentEncoding;

        if (Data == null)
            return;

        // If you need special handling, you can call another form of SerializeObject below
        var serializedObject = JsonConvert.SerializeObject(Data, Formatting.None);
        response.Write(serializedObject);
    }
}

Then, in your controller, override the Json method to use the new class:

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonNetResult(data, contentType, contentEncoding, behavior);
}
like image 192
Farlop Avatar answered Oct 04 '22 20:10

Farlop


Controller.Json uses the JavaScriptSerializer not the Newtonsoft Json library (which is where the JsonPropertyAttribute originates from).

You'll either need to use the Newtonsoft library library and return the serialized result that way, or you continue calling Json and write a converter that will ignore nulls.

like image 25
Brad Christie Avatar answered Oct 04 '22 22:10

Brad Christie