0

I have a problem with deresializing downloaded JSON string from API. I would like to deserialize it to the abstract class, so the type would be decided by runtime.

My code example:

JsonSerializerSettings settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
JsonClasses myObj = (JsonClasses) JsonConvert.DeserializeObject(this.Response, settings);

For explanation, in this.Response I have JSON string. I used this way (TypeNameHandling) of dealing with deserialization based on another topic in StackOverflow, but it didn't work for me - maybe because the fact, that they at first did serialization of the object to string and after that the deserialization. I have some derived classes from abstract class JsonClasses. They have diferent "JSON structure". Is there any possibility to resolve this some not so much difficultr way?

Thanks for help!

  • Why not `JsonConvert.DeserializeObject(this.Response, settings)`? – ASpirin Sep 19 '17 at 09:49
  • 1
    How do you want to create an object of an abstract class? – SᴇM Sep 19 '17 at 10:05
  • Are you looking for [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/3744182)? – dbc Sep 19 '17 at 10:34
  • I'm sorry I wrote it not so correct. I need to create an instance of some inherited class but the return object is type of JsonClasses (parent of these inherited classes). But even when I use var test = JsonConvert.DeserializeObject(this.Response, settings); it didn't work – Andrew Lerion Sep 19 '17 at 11:42
  • Is `JsonClasses` abstract one or the derived one? You need to specify a derived non-abstract type in the generic parameter, `JsonConvert.DeserializeObject(json)`. – Lasse V. Karlsen Sep 19 '17 at 12:04

1 Answers1

2

You can't create an object of abstract class:

MSDN: Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

Just inherit some class from your abstract class (with no body), and deserialize to it.

For example:

public class YourClassName : JsonClasses
{
}

UPDATE

For example I've created an example generic method, that could help:

public T DeserializeFromJsonClasses<T>() where T : JsonClasses
{
    T myObj = default(T);
    JsonSerializerSettings settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All};
    myObj = JsonConvert.DeserializeObject<T>(this.Response, settings);
    return myObj;
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41