Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why when I deserialize with JSON.NET ignores my default value?

I'm using JSON.NET as my main serializer.

This is my model, look that I've setted some JSONProperties and a DefaultValue.

public class AssignmentContentItem
{
    [JsonProperty("Id")]
    public string Id { get; set; }
    [JsonProperty("Qty")]
    [DefaultValue(1)]
    public int Quantity { get; set; }
}

When I serialize a List<AssignmentContentItem>, it doing a good work:

private static JsonSerializerSettings s = new JsonSerializerSettings
{
    DefaultValueHandling = DefaultValueHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore
};

OUTPUT:

[{"Id":"Q0"},{"Id":"Q4"},{"Id":"Q7"}]

But when I'd like to deserialize this jsonContent, the property Qty is always 0 and is not set to the default value. I mean, when I deserialize that jsonContent, as DefaultValue for Quantity should be one instead of 0.

public static List<AssignmentContentItem> DeserializeAssignmentContent(string jsonContent)
{
    return JsonConvert.DeserializeObject<List<AssignmentContentItem>>(jsonContent, s);
}

What should I do

like image 502
Darf Zon Avatar asked Mar 16 '13 17:03

Darf Zon


People also ask

How does JSON deserialization work?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it creates a JavaScriptSerializer instance and calls Deserialize() by passing JSON data. It returns a custom object (BlogSites) from JSON data.

Is JSON deserialization slow?

The expected latency when downloading anything from a server to a client should increase as the size of the file increases.

What does it mean to serialize and deserialize JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


2 Answers

The DefaultValue attribute does not set the value of the property. See this question: .NET DefaultValue attribute

What you might be better off doing is setting the value in the constructor:

public class AssignmentContentItem
{
    [JsonProperty("Id")]
    public string Id { get; set; }
    [JsonProperty("Qty")]
    public int Quantity { get; set; }

    public AssignmentContentItem()
    {
        this.Quantity = 1;
    }
}

Where this line:

AssignmentContentItem item =
    JsonConvert.DeserializeObject<AssignmentContentItem>("{\"Id\":\"Q0\"}");

Results in an AssignmentContentItem with its Quantity set to 1.

like image 96
nick_w Avatar answered Oct 01 '22 10:10

nick_w


You can use the the DefaultValueHandling.Populate setting so that Json.Net will populate the created object with the default value.

public static List<AssignmentContentItem> DeserializeAssignmentContent(string jsonContent)
{
    return JsonConvert.DeserializeObject<List<AssignmentContentItem>>(jsonContent, new JsonSerializerSettings
    { 
            DefaultValueHandling = DefaultValueHandling.Populate, 
            NullValueHandling = NullValueHandling.Ignore 
    });
}

http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DefaultValueHandling.htm

like image 35
Slugart Avatar answered Oct 01 '22 10:10

Slugart