I have the following object and I am trying to serialize it to Json with Json.NET
[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();
}
I try to serialize it with following code:
var jsSettings = new JsonSerializerSettings();
var fs = new FlightSelection();
string json = JsonConvert.SerializeObject(fs, Formatting.None, jsSettings);
I get the following error: The 'obj' argument is not a FlightSelection object.
I can't really understand why. The only place in the object where I see 'obj' is in the Equals method. Why does the serializer care about a method.
Am I missing something simple?
EDIT: Stack trace as requested in comments:
at CC.Fusion.Business.Model.FlightSelection.Equals(Object obj) at 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& 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)
{"FlightWeekSelectionType":null,"IsValidProposalLineWeeksExists":false,"Play":0, "ProposalLineWeeks":null,"SelectedCount":5,"Skip":0}
. Can we have all the implementation? The crash might be related to your implementation. PS : Did you try to override GetHashCode? – Mechanical Object