c# - Can't serialize an object with Json.Net -
i have following object , trying serialize json 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 serialize following code:
var jssettings = new jsonserializersettings(); var fs = new flightselection(); string json = jsonconvert.serializeobject(fs, formatting.none, jssettings);
i following error: the 'obj' argument not flightselection object.
i can't understand why. place in object see 'obj' in equals method. why serializer care method.
am missing simple?
edit: stack trace requested in comments:
at cc.fusion.business.model.flightselection.equals(object obj) @ system.collections.generic.objectequalitycomparer
1.indexof(t[] array, t value, int32 startindex, int32 count) @ system.array.indexof[t](t[] array, t value, int32 startindex, int32 count) @ system.collections.generic.list
1.indexof(t item) @ newtonsoft.json.serialization.jsonserializerinternalwriter.checkforcircularreference(jsonwriter writer, object value, jsonproperty property, jsoncontract contract, jsoncontainercontract containercontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.calculatepropertyvalues(jsonwriter writer, object value, jsoncontainercontract contract, jsonproperty member, jsonproperty property, jsoncontract& membercontract, object& membervalue) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, ienumerable values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty) @ newtonsoft.json.serialization.jsonserializerinternalwriter.serialize(jsonwriter jsonwriter, object value, type objecttype) @ newtonsoft.json.jsonserializer.serializeinternal(jsonwriter jsonwriter, object value, type objecttype) @ newtonsoft.json.jsonserializer.serialize(jsonwriter jsonwriter, object value, type objecttype) @ newtonsoft.json.jsonconvert.serializeobject(object value, type type, formatting formatting, jsonserializersettings settings) @ newtonsoft.json.jsonconvert.serializeobject(object value, formatting formatting, jsonserializersettings settings) @ approxyserver.approxy.getproposal(int32 proposalid) in c:\code.net\clearchannel\sandbox\approxyserver\approxy\approxy.svc.cs:line 194 @ syncinvokegetproposal(object , object[] , object[] ) @ system.servicemodel.dispatcher.syncmethodinvoker.invoke(object instance, object[] inputs, object[]& outputs) @ system.servicemodel.dispatcher.dispatchoperationruntime.invokebegin(messagerpc& rpc)
i looked @ code indicated (pastebin.com/wqkp45mr) if change implementation of equals method coming iequitable contract in following way, works
public override bool equals(object obj) { //if (obj == null) return base.equals(obj); //if (!(obj flightselection)) // throw new invalidcastexception("the 'obj' argument not flightselection object."); //else // return equals(obj flightselection); var flightselection = obj flightselection; if (flightselection == null) return false; return equals(flightselection); }
the result obtained :
{"flightweekselectiontype":0,"play":1,"proposallineweeks":[],"selectedcount":0," skip":1,"isvalidproposallineweeksexists":false} press key continue . . .
i hope helps...
edit :
if can't modify source code following works ok , 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 flightselection; if (flightselection == null) return false; return equals(flightselection); } }
Comments
Post a Comment