c# - Custom null handling with JSON.NET deserialization -
i have following json:
[ { "id": "test1234", "child": { "id": "null" } }, { "id" : "test1235", "child": { "id" : "test1234" } } ] i have mapped following c# class:
[jsonobject] public class myexample { string id; myexample child; } this has 2 objects model parent/child relationship. first object, test1234, should have no child (null). instead, json.net (rightly) deserializes child myexample object id "null". here's behavior want:
var myobjects = jsonconvert.deserializeobject<list<myexample>>(jsontext); assert.areequal("test1234", myobjects[0].id); assert.isnull(myobjects[0].child); assert.areequal("test1235", myobjects[1].id); assert.areequal("test1234", myobjects[1].child.id); is there easy way specify null handling properties? default way of representing null values in api i'm working , have other (related) classes work way.
you might able write custom code detect when id null , assign child null. it's pretty simple , should few lines of code. this:
if (myobject.child.id == null) { myobject.child = null; }
Comments
Post a Comment