栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在JsonConverter中递归调用JsonSerializer

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在JsonConverter中递归调用JsonSerializer

这是一个非常普遍的问题。使用“
JsonConvert.SerializeObject”不是一个坏主意。但是,在某些情况下(通常是集合)可以使用的一个技巧是在写入时将其转换为接口,在读取时将其反序列化为简单的派生。

下面是一个简单的转换器,它处理可能被序列化为一组KVP而不是看起来像对象的字典(在这里显示我的年龄:))

注意“ WriteJson”强制转换为IDictionary ,“ ReadJson”使用“
DummyDictionary”。您最终得到了正确的结果,但是使用了传递的序列化器而不会导致递归。

/// <summary>/// Converts a <see cref="KeyValuePair{TKey,TValue}"/> to and from JSON./// </summary>public class DictionaryAsKVPConverter<TKey, TValue> : JsonConverter{    /// <summary>    /// Determines whether this instance can convert the specified object type.    /// </summary>    /// <param name="objectType">Type of the object.</param>    /// <returns>    ///     <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.    /// </returns>    public override bool CanConvert(Type objectType)    {        if (!objectType.IsValueType && objectType.IsGenericType) return (objectType.GetGenericTypeDefinition() == typeof(Dictionary<,>));        return false;    }    /// <summary>    /// Writes the JSON representation of the object.    /// </summary>    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>    /// <param name="value">The value.</param>    /// <param name="serializer">The calling serializer.</param>    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)    {        var dictionary = value as IDictionary<TKey, TValue>;        serializer.Serialize(writer, dictionary);    }    /// <summary>    /// Reads the JSON representation of the object.    /// </summary>    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>    /// <param name="objectType">Type of the object.</param>    /// <param name="existingValue">The existing value of object being read.</param>    /// <param name="serializer">The calling serializer.</param>    /// <returns>The object value.</returns>    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)    {        Dictionary<TKey, TValue> dictionary;        if (reader.TokenType == JsonToken.StartArray)        { dictionary = new Dictionary<TKey, TValue>(); reader.Read(); while (reader.TokenType == JsonToken.StartObject) {     var kvp = serializer.Deserialize<KeyValuePair<TKey, TValue>>(reader);     dictionary[kvp.Key] = kvp.Value;     reader.Read(); }        }        else if (reader.TokenType == JsonToken.StartObject) // Use DummyDictionary to fool JsonSerializer into not using this converter recursively dictionary = serializer.Deserialize<DummyDictionary>(reader);        else dictionary = new Dictionary<TKey, TValue>();        return dictionary;    }    /// <summary>    /// Dummy to fool JsonSerializer into not using this converter recursively    /// </summary>    private class DummyDictionary : Dictionary<TKey, TValue> { }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号