Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

c# - Where should I plug in my custom DefaultContractResolver JSON.NET?

I have a WebAPI 2.0 project in ASP.Net in which I implemented a custom DefaultContractResolver so that I can control how my entities are serialized to JSON using JSON.Net; however I am unsure about how can I tell the framework to use my custom implementation. I'd also like to know if its possible to change the ContractResolver for a specific controller / action.

Thanks!

--- Edit 03/07/2014 I've already figured out how to use my custom implementation by creating a new ConfigSettings object and manually parsing the object; However the resulting JSON is serialized twice (it has extra characters). This is caused because the webAPI controller by default serializes the response, So i'd like to change to override that behavior... Is this the right way to go?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just went through the pains of figuring that out myself and I needed one that works per-request. You can use that approach and just return the same media formatter. I found setting a formatter on GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver a bit unreliable for per-request needs, even though I tried handling the per-request needs in one single instance of that class. You can, however try setting your ContractResolver instance there, somewhere in App_Start code

I ended up creating a custom JsonMediaTypeFormatter that checks if there is a configured ContractResolver for the request, you can just return one and the same resolver:

public class DynamicJsonMediaTypeFormatter : JsonMediaTypeFormatter
{ 
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, System.Net.Http.Headers.MediaTypeHeaderValue mediaType)
{
// shown is getting the current formatter, but you can return an instance you prefer
  var formatter = base.GetPerRequestFormatterInstance(type, request, mediaType) as JsonMediaTypeFormatter; 

// Here I had more code to get the resolver based on request, skipped
  ((JsonMediaTypeFormatter)formatter).SerializerSettings.ContractResolver = <YourContractResolverInstance>;
    return formatter;
}
}

I presume you've figured that part out already but your contract resolver can override `CreateProperties' and have your own logic decide what json properties will be present and what names will they use (added for completeness and benefit of other readers):

public class DynamicContractResolver : DefaultContractResolver
{
...
 protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization){
  ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...