Json.NET custom serialization with JsonConverter - how to get the quot;defaultquot; behavior(使用 JsonConverter 进行 Json.NET 自定义序列化 - 如何获取“默认行为)
问题描述
我的类 DataType 有一个 JsonConverter.当在 Json 中使用纯字符串作为 DataType 类型的属性的值时,我想做一些特殊的处理.在值是完整"对象的情况下,我想做正常"反序列化.
I have a JsonConverter for my class DataType. I would like to do some special handling when plain string used in Json as the value of a property of type DataType. In the case where the value is a "full" object, I would like to do the "normal" deserialization.
这是我的尝试
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    if (reader.Value != null && reader.ValueType == typeof (string))
    {
        return someSpecialDataTypeInstance;
    }
    else if (reader.TokenType == JsonToken.StartObject)
    {
        DataType dataType = serializer.Deserialize<DataType>(reader);
        return dataType;
    }
    else
    {
        throw new JsonSerializationException();
    }
}
但这不起作用,因为这一行:DataType dataType = serializer.Deserialize(reader);导致无限递归.
But this doesn't work, because this line: DataType dataType = serializer.Deserialize(reader); causes infinite recursion.
这能以某种方式轻松完成吗?(无需手动逐个属性)
Could this be done somehow easily? (without the need to manually go property-by-property)
推荐答案
一种简单的方法是分配一个类的实例,然后使用 JsonSerializer.Populate(JsonReader, Object).这是在标准 CustomCreationConverter:
One easy way to do it is to allocate an instance of your class then use JsonSerializer.Populate(JsonReader, Object).  This is the way it is done in the standard CustomCreationConverter<T>:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    if (reader.Value != null && reader.ValueType == typeof(string))
    {
        return someSpecialDataTypeInstance;
    }
    else if (reader.TokenType == JsonToken.StartObject)
    {
        existingValue = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
        serializer.Populate(reader, existingValue);
        return existingValue;
    }
    else if (reader.TokenType == JsonToken.Null)
    {
        return null;
    }
    else
    {
        throw new JsonSerializationException();
    }
}
限制:
这不处理
TypeNameHandling已启用,并且存在指定多态子类型的"$type"属性.
在这种情况下,您需要执行 JsonDerivedTypeConverer
In this case you'll need to do some of the tricks use by  JsonDerivedTypeConverer<T> in JsonConverter with Interface.   
要反序列化的类型必须有一个无参数构造函数 可访问 Json.NET.如果不是,并且 existingValue 为 null,则需要通过 new DataType(arg1, arg2, ...) 手动构造它.
The type to be deserialized must have a parameterless constructor accessible to Json.NET.  If not, and existingValue is null, it will be necessary to construct it manually, via new DataType(arg1, arg2, ...).
通过 PreserveReferencesHandling 保存参考不支持 a>.
Reference preservation via PreserveReferencesHandling is not supported.  
有关处理这种情况的一种方法,请参阅如何根据json?.
For one approach to handle this situation see How can I choose what type to deserialize at runtime based on the structure of the json?.
示例小提琴.
这篇关于使用 JsonConverter 进行 Json.NET 自定义序列化 - 如何获取“默认"行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JsonConverter 进行 Json.NET 自定义序列化 - 如何获取“默认"行为
				
        
 
            
        - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - 使用 rss + c# 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 
						
						
						
						
						