Amila
Amila

Reputation: 2819

Access the default JSON.NET serializer in ASP.NET Web API

I have a ASP.NET web API project and since it does not support having 2 body parameters, I use a JObject parameter and then extract the actual parameters from it. Like this.

Public bool mymethod(JObject data){
   myclassA a = data["a"].toObject<myclassA>();
   myclassA b = data["b"].toObject<myclassB>();
}

But the 2 class types implement ISerializable and I need the JSON.NET to ignore it. I have set up the default JSON.NET serializer to do that and it works fine when serialization is done automatically.

But I need to get a reference to the built in JSON.NET serializer so that I could use it like this in the above code.

myclassA b = data["b"].toObject<myclassB>(defaultSerializer);

Currently I create a new instance of the JSON.NET serializer and use it. But how can I get a reference to the default built in serializer in the asp.net WEB API ?

Also I cannot change anything in class types as this is sort of a legacy app that I'm converting to web api. Thanks.

Upvotes: 3

Views: 4260

Answers (1)

Youssef Moussaoui
Youssef Moussaoui

Reputation: 12395

Try this:

JsonSerializer serializer = JsonSerializer.Create(Configuration.Formatters.JsonFormatter.SerializerSettings);

That should give you the same serializer Web API uses.

Upvotes: 11

Related Questions