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

时间:2013-07-23 19:25:44

标签: c# json serialization 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方法。为什么序列化程序关心方法。

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

编辑:评论中要求的堆栈跟踪:

  

at 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 item)      at 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,Object value,JsonContainerContract contract,JsonProperty member,JsonProperty property,JsonContract&amp; memberContract,Object&amp; memberValue)      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.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 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.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.Serialize(JsonWriter jsonWriter,Object value,Type objectType)      at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)      at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,Object value,Type objectType)      在Newtonsoft.Json.JsonConvert.SerializeObject(对象值,类型类型,格式化格式,JsonSerializerSettings设置)      在Newtonsoft.Json.JsonConvert.SerializeObject(对象值,格式化格式,JsonSerializerSettings设置)      位于c:\ Code.Net \ ClearChannel \ Sandbox \ APProxyServer \ APProxy \ APProxy.svc.cs中的APProxyServer.APProxy.GetProposal(Int32 proposalID):第194行      在SyncInvokeGetProposal(Object,Object [],Object [])      在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[]输入,对象[]和输出)      在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)

1 个答案:

答案 0 :(得分:4)

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

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);
        }
    }