Tuesday 15 April 2014

c# - JSON.net is stripping properties when iterating over them -



c# - JSON.net is stripping properties when iterating over them -

i have json string i'm parsing jobject using jobject.parse(string). when properties , objects within json string nowadays evidenced tostring() method call. phone call ienumerable method such as

var _mapping = jobject.parse(json); var _json = _mapping.tostring(); // _json == json @ poing other formating var map = _mapping["c1-14.10.16"]["mappings"]["applog"]["properties"]; foreach(jtoken v in map) { string s = v.tostring(); // s != corresponding node string of json }

many properties disappear v , v's children.

here short snippet of json fails

{"c1-14.10.16":{"mappings":{"applog":{"properties":{"error":{"properties":{"error":{"properties":{"data":{"properties":{"disableprepareforrethrow":{"type":"string","index_analyzer":"standard"},"helplink.basehelpurl":{"type":"string","index_analyzer":"standard"},"helplink.evtid":{"type":"string","index_analyzer":"standard"},"helplink.evtsrc":{"type":"string","index_analyzer":"standard"},"helplink.linkid":{"type":"string","index_analyzer":"standard"},"helplink.prodname":{"type":"string","index_analyzer":"standard"},"microsoft.servicebus":{"type":"string","index_analyzer":"standard"},"length":{"type":"integer"}}}}}}}}}}}}

the property in "c1-14.10.16".mappings.applog.properties.error.properties.error.properties.data.properties recognized length. none of other properties recognized @ all.

what doing wrong makes strip out other properties?

the properties there -- @ much lower level code seems expect. can see follows:

public static void testjsonparse(string json) { seek { var _mapping = jobject.parse(json); var _json = _mapping.tostring(); // _json == json @ poing other formating var map = _mapping["c1-14.10.16"]["mappings"]["applog"]["properties"]; map.writepropertiestoconsole(); debug.assert(map.count() == 1); // no assert because properties aren't here. var submap = _mapping["c1-14.10.16"]["mappings"]["applog"]["properties"]["error"]["properties"]["error"]["properties"]["data"]["properties"]; submap.writepropertiestoconsole(); debug.assert(submap.count() == 8); // no assert - properties here. debug.assert(_mapping["c1-14.10.16"]["mappings"]["applog"]["properties"]["error"]["properties"]["error"]["properties"]["data"]["properties"]["microsoft.servicebus"]["index_analyzer"].tostring() == "standard"); // no assert } grab (exception ex) { debug.assert(false, ex.tostring()); // no exception, no assert. } } public static void writepropertiestoconsole(this jtoken submap) { int itoken = 0; console.writeline(string.format("tokens {0}: {1} found", submap.path, submap.count())); foreach (jtoken v in submap) { string s = v.tostring(); console.writeline(string.format("token {0}: {1}", itoken++, s)); } }

and here console output:

tokens c1-14.10.16.mappings.applog.properties: 1 found token 0: "error": { "properties": { "error": { "properties": { "data": { "properties": { "disableprepareforrethrow": { "type": "string", "index_analyzer": "standard" }, "helplink.basehelpurl": { "type": "string", "index_analyzer": "standard" }, "helplink.evtid": { "type": "string", "index_analyzer": "standard" }, "helplink.evtsrc": { "type": "string", "index_analyzer": "standard" }, "helplink.linkid": { "type": "string", "index_analyzer": "standard" }, "helplink.prodname": { "type": "string", "index_analyzer": "standard" }, "microsoft.servicebus": { "type": "string", "index_analyzer": "standard" }, "length": { "type": "integer" } } } } } } } tokens c1-14.10.16.mappings.applog.properties.error.properties.error.properties.data.properties: 8 found token 0: "disableprepareforrethrow": { "type": "string", "index_analyzer": "standard" } token 1: "helplink.basehelpurl": { "type": "string", "index_analyzer": "standard" } token 2: "helplink.evtid": { "type": "string", "index_analyzer": "standard" } token 3: "helplink.evtsrc": { "type": "string", "index_analyzer": "standard" } token 4: "helplink.linkid": { "type": "string", "index_analyzer": "standard" } token 5: "helplink.prodname": { "type": "string", "index_analyzer": "standard" } token 6: "microsoft.servicebus": { "type": "string", "index_analyzer": "standard" } token 7: "length": { "type": "integer" }

the properties there, @ depth specified in json string provided in question. json.net isn't stripping illustration provided. can see hierarchy using http://jsonformatter.curiousconcept.com/:

if alter problem description follows: "given json, how can recursively find kid property named 'properties' more 1 kid property", can this, adapted here:

static ienumerable<jtoken> findproperties(jtoken root) { homecoming root.walknodes().where(n => { var _parent = n.parent jproperty; if (_parent != null && _parent.name == "properties") { if (n.count() > 1) homecoming true; } homecoming false; }); } public static ienumerable<jtoken> walknodes(this jtoken node) { if (node.type == jtokentype.object) { yield homecoming (jobject)node; foreach (jproperty kid in node.children<jproperty>()) { foreach (var childnode in child.value.walknodes()) yield homecoming childnode; } } else if (node.type == jtokentype.array) { foreach (jtoken kid in node.children()) { foreach (var childnode in child.walknodes()) yield homecoming childnode; } } }

and then, test it:

var _properties = findproperties(_mapping); var list = _properties.toarray(); debug.assert(list.length == 1 && list[0] == submap); // no assert

c# json json.net

No comments:

Post a Comment