无法使用 Json.Net 序列化对象



我有以下对象,我正试图用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.ObjectEqualityComparer 1.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.List 1.IndexOf(T项)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter编写器,对象值,JsonProperty属性,JsonContract合同,JsonContainerContract,JsonPropertycontainerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter编写器,对象值,JsonObjectContract约定,JsonProperty成员,JsonContainerContract集合contract,JsonPropertycontainerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter编写器,对象值,JsonObjectContract约定,JsonProperty成员,JsonContainerContract集合contract,JsonPropertycontainerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter编写器,IEnumerable值,JsonArrayContract合约,JsonProperty成员,JsonContainerContract集合合约,Json Property containerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter编写器,对象值,JsonObjectContract约定,JsonProperty成员,JsonContainerContract集合contract,JsonPropertycontainerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter JsonWriter,Object value,Type 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(对象值、类型类型、格式化格式、JsonSerializerSettings设置)位于Newtonsoft.Json.JsonConvert.SerializeObject(对象值、格式化格式、JsonSerializerSettings设置)位于c:\Code.Net\ClearChannel\SSandbox\APProxyServer\APProxy\APProxy.svc.cs:line 194中的APProxyServer.APProxy.GetProposal(Int32 proposalID)在SyncInvokeGetProposal(Object,Object[],Object[])位于System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例、对象[]输入、对象[]和输出)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.IInvokeBegin(MessageRpc&rpc)

我查看了您指示的代码(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);
        }
    }

相关内容

  • 没有找到相关文章

最新更新