2

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought about removing the $ to see if it causes any problems, but nothing changed (and I'm happy it didn't as I really need it there). I'm going to look into it. Thanks.
OK, I rechecked into that matter and the $ was actually the problem. Thanks.
1

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' }
            ]
        }),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.