使用[JsonConverter]属性时,我的JsonConverter类中的StackOverflowException

时间:2014-03-14 14:07:20

标签: .net c#-4.0 json.net

我有一个测试类,我正在尝试序列化:

public class Testing
{
    private string _name;
    private string _firstname = "firstname";
    public string _lastname;
    private DateTime _datenow = DateTime.Now;
    public DateTime _birthdate = DateTime.Now;

    public string Name { get { return _name; } set { _name = value; } }
}

我正在使用自定义JsonConverter来处理测试类的序列化:

public class TestingConverter : JsonConverter
{
    private Type[] _types = new Type[] { typeof(Testing)};

    /// <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)
    {
        try
        {
            JToken t = JToken.FromObject(value); //This is what i want to do
            //rest of code
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            throw;
        }
    }

    public override bool CanRead
    {
        get 
        { 
            return false;
        }
    }
}

如果我通过将转换器传递给JsonSerializer对象来执行序列化,它可以正常工作:

Testing t = new Testing();
t._lastname = "USER LAST NAME";
JsonSerializer p = JsonSerializer.CreateDefault();
p.Converters.Add(new TestingConverter());

using (StreamWriter file = File.CreateText("output.json"))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
    p.Serialize(writer, t);
}

但是,如果我用[JsonConverter]属性标记我的测试类:

[JsonConverter(typeof(TestingConverter))]
public class Testing
{
    private string _name;
    private string _firstname = "firstname";
    public string _lastname;
    private DateTime _datenow = DateTime.Now;
    public DateTime _birthdate = DateTime.Now;

    public string Name { get { return _name; } set { _name = value; } }
}

并像这样序列化:

Testing t = new Testing();
t._lastname = "USER LAST NAME";
JsonSerializer p = JsonSerializer.CreateDefault();


using (StreamWriter file = File.CreateText("output.json"))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
   p.Serialize(writer, t);
}

调用了我的TestingConverter类,但它在JToken.FromObject(value)方法进入递归循环,最后崩溃为StackOverflowException

谁能告诉我为什么会这样?我错过了什么?

1 个答案:

答案 0 :(得分:6)

将转换器的实例传递给序列化程序时,只有序列化程序的实例知道转换器。在转换器内部,当您调用JToken.FromObject(value)时,它会使用不同的序列化程序实例将值转换为JToken。该实例不了解您的转换器,因此它按预期使用Json.Net的默认序列化逻辑。一切都很好。

另一方面,如果在类型上放置[JsonConverter]属性以指示转换器处理类型,现在所有序列化程序实例都知道您的转换器。转换器中对JToken.FromObject(value)的调用会启动一个新的序列化程序实例;该实例看到它应该使用您的转换器来处理此对象类型,因此它以递归方式调用您的转换器。这会一直重复,直到你的堆栈空间不足为止。

如果要使用[JsonConverter]属性,则需要更改转换器的内部以避免递归调用链。通常这涉及手动处理该类型的所有单个属性。例如,以下版本将适用于所应用的属性:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    Testing t = (Testing)value;
    JObject jo = new JObject();
    jo.Add("name", t.Name);
    jo.Add("lastname", t._lastname);
    jo.Add("birthdate", t._birthdate);
    jo.WriteTo(writer);
}