Why is JSON.NET is not working with inheritance while deserializing
Asked Answered
T

2

9

I am deserializing the JSON string to root object by using the following class which works fine .

[Serializable]
    public class MoviesListRootObject
    {
        public int count { get; set; }
        public Pagination pagination { get; set; }
        public List<Response> response { get; set; }
    }

...................................

var json = wc.DownloadString(jsonRequestURL);
var rootObj = JsonConvert.DeserializeObject<MoviesListRootObject>(json);

But if I am generalizng the root object bt creating parent class and then inheriting from it , then I get null after deserialization!!!!

[Serializable]
    public class RootObject
    {
        public int count { get; set; }
        public Pagination pagination { get; set; }
    }

[Serializable]
    public class MoviesListRootObject:RootObject
    {
        public List<MovieResponse> movieResponse { get; set; }

    }

..............................................

 var json = wc.DownloadString(jsonRequestURL);
 var rootObj = JsonConvert.DeserializeObject<MoviesListRootObject>(json);
Trixie answered 12/9, 2012 at 5:18 Comment(2)
it is better if you include the sample JSON string.Overset
Try to use the settings mentioned in this other answer: https://mcmap.net/q/35780/-how-to-deserialize-json-into-ienumerable-lt-basetype-gt-with-newtonsoft-json-net or apply [JsonObject(MemberSerialization.OptIn)] to the base class declaration as suggested in https://mcmap.net/q/1319451/-deserializing-json-to-a-net-base-class-with-json-net If both fail, check the custom approach in this blog post dotnetbyexample.blogspot.com.au/2012/02/…Apprehend
B
4

It is quite simple and out of the box support provided by json.net, you just have to use the following JsonSettings while serializing and Deserializing:

JsonConvert.SerializeObject(graph, Formatting.None, new JsonSerializerSettings() {
    TypeNameHandling = TypeNameHandling.All,
    TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});

and for Deserializing use the below code:

JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bData), type,
    new JsonSerializerSettings() {
    TypeNameHandling = TypeNameHandling.All
});

Just take a note of the JsonSerializerSettings object initializer, that is important for you.

Bria answered 14/1, 2014 at 10:6 Comment(0)
Z
0

Assuming json string looks like the following

{"movieResponse":[{"Rating":"Good"}],"count":1,"pagination":{"PageIndex":1}}

I find it works fine with me. I am currently using Json.net 4.5 r11

If you are serialised object when the class structure looks like

[Serializable]
public class MoviesListRootObject
{
        public int count { get; set; }
        public Pagination pagination { get; set; }
        public List<Response> response { get; set; }
}

And the json string looks something like below

{"count":1,"pagination":{"PageIndex":1},"response":[{"Rating":"Good"}]}

And now you are using the new structure to deserialise then you will get null movieResponse since the property is changed in the new structure.

To solve this problem create a new custom jsonConverter deriving from JsonConverter and create your object programatically. Please have a look at the link json-deserialization-with-jsonnet-class to get some idea. In case if you already know about this and the problem still exists then please update the question with more detail like Json.net version used, json string, complete class structure etc

HTH.

Zagazig answered 5/12, 2012 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.