Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Globally use a JsonConverter on a class without the attribute

I have a MVC .net project, and I am using mongodb. I use a custom JsonConverter to serialize ObjectId properties as a string, as I describes in this answer: Automatically retun mongodb ObjectId as string with Json.NET in MVC

In there I used an attribue so that the custom converter is used on a certain property: [JsonConverter(typeof(ObjectIdConverter))

Is there a way to tell the serializer to use the ObjectIdConverter on all properties of type ObjectId? I don't wan't to go over on the entire project and add this property.

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Yes, you can use a custom IContractResolver to programmatically apply a JsonConverter to a class or property. The simplest way to do this is to derive your resolver from the DefaultContractResolver class and then override the appropriate method. Below is an example resolver that instructs Json.Net to use an ObjectIdConverter on all instances of the ObjectId type, regardless of what class they might appear in.

class CustomResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        JsonObjectContract contract = base.CreateObjectContract(objectType);
        if (objectType == typeof(ObjectId))
        {
            contract.Converter = new ObjectIdConverter();
        }
        return contract;
    }
}

To use the resolver, you can construct a JsonSerializer instance and set the ContractResolver property on it, then use that instance to do your serialization/deserialization. If you are using JObject.ToObject() and JObject.FromObject(), note that both methods have overloads that accept a JsonSerializer instance.

JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new CustomResolver();

JObject jo = JObject.FromObject(foo, serializer);

Alternatively, if you are using the JsonConvert class to do your serialization/deserialization, you can create an instance of JsonSerializerSettings, set the ContractResolver property on that, then pass the settings to the SerializeObject() and DeserializeObject() methods.

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CustomResolver();

Foo foo = JsonConvert.DeserializeObject<Foo>(json, settings);

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...