Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run default serialization logic from JsonConverter

I have a JsonConverter that, depending on an instance specific flag, needs to either

  • run custom serialization logic
  • run the default Json.NET serialization logic

How can the default Json.NET serialization logic be ran from a JsonConverter?

Thanks

like image 233
sient Avatar asked Jan 26 '14 16:01

sient


People also ask

What is JsonConverter attribute?

JsonConverterAttribute. The JsonConverterAttribute specifies which JsonConverter is used to convert an object. The attribute can be placed on a class or a member. When placed on a class, the JsonConverter specified by the attribute will be the default way of serializing that class.

What is JsonConverter?

Converts an object to and from JSON. Newtonsoft.Json.

What is Jsonconvert SerializeObject C#?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.


1 Answers

Here is an example. Say your class to serialize looks like this:

class Foo {     public bool IsSpecial { get; set; }     public string A { get; set; }     public string B { get; set; }     public string C { get; set; } } 

The IsSpecial flag is used to control whether we do something special in the converter or just let things serialize naturally. You can write your converter like this:

class FooConverter : JsonConverter {     public override bool CanConvert(Type objectType)     {         return typeof(Foo).IsAssignableFrom(objectType);     }      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)     {         Foo foo = (Foo)value;         JObject jo;         if (foo.IsSpecial)         {             // special serialization logic based on instance-specific flag             jo = new JObject();             jo.Add("names", string.Join(", ", new string[] { foo.A, foo.B, foo.C }));         }         else         {             // normal serialization             jo = JObject.FromObject(foo);         }         jo.WriteTo(writer);     }      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)     {         throw new NotImplementedException();     } } 

Then, to use the converter, pass an instance of it to the SerializeObject method (e.g. in the settings). (Do NOT decorate the target class with a JsonConverter attribute, or this will result in an infinite recursive loop when you serialize.)

class Program {     static void Main(string[] args)     {         List<Foo> foos = new List<Foo>         {             new Foo             {                 A = "Moe",                 B = "Larry",                 C = "Curly",                 IsSpecial = false             },             new Foo             {                 A = "Huey",                 B = "Dewey",                 C = "Louie",                 IsSpecial = true             },         };          JsonSerializerSettings settings = new JsonSerializerSettings();         settings.Converters.Add(new FooConverter());         settings.Formatting = Formatting.Indented;          string json = JsonConvert.SerializeObject(foos, settings);         Console.WriteLine(json);     } } 

Output:

[   {     "IsSpecial": false,     "A": "Moe",     "B": "Larry",     "C": "Curly"   },   {     "names": "Huey, Dewey, Louie"   } ] 
like image 157
Brian Rogers Avatar answered Sep 30 '22 14:09

Brian Rogers