2
votes

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.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)

1
What's the stack trace?SLaks
You tried with Ignore Attribute also?Rameez Ahmed Sayad
What is public FlightSelection();? I don't think that would compile?Nathan Dace
@PocketDews : The way it is , the code doesn't compile. I copied & pasted the code on a editor, fixed (by adding dummy return values) and compiled.I did not have any crash. Obtained result is the following : {"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
@PocketDews The object is in the DLL that I have no source for. What you see is from the Object Viewer.AngryHacker

1 Answers

4
votes

I looked @ the code you indicated (pastebin.com/WQkP45mr) if you change the implementation of Equals method coming from IEquitable contract in the following way, it works

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

The result I obtained :

{"FlightWeekSelectionType":0,"Play":1,"ProposalLineWeeks":[],"SelectedCount":0,"
Skip":1,"IsValidProposalLineWeeksExists":false}
Press any key to continue . . .

I hope this helps...

EDIT :

If you can't modify source code the following works ok , I tested too.

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