Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

Try this:

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

That should give you the same serializer Web API uses.

share|improve this answer
Thanks, actually I'm using a slight variation of this. But this still calls JsonSerializer.Create() which creates a new instance of the serializer. I'm thinking if I could get reference to the serializer that has been already instantiated by the web api itself. – Amila Mar 14 at 7:13
Web API creates a new serializer for each request. So that doesn't really mean anything. Json Serializers are very lightweight objects. – Youssef Moussaoui Mar 31 at 0:37
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.