2

我有以下对象,我正在尝试使用 Json.NET 将其序列化为 Json

[Serializable]
public class FlightSelection : IEquatable<FlightSelection>
{
    public static readonly DateTime InitialDate;

    public FlightSelection();

    public FlightWeekSelectionType FlightWeekSelectionType { get; set; }
    public bool IsValidProposalLineWeeksExists { get; }
    public int Play { get; set; }
    public List<ProposalLineWeek> ProposalLineWeeks { get; set; }
    public int SelectedCount { get; }
    public int Skip { get; set; }

    public void ApplyPattern();
    public bool Equals(FlightSelection other);
    public override bool Equals(object obj);
    public bool[] ToBoolArray();
    public override string ToString();
}

我尝试使用以下代码对其进行序列化:

var jsSettings = new JsonSerializerSettings();
var fs = new FlightSelection();
string json = JsonConvert.SerializeObject(fs, Formatting.None, jsSettings);

我收到以下错误:The 'obj' argument is not a FlightSelection object.

我真的不明白为什么。我在对象中看到“obj”的唯一位置是 Equals 方法。为什么序列化器关心一个方法。

我错过了一些简单的东西吗?

编辑:按照评论中的要求进行堆栈跟踪:

在 CC.Fusion.Business.Model.FlightSelection.Equals(Object obj) 在 System.Collections.Generic.ObjectEqualityComparer1.IndexOf(T[] array, T value, Int32 startIndex, Int32 count) at System.Array.IndexOf[T](T[] array, T value, Int32 startIndex, Int32 count) at System.Collections.Generic.List1.Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,对象值、JsonContainerContract 合同、JsonProperty 成员、JsonProperty 属性、JsonContract& memberContract、Object& memberValue) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) 在 Newtonsoft。 Json.Serialization.JsonSerializerInternalWriter。SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member , JsonContainerContract 集合合约,JsonProperty containerProperty) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) (JsonWriter jsonWriter, 对象值,类型 objectType) 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) 在 Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType) 在 Newtonsoft.Json.JsonConvert.SerializeObject(Object value , Type type, Formatting formatting, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings) at APProxyServer.APProxy.GetProposal(Int32 proposalID) in c:\Code.Net\ClearChannel\Sandbox\ APProxyServer\APProxy\APProxy.svc.cs: SyncInvokeGetProposal(Object , Object[] , Object[] ) 处的第 194 行,位于 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] 输入, Object[]& 输出)在 System.ServiceModel.Dispatcher。DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

4

1 回答 1

4

我查看了@您指出的代码 (pastebin.com/WQkP45mr) 如果您通过以下方式更改来自 IEquitable 合同的 Equals 方法的实现,它可以工作

public override bool Equals(object obj)
    {

        //if (obj == null) return base.Equals(obj);

        //if (!(obj is FlightSelection))
        //    throw new InvalidCastException("The 'obj' argument is not a FlightSelection object.");
        //else
        //    return Equals(obj as FlightSelection);    

        var flightSelection = obj as FlightSelection;
        if (flightSelection == null)
            return false;
        return Equals(flightSelection);
    }

我得到的结果:

{"FlightWeekSelectionType":0,"Play":1,"ProposalLineWeeks":[],"SelectedCount":0,"
Skip":1,"IsValidProposalLineWeeksExists":false}
Press any key to continue . . .

我希望这有帮助...

编辑 :

如果您无法修改源代码,则以下工作正常,我也进行了测试。

    class Program
    {
        static void Main(string[] args)
        {
            var jsSettings = new JsonSerializerSettings();
            var fs = new AngryHackerFlightSelection();
            string json = JsonConvert.SerializeObject(fs, Newtonsoft.Json.Formatting.None, jsSettings);
            Console.WriteLine(json);
        }
    }

    public class AngryHackerFlightSelection : FlightSelection
    {
        public override bool Equals(object obj)
        {
            var flightSelection = obj as FlightSelection;
            if (flightSelection == null)
                return false;
            return Equals(flightSelection);
        }
    }
于 2013-07-23T19:54:31.243 回答