java - Processing shell-style comments in JSON -


i'm dealing systems manipulate "relaxed" json data includes shell-style # line comments:

[   {     # batman     "first-name": "bruce",     "last-name": "wayne"   },   {     # superman     "first-name": "clark",     "last-name": "kent"   } ] 

the part of system i'm working on uses json-lib - i'm surprised discover tolerant of shell-style comments - parse json input.

i need extract additional annotation comments, json-lib seems discard them without providing api reading them:

jsonobject map = (jsonobject)jsonserializer.tojson("{\n"+                                                    "    # batman\n" + // note shell-style # comment                                                    "    \"first-name\": \"bruce\",\n" +                                                    "    \"last-name\": \"wayne\"\n" +                                                    "}"); system.out.println(map.tostring()); /* <<'output'  * {"first-name":"bruce","last-name":"wayne"}  * output  * note absence of shell-style comment  */ 

this makes sense since comments aren't part of json spec , i'm lucky json-lib doesn't choke when parsing them in first place.

of note:

  • other systems consume same json , annotations need transparent them, json structure can't modified adding properties comments instead.
  • not components , objects in system have access raw json source: 1 component reads file , parses using jsonlib , passes de-serialized maps etc around.

how can read , parse these comments while processing json input? there library allow me read them , relate them position in json - can connect batman comment "bruce wayne" entry?

i'm using json-lib, i'm open investigating other json libraries , equally open using other languages extend json, such yaml - i'm not sure tools allow me read , process comments in input.

what chose modify public domain json.org library support shell comments , adding comments json object, i've done in github gist:

https://gist.github.com/peteroupc/5529464

example of use:

jsonobject obj=new jsonobject("{ # comment\n"+         "\"first-key\":\"first-value\",\n"+         "\"second-key\":\"second-value\" }",         jsonobject.option_shell_comments | // support shell-style comments         jsonobject.option_add_comments // incorporate comments in json object ); system.out.println(obj); // output json object 

example output. note comment occurs in key called "@comment".

{"second-key":"second-value","@comment":"comment","first-key":"first-value"} 

but 1 of requirements "the json structure can't modified adding properties comments instead." means comments must associated json objects in other way. fortunately, specification called json pointer published rfc 6901. json pointer string refers json object within json object. accordingly, additional steps required: find child objects "@comment" keys, remove keys, , create mapping of json pointers comments.

this illustrated code below.

// objects comments associated them // contain "@comment" key; json pointers // (rfc6901) these objects , remove "@comment" keys. map<string,object> pointers=jsonpointer.getpointerswithkeyandremove(obj,"@comment"); // each json pointer, corresponding object. // jsonobjects. for(string pointer : pointers.keyset()){     jsonobject subobj=(jsonobject)jsonpointer.getobject(obj,pointer);     system.out.println(subobj); // output object     system.out.println(pointers.get(pointer)); // output key's value } 

example output:

{"second-key":"second-value","first-key":"first-value"} comment 

since json pointer new, wrote own implementation of , included in github gist.


here further examples clarify.

given json array (use jsonarray instead of jsonobject in example):

[{ # foo "foo-key":"foo-value"}, { #  # quite long comment. "bar-key":"bar-value"}] 

the result be:

{"foo-key":"foo-value"} foo {"bar-key":"bar-value"} quite long comment. 

as result, multiple comments coalesced single comment. given json array:

[{ # foo "foo-key":"foo-value"}, { #  # quite long comment. "bar-key":"bar-value"  # comment.   }] 

the result be:

{"foo-key":"foo-value"} foo {"bar-key":"bar-value"} comment. 

as result, multiple comments occurred in multiple places on "bar" object not coalesced.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -