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

标签 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);

我收到以下错误:“obj”参数不是 FlightSelection 对象。

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

我错过了一些简单的事情吗?

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

at CC.Fusion.Business.Model.FlightSelection.Equals(Object obj) at 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.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& memberContract, Object& 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) at 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:line 194 at SyncInvokeGetProposal(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(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);
        }
    }

关于c# - 无法使用 Json.Net 序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819283/

相关文章:

c# - 可重用的 C#、Java 和/或 Flex 组件

java - Jackson 使用可变键序列化对象的数组列表

c++ - 为什么 const char* 不适用于 stringstream 和 boost 的 read_json?

html - 如何获得 chrome 扩展程序来影响网页本身? (不是弹出窗口)

serialization - 如何解码二进制/原始谷歌 protobuf 数据

c++ - 用自己的类序列化 QHash?

c# - WPF Windows 8 兼容性问题

c# - 使用 Type 类访问 XML 文档

c# - 从 C# 代码隐藏触发 Javascript

json - Flutter Google Maps Polyline-无法将参数类型 'LatLng'分配给参数类型 'List<LatLng>'