Recursively call JsonSerializer in a JsonConverter

后端 未结 3 574
一个人的身影
一个人的身影 2020-12-11 00:40

I\'m writing a JsonConverter to perform some conversion tasks I need accomplished on read/write. In particular, I\'m taking the existing serialization behavior

相关标签:
3条回答
  • 2020-12-11 01:15

    This is a very common problem. Using "JsonConvert.SerializeObject" isn't a bad idea. However, one trick that can be used in some circumstances (typically collections) is to cast to the interface when writing and deserialize to a simple derivative when reading.

    Below is a simple converter that deals with dictionaries that might have been serialized as a set of KVPs rather than looking like an object (showing my age here :) )

    Note "WriteJson" casts to IDictionary< K,V> and "ReadJson" uses "DummyDictionary". You end up with the right thing but uses the passed serializer without causing recursion.

    /// <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> { }
    }
    
    0 讨论(0)
  • 2020-12-11 01:32

    Sorry, but maybe I'm confusing. I used this methods for serialize my objects:

    using System;
    using Newtonsoft.Json;
    
    namespace Utilities
    {
        public static class serializer
        {
            public static string SerializeObject(object objectModel) {
                return JsonConvert.SerializeObject(objectModel);
            }
            public static object DeserializeObject<T>(string jsonObject)
            {
                try
                {
                    return JsonConvert.DeserializeObject<T>(jsonObject);
                }
                catch (Exception ex) { return null; }
                
            }
        }
    }

    and I used of this code:

    userLoged = (modelUser)serializer.DeserializeObject<modelUser>((string)Session["userLoged"]);

    I hope this has been helpful.

    0 讨论(0)
  • 2020-12-11 01:39

    You can indeed use the serializer instance passed to your converter, and exclude the current converter. This will not be thread safe, however (see comments of this answer)

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Converters.Remove(this);
        serializer.Serialize(writer, value);
        serializer.Converters.Add(this);
    }
    
    0 讨论(0)
提交回复
热议问题