Marius Balaban
Marius Balaban

Reputation: 197

Nested json deserialized incompletely

I mentioned in title that the json is deserialized incompletely because some of the objects have the right value in the controller (I send the json to an asp.net mvc 4 controller), but the problem occurs with properties of objects stored in arrays (to be exact, the problem appears with Data)

I have the following classes:

public class Node
{
    [JsonProperty("id")]
    public int? Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("type")]
    public string Type { get; set; }


    private Data _data = new Data();

    [JsonProperty("data")]
    public Data Data { get; set; }

    private List<Adjacency> _adjacencies = new List<Adjacency>();
    [JsonProperty("adjacencies")]
    public List<Adjacency> Adjacencies
    {
        get { return _adjacencies; }
        set { _adjacencies = value; }
    }


    private List<Instance> _dependendStates = new List<Instance>();

    public List<Instance> DependentStates
    {

        get { return _dependendStates; }
        set { _dependendStates = value; }
    }
}

public class Data
{
    [JsonProperty("$posX")]
    public decimal PosX { get; set; }
    [JsonProperty("$posY")]
    public decimal PosY { get; set; }
}

public class Adjacency
{
    [JsonProperty(PropertyName = "nodeTo")]
    public string NodeTo { get; set; }
    [JsonProperty(PropertyName = "data")]
    public AdjData Data { get; set; }
    //doesn't contain definition for automated transitions
}

public class AdjData
{
    [JsonProperty(PropertyName = "$labelid")]
    public string LabelId { get; set; }
    [JsonProperty(PropertyName = "$labeltext")]
    public string Label { get; set; }
    [JsonProperty(PropertyName = "$type")]
    public string Type { get; set; }    
}

I've highlighted fields that don't have the right value.

The null problem http://img19.imageshack.us/img19/530/36976913.png

The json looks like this:

{
   "result":[
      {
         "id":"100",
         "name":"Start",
         "data":{
            "$posX":-100,
            "$posY":-100,
            "$deployed":true,
            "$color":"#000000",
            "$selected":"true"
         },
         "adjacencies":[
            {
               "nodeTo":"188",
               "data":{
                  "$type":"labeled_arrow",
                  "$labelid":"Label Name",
                  "$labeltext":"Label Name"
               }
            }
         ]
      },
      {
         "id":"188",
         "name":"Second  ",
         "data":{
            "$dim":20,
            "$color":"#000000",
            "$selected":"true"
         },
         "adjacencies":[

         ]
      }
   ],
   "smName":"gftrds"
}

I'm having a problem sorting this out as I do not understand or see where the problem is.

Upvotes: 2

Views: 1000

Answers (2)

Benjamin Galileo Hron
Benjamin Galileo Hron

Reputation: 61

I experienced the same issue, however in my case this was related to how the data was being transmitted from client side. Make sure the AJAX request is using the proper headers and formatting. For example:

        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify({
            MemberId : '123',
            UserName: '456',
            Parameters: [
                { Value : 'testing' },
                { Value : 'test2' }
            ]
        }),

Upvotes: 1

nick_w
nick_w

Reputation: 14938

Disclaimer: I wasn't able to reproduce this exactly, as in my case Label and LabelId deserialized fine, though Type did not, so my anwser may not address the problem you are having.

In my case, I was only able to deserialize the "$type":"labeled_arrow" property if it was not the first one - if it appeared after "$labelid":"Label Name" or "$labeltext":"Label Name" it worked fine.

Strangely, if the $type property was called type in the Json and referred to as [JsonProperty(PropertyName = "type")] in the C#, it would deserialize fine, regardless of the order.

In your case you can't deserialize Label or LabelId either, so it could be that your problem is caused by something else. To rule out what I have suggested, it might pay to try modifying some of the property names (possibly just take off the $) to see if they are causing the properties to be deserialized incorrectly.

Upvotes: 1

Related Questions