<bdo id='udZ7u'></bdo><ul id='udZ7u'></ul>

  1. <legend id='udZ7u'><style id='udZ7u'><dir id='udZ7u'><q id='udZ7u'></q></dir></style></legend>

    <i id='udZ7u'><tr id='udZ7u'><dt id='udZ7u'><q id='udZ7u'><span id='udZ7u'><b id='udZ7u'><form id='udZ7u'><ins id='udZ7u'></ins><ul id='udZ7u'></ul><sub id='udZ7u'></sub></form><legend id='udZ7u'></legend><bdo id='udZ7u'><pre id='udZ7u'><center id='udZ7u'></center></pre></bdo></b><th id='udZ7u'></th></span></q></dt></tr></i><div id='udZ7u'><tfoot id='udZ7u'></tfoot><dl id='udZ7u'><fieldset id='udZ7u'></fieldset></dl></div>
    1. <tfoot id='udZ7u'></tfoot>
    2. <small id='udZ7u'></small><noframes id='udZ7u'>

      使用 JsonConverter 进行 Json.NET 自定义序列化 - 如何

      时间:2023-08-24
        <bdo id='PgCal'></bdo><ul id='PgCal'></ul>
      • <legend id='PgCal'><style id='PgCal'><dir id='PgCal'><q id='PgCal'></q></dir></style></legend>

        <small id='PgCal'></small><noframes id='PgCal'>

                <tbody id='PgCal'></tbody>
              <i id='PgCal'><tr id='PgCal'><dt id='PgCal'><q id='PgCal'><span id='PgCal'><b id='PgCal'><form id='PgCal'><ins id='PgCal'></ins><ul id='PgCal'></ul><sub id='PgCal'></sub></form><legend id='PgCal'></legend><bdo id='PgCal'><pre id='PgCal'><center id='PgCal'></center></pre></bdo></b><th id='PgCal'></th></span></q></dt></tr></i><div id='PgCal'><tfoot id='PgCal'></tfoot><dl id='PgCal'><fieldset id='PgCal'></fieldset></dl></div>

                <tfoot id='PgCal'></tfoot>

                本文介绍了使用 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 使用的一些技巧3744182">带接口的 JsonConverter.

                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 自定义序列化 - 如何获取“默认"行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何合并两个 JObject? 下一篇:Json.net 全局设置

                相关文章

                最新文章

                  • <bdo id='aANoK'></bdo><ul id='aANoK'></ul>
                  <i id='aANoK'><tr id='aANoK'><dt id='aANoK'><q id='aANoK'><span id='aANoK'><b id='aANoK'><form id='aANoK'><ins id='aANoK'></ins><ul id='aANoK'></ul><sub id='aANoK'></sub></form><legend id='aANoK'></legend><bdo id='aANoK'><pre id='aANoK'><center id='aANoK'></center></pre></bdo></b><th id='aANoK'></th></span></q></dt></tr></i><div id='aANoK'><tfoot id='aANoK'></tfoot><dl id='aANoK'><fieldset id='aANoK'></fieldset></dl></div>
                  <legend id='aANoK'><style id='aANoK'><dir id='aANoK'><q id='aANoK'></q></dir></style></legend>

                  <small id='aANoK'></small><noframes id='aANoK'>

                  <tfoot id='aANoK'></tfoot>