27

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 and tacking on some additional properties on write / reading those additional properties on read.

Inside the JsonConverter, I'd like to make use of the passed JsonSerializer instance to perform the majority of the conversion functionality. However, when I do this, I end up in a recursive loop where the serializer calls into my converter which calls into the serializer which calls into the converter and etc.

I've seen people do things such as use JsonConvert.SerializeObject, passing in all the converters from the serializer instance except this. However, that won't work for me because it bypasses all of the other customization I've done on my serializer, such as custom contract resolver and DateTime handling.

Is there a way I can either:

  1. Use the serializer instance passed to me, but somehow exclude my converter, or
  2. Clone the serializer passed to me (without manually constructing a new one and copying it property by property) and remove my converter?
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202

4 Answers4

8

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> { }
}
AndyPook
  • 2,762
  • 20
  • 23
  • Follow-up: https://stackoverflow.com/q/60254867/156755 if you'd care to take a look – Basic Feb 17 '20 at 01:32
  • Is there any particular reason why you don't explicitly check for `DummyDictionary` in `CanConvert`? – Zev Spitz Nov 24 '20 at 09:37
  • note that DummyDictionary is `private`. It can never be passed in. The instance of Dummy is cast/returned as a Dictionary which is what CanConvert checks for. So, I think it's ok? What situation are you looking at? – AndyPook Nov 24 '20 at 09:41
  • Not a specific situation; it just seems a little clearer to me to write `if (objectType == typeof(DummyDictionary)) {return false;}`, and then perform the rest of the checks. This would allow adding a little more flexibility: e.g. any types implementing `IDIctionary`, or including other types that inherit from `Dictionary`. – Zev Spitz Nov 24 '20 at 10:01
  • I don't think you'd want that. You'd still want to be able to serialize the result of a previous deserialize. Your change would prevent that. Changing to IDictionary would be sensible. Though custom impls are pretty rare. The code was just cut&paste from a project at the time. Worked for us :) – AndyPook Nov 24 '20 at 13:04
1

It is possible to disable your own converter for exactly one invocation of Serialize. This will even work if the converter is used reentrantly and/or multi-threaded.

class MyConverter : JsonConverter
{
  [ThreadStatic]
  private static bool Disabled;

  public override bool CanConvert(Type objectType) =>
    Disabled ? (Disabled = false) : the_type_condition;

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  { ...
    try
    { Disabled = true;
      serializer.Serialize(writer, value); // will not invoke WriteJson recursively
    } finally
    { Disabled = false;
    }
    ...
  }
}
Marcel
  • 1,688
  • 1
  • 14
  • 25
-4

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);
}
B. Ball
  • 114
  • 8
  • This isn't thread-safe. Since almost all web-scale work is multithreaded, this doesn't actually help. – David Pfeffer Jan 17 '19 at 21:35
  • 2
    @DavidPfeffer it seems that would indeed be something to take into account. I think making a new serializer each time you need to serialize or deserialize something would solve this, but perhaps the impact on performance is too great to make that solution viable... – B. Ball Jan 19 '19 at 13:27
-9

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.

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
diurvan
  • 1
  • 2
  • 2
    This does not answer the question that was asked. The question was not "How do I serialize my object using `JsonConvert`?" It was asking how to avoid a recursive loop from inside a custom `JsonConverter`. Note that [`JsonConvert`](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm) and [`JsonConverter`](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm) are two entirely different classes in Json.Net. – Brian Rogers Apr 28 '16 at 14:35