c# - Capture Exception and Set a value for JSONProperty -
c# - Capture Exception and Set a value for JSONProperty -
i wanted capture exceptions in createproperty method of json.net , replace type , message of property exception occured in property.
this have acheived far.
class customresolver : defaultcontractresolver { protected override jsonproperty createproperty(memberinfo member, memberserialization memberserialization) { jsonproperty property = base.createproperty(member, memberserialization); property.shouldserialize = instance => { var prop = (propertyinfo)member; seek { if (prop.canread) { prop.getvalue(instance, null); } } catch(exception exception) { property.propertytype = typeof(string); property.defaultvalue = exception.message; homecoming true; } homecoming true; }; homecoming property; } }
and have class.
class myclass{ public int id {get; set;} public decimal amount { { throw new exception("test exception"); } } }
now, trying serialize object :
var obj = new myclass(); var convertedstring = jsonconvert.serializeobject(obj, formatting.indented, new jsonserializersettings { referenceloophandling = referenceloophandling.ignore, converters = new list<jsonconverter> { new isodatetimeconverter { datetimeformat = "yyyy-mm-dd hh:mm:ss" } }, contractresolver = new customresolver() });
what trying acheive :
{ "id" : 0, "amount" : "test exception" }
but doesn't seems working. can help me out this.
thanks
c# json.net
Comments
Post a Comment