I have a class decorated with JsonConverter attribute to use my custom converter. The aim of the custom converter is to serialize CustomProperty
using some custom logic. Instead of writing code to serialize all the primitive properties, I decided to use JObject.FromObject
to automatically serialize the properties and would later do something like o.Remove("CustomProperty")
and then add the custom serialized member to o
.
But since the class is decorated with JsonConverter
attribute, JObject.FromObject
again calls my ClassAJsonConverter
which leads to infinte recursive call. At the point of calling JObject.FromObject
is it possible to specifically tell the json to use it's default converter instead of my custom one.
[Newtonsoft.Json.JsonConverter(typeof(ClassAJsonConverter))]
public class ClassA
{
public string A {get; set;}
public int B {get; set;}
.
//20 some properties
.
public CustomProp CustomProperty {get; set;}
}
public class ClassAJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ClassA);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
.
var o = JObject.FromObject(value); //Here infinite recurrence occur
.
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
.
.
.
}
}
Note: I came across Recursively call JsonSerializer in a JsonConverter but was not able to implement it. Also I would not like to add dependency to AutoMapper just for this one use. Since the question was more than a year old, has anyone found a better way to do it?