gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:56:02 26 4
gpt4 key购买 nike

我有以下对象,我正在尝试使用 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.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& 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 契约(Contract)的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com