Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to add JsonSerializerSettings to a custom JsonConverter

Tags:

c#

json.net

In using json.net I'm required to adjust the (de)serializer settings, so I create a JsonSerializerSettings instance and use it like this:

JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss); 

But I then find it necessary to use a custom JsonConverter for some banged up data, and then use it like this:

JsonConvert.DeserializeObject<SomeType>(someString, new myConverter());

But I'm now finding I need to use both JsonSerializerSettings and a custom converter but I don't see any overloads of the DeserializeObject method that would allow that.

What might I be missing?

I'm using .net v4.5.2 and json.net v10.0.

like image 277
axa Avatar asked Sep 02 '25 15:09

axa


1 Answers

JsonSerializerSettings has a Converters collection. So add your converter to that collection and then pass the settings to DeserializeObject.

mySerializerSettings.ss.Converters.Add(new myConverter());
var obj = JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss);
like image 95
Brian Rogers Avatar answered Sep 05 '25 04:09

Brian Rogers