c# – 为什么Json.Net在序列化时会在我的对象上调用Equals方法?

前端之家收集整理的这篇文章主要介绍了c# – 为什么Json.Net在序列化时会在我的对象上调用Equals方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用Newtonsoft.Json SerializeObject方法时遇到了一个错误.它在 here之前被问过,但是与Newtonsoft合作的人没有回答为什么会这样.

基本上,当调用SerializeObject时,如下所示:

string json = Newtonsoft.Json.JsonConvert.SerializeObject(from,new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });

我在我的类中覆盖了很多Equals方法中的错误

public override bool Equals(object obj)
{
    if (obj == null)
        return false;

    CapacityConfiguration cc = (CapacityConfiguration)obj; // <-- TypeCastException here; other Properties of the same class are sent in as parameter!
}

当然,通过这样检查,我意识到修复起来很“容易”:

public override bool Equals(object obj)
{
    if (obj is CapacityConfiguration == false)
        return false;

    CapacityConfiguration cc = (CapacityConfiguration)obj;
}

但真正的问题是:
为什么Json.Net在类的Equals方法中发送其他类型的对象?更具体地说,Json.Net似乎在类中发送了许多其他属性,而不是相同类型的另一个对象.

对我来说,这完全是奇怪的.任何输入将不胜感激.

我根据Visual Studio使用“Version 8.0.0.0”.

更新1

它很容易测试,因为它是可重复的:

public class JsonTestClass
{
    public string Name { get; set; }
    public List<int> MyIntList { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        JsonTestClass jtc = (JsonTestClass)obj;
        return true;
    }
}

然后将此代码放在Program.cs或其他任何地方:

JsonTestClass c = new JsonTestClass();
c.Name = "test";
c.MyIntList = new List<int>();
c.MyIntList.Add(1);

string json = Newtonsoft.Json.JsonConvert.SerializeObject(c,new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });

你会得到TypeCast异常:

解决方法

Why will JsonConvert.SerializeObject call the object.Equals method?

因为当您使用JsonConvert.SerializeObject时,会调用方法CheckForCircularReference来检查属性是否重新引用您自己的对象,从而导致无限循环.

private bool CheckForCircularReference(JsonWriter writer,object value,JsonProperty property,JsonContract contract,JsonContainerContract containerContract,JsonProperty containerProperty)

在CheckForCircularReference方法中,部分代码使用Contains方法,如果你的对象没有实现IEquatable< T>,那么它将调用object.Equals.接口.

bool exists = (Serializer._equalityComparer != null)
                ? _serializeStack.Contains(value,Serializer._equalityComparer)
                : _serializeStack.Contains(value);

说明

> _serializeStack是当前正在序列化的对象列表.
> List< T> .Contains方法检查集合中是否包含当前属性.
> List< T> .Contains使用EqualityComparer< T> .Default,其又使用IEquatable< T>.如果类型实现它,或者object.Equals否则.
>对象值参数是您当前的Property对象.

这是一个自引用循环的example

public class JsonTestClass
{
    public string Name { get; set; }
    public List<int> MyIntList { get; set; }
    public JsonTestClass Test{get;set;}
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        JsonTestClass jtc = (JsonTestClass)obj;
        return true;
   }
}

JsonTestClass c = new JsonTestClass();
c.Name = "test";
c.Test = c;
string json = JsonConvert.SerializeObject
               (c,new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });

我们会得到一个例外:

Self referencing loop detected for property ‘test’ with type ‘Program+JsonTestClass’. Path ”.

但如果我们这样做就没有错误

JsonTestClass c = new JsonTestClass();
c.Name = "test";
c.Test = new JsonTestClass();

string json = JsonConvert.SerializeObject
       (c,new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
原文链接:https://www.f2er.com/csharp/244549.html

猜你在找的C#相关文章