[英]Can't serialize an object with 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方法中。 为什么序列化程序关心方法。
我是否缺少简单的东西?
编辑:根据注释中的要求跟踪堆栈:
在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
处CC.Fusion.Business.Model.FlightSelection.Equals(Object obj)处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项)在Newton.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer,Object value,JsonProperty属性(Newsonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,Object value,JsonContainerContract Contract,JsonProperrit成员,JsonProperial成员,JsonProperial成员。 .SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,Jso Newton.Json.Serialization.InnerWriter.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialization.Json.Serialize.InnerWriter.SerializeValue。 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueValueContract,JsonProperritty.JsonContainerS.ProtocolS。 (Newsonsoft.Json.Serialization.J中的(JsonWriter作家,IEnumerable值,JsonArrayContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)) sonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)在Newton.Json.Serialization.JsonSerializerInternalWriter。 )在Newton.Json.SerizationW.JsonSerialWriter()在Ne的Newton.Json.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,对象值,类型objectType)在Newtonsoft.Json.Json.JsonSerializer.SerializeInternal wtonsoft.Json.JsonConvert.SerializeObject(对象值,类型类型,格式设置格式,JsonSerializerSettings设置)位于APProxyServer.APProxy.GetProposal(Int32 proposalID)中,位于APProxyServer.APProxy.GetProposal(Int32 proposalID)上的Object值,格式类型,JsonSerializerSettings设置) :\\ Code.Net \\ ClearChannel \\ Sandbox \\ APProxyServer \\ APProxy \\ APProxy.svc.cs:Line 194在SyncInvokeGetProposal(Object,Object [],Object [])在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)上的[]输入,Object []&输出)
我查看了@您指示的代码(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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.