Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a custom JsonConverter globally in Json.Net

Tags:

c#

json.net

Using Json.Net, I have properties in my objects which need special care in order to serialize / deserialize them. Making a descendant of JsonConverter, I managed to accomplish this successfully. This is the common way of doing this:

public class SomeConverter : JsonConverter {     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)     {         ...     }      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)     {         ...     }      public override bool CanConvert(Type objectType)     {         ...     } }  class SomeClass {     [JsonProperty, JsonConverter(typeof(SomeConverter))]     public SomeType SomeProperty; }  //Later on, in code somewhere SomeClass SomeObject = new SomeClass(); string json = JsonConvert.SerializeObject(SomeObject, new SomeConverter()); 

My problem with this code is that I need to introduce my custom converter in every serialization / deserialization. In my project there are many cases that I cannot do that. For instance, I'm using other external projects which make use of Json.Net as well and they will be working on my SomeClass instances. But since I don't want to or can't make change in their code, I have no way to introduce my converter.

Is there any way I can register my converter, using some static member perhaps, in Json.Net so no matter where serialization / deserialization happens, my converter is always present?

like image 881
Mehran Avatar asked Oct 22 '13 06:10

Mehran


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 does Jsonconvert DeserializeObject do?

DeserializeObject Method. Deserializes the JSON to a . NET object.

What is Jsonserializersettings?

Specifies the settings on a JsonSerializer object. Newtonsoft.Json.


2 Answers

Yes, this is possible using Json.Net 5.0.5 or later. See JsonConvert.DefaultSettings.

JsonConvert.DefaultSettings = () => new JsonSerializerSettings {     Converters = new List<JsonConverter> { new SomeConverter() } };  // Later on... string json = JsonConvert.SerializeObject(someObject);  // this will use SomeConverter 

If you're using Web API, you can set up a converter globally like this instead:

var config = GlobalConfiguration.Configuration; var jsonSettings = config.Formatters.JsonFormatter.SerializerSettings; jsonSettings.Converters.Add(new SomeConverter()); 
like image 191
Brian Rogers Avatar answered Sep 20 '22 16:09

Brian Rogers


Another approach (which wins in priority over the one @Brian mentions above) is to implement a custom contract resolver

JsonFormatter.SerializerSettings.ContractResolver = new CustomContractResolver(); 

And the implementation is rather straightforward

public class CustomContractResolver : DefaultContractResolver {     private static readonly JsonConverter _converter = new MyCustomConverter();     private static Type _type = typeof (MyCustomType);      protected override JsonConverter ResolveContractConverter(Type objectType)     {         if (objectType == null || !_type.IsAssignableFrom(objectType)) // alternatively _type == objectType         {             return base.ResolveContractConverter(objectType);         }          return _converter;     } } 

Both methods are valid, this one is just a bigger hammer

like image 28
Yishai Galatzer Avatar answered Sep 21 '22 16:09

Yishai Galatzer