I made a simple custom JsonConvert class. Right now I'm trying to convert the JSON data to a custom class like so:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//JObject jo = JObject.Load(reader);
MyItem instance = serializer.Deserialize<MyItem>(reader);
// want to do more thing here...
}
Every time it executes the Deserialize<>() method it re-enters the ReadJson method and thus keeping me in a never ending loop.
How can I convert the JSON data to my custom object without ending up in a never ending loop?
Updated
The JSON I'm receiving is generic. I need the custom converter to map it to the right type. The JSON looks like this:
{
/* Base obj properties, should be mapped to Page.cs */
"$type": "App.Models.Page, App",
"title": "Page title",
"technicalName": "some_key",
"fields": [
/* Should be mapped to InputField.cs */
{
"$type": "App.Models.InputField, App",
"type": "input",
"typeTitle": "Input",
"title": "Input",
"technicalName": "input",
"inputType": "text",
"defaultValue": "Input default value"
},
/* Should be mapped to TextareaField.cs */
{
"$type": "App.Models.TextareaField, App",
"type": "textarea",
"typeTitle": "Textarea",
"title": "Textarea",
"technicalName": "textarea",
"defaultValue": "default textarea value"
}
]
}
In short my class files look like this:
class Page
{
public string Title {get;set;}
public string TechnicalName {get;set;}
public List<Field> Fields {get;set;} // Note the base class "Field"
}
class InputField : Field // Field has base properties
{
// InputField has its own properties
}
class TextareaField : Field // Field has base properties
{
// TextareaField has its own properties
}
TypeNameHandling
I've set the json formatters settings:
jsonOutputFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
jsonInputFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
When I output a Page object through Web API then I get a JSON structure exactly like the one above. When I sent back that exact JSON data I expect it to nicely map back to a Page object with the correct types in the Field list property.
But all the Fields are mapped to its base class Field instead of the types that are defined in $type.
So currently the JSON formatter works great for outputting. It shows all the $types. But the input mapping isn't really mapping back to its specific types.
JsonConvertin the first place? Why not use the one provided by the library?public new object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)