Json.net deserialization is returning an empty object

13,396

Solution 1

When you serialized your SplunkDataModel to JSON, you wrapped it in an object with a summary property. Hence, when you deserialize the JSON back to objects, you need to use the same structure. There are several ways to go about it; they all achieve the same result.

  1. Declare a class to represent the root level of the JSON and deserialize into that:

    public class RootObject
    {
        public SplunkDataModel Summary { get; set; }
    }
    

    Then:

    var deserialized = JsonConvert.DeserializeObject<RootObject>(contents).Summary;
    
  2. Or, deserialize by example to an instance of an anonymous type, then retrieve your object from the result:

    var anonExample = new { summary = new SplunkDataModel() };
    var deserialized = JsonConvert.DeserializeAnonymousType(contents, anonExample).summary;
    
  3. Or, deserialize to a JObject, then materialize your object from that:

    JObject obj = JObject.Parse(contents);
    var deserialized = obj["summary"].ToObject<SplunkDataModel>();
    

Solution 2

On my side, it was because I had no public setter for my properties. Instead of having

public class MyClass
{        
    public int FileId { get; }
}

I should have

public class MyClass
{        
    public int FileId { get; set; }
}

silly mistake that cost me hours....

Share:
13,396

Related videos on Youtube

Manish
Author by

Manish

Developing Android App by night and developing C# application during day,

Updated on September 16, 2022

Comments

  • Manish
    Manish over 1 year

    I'm using the code below for serialization.

    var json = JsonConvert.SerializeObject(new { summary = summary });
    

    summary is a custom object of type SplunkDataModel:

    public class SplunkDataModel
    {
        public SplunkDataModel() {}
    
        public string Category { get; set; }
        public int FailureCount { get; set; }
        public Dictionary<string, SplunkError> FailureEntity { get; set; }
        public Dictionary<string, string> JobInfo { get; set; }
        public string JobStatus { get; set; }
        public int SuccessCount { get; set; }
        public List<string> SuccessEntity { get; set; }
        public int TotalCount { get; set; }
    }
    

    Serialization results in the JSON below:

    {
      "summary": {
        "Category": "category",
        "JobStatus": "Failure",
        "JobInfo": {
          "Course processing failed": "" 
        },
        "TotalCount": 0,
        "SuccessCount": 0,
        "FailureCount": 0,
        "FailureEntity": {},
        "SuccessEntity": []
      }
    }
    

    Now, for unit testing purposes, I need to deserialize it, but the code below is returning an object with empty values. Where am I going wrong?

    var deserialized = JsonConvert.DeserializeObject<SplunkDataModel>(contents);
    
  • Ashkan S
    Ashkan S almost 5 years
    AKHHHH! OMG! Thanks!
  • Hamit YILDIRIM
    Hamit YILDIRIM almost 5 years
    right in my case there is internal public string Currency { get; internal set; }
  • Angel Yordanov
    Angel Yordanov about 4 years
    @HamitYILDIRIM Aaargh... i pulled half my hair until i read your comment, thanks alot!
  • Hamit YILDIRIM
    Hamit YILDIRIM about 3 years
    I dont know your code but there should be a problem interesting your code scope. Asynchronous function have to know that function call completed and ready to deserialize. For example you can use it in an arrow function @PhilNicholas
  • Phil Nicholas
    Phil Nicholas about 3 years
    I faced the same problem as @HamitYILDIRIM DeserializeAsync would not map JSON properties with { get; internal set; } I decorated the properties with the [JsonInclude] attribute and everything worked as expected.
  • StingyJack
    StingyJack about 3 years
    If anyone is wondering, setting InternalsVisibleTo for the Newtonsoft.Json library doesnt fix this.