在没有属性的类上全局使用 JsonConverter

Globally use a JsonConverter on a class without the attribute(在没有属性的类上全局使用 JsonConverter)
本文介绍了在没有属性的类上全局使用 JsonConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 MVC .net 项目,我正在使用 mongodb.我使用自定义 JsonConverter 将 ObjectId 属性序列化为字符串,正如我在此答案中所述:在 MVC 中使用 Json.NET 自动将 mongodb ObjectId 作为字符串返回

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

在那里我使用了一个属性,以便在某个属性上使用自定义转换器:[JsonConverter(typeof(ObjectIdConverter))

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

有没有办法告诉序列化程序对 ObjectId 类型的所有属性使用 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.

谢谢!

推荐答案

是的,您可以使用自定义 IContractResolver 以编程方式将 JsonConverter 应用于类或属性.最简单的方法是从 DefaultContractResolver 类派生解析器,然后覆盖适当的方法.下面是一个示例解析器,它指示 Json.Net 在 ObjectId 类型的所有实例上使用 ObjectIdConverter,无论它们可能出现在哪个类中.

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;
    }
}

要使用解析器,您可以构造一个 JsonSerializer 实例并在其上设置 ContractResolver 属性,然后使用该实例进行序列化/反序列化.如果您使用 JObject.ToObject()JObject.FromObject(),请注意这两个方法都有接受 JsonSerializer 实例的重载.

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);

或者,如果您使用 JsonConvert 类进行序列化/反序列化,则可以创建 JsonSerializerSettings 的实例,设置 ContractResolver 属性,然后将设置传递给 SerializeObject()DeserializeObject() 方法.

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);

希望这会有所帮助.

这篇关于在没有属性的类上全局使用 JsonConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)