java - How can I read a value as JSON while using JsonParser.nextToken in Jackson -
i converting tree model streaming api within jackson, prevent out of heap memory issues on android. farther speed processing, reading entire "tags" value array json string. in example, want json string containing 3 tags , values each study id.
a simplified version of info is:
"tagrpts":[ { "id":46, "tags":[ { "name":"tag1", "values":[ {"t":"instance 1","v":"407"}, {"t":"instance 2","v":"360"}, {"t":"instance 3","v":"309"}]}, { "name":"tag2", "values":[ {"t":"instance 1","v":"926"}, {"t":"instance 2","v":"168"}, {"t":"instance 3","v":"366"}]}, { "name":"tag3", "values":[ {"t":"instance 1","v":"744"}, {"t":"instance 2","v":"668"}, {"t":"instance 3","v":"62"}]}]}, { "id":47, "tags":[ { "name":"machine 1", "values":[ {"t":"instance 1","v":"613"}, {"t":"instance 2","v":"882"}, "t":"instance 3","v":"602"}]}, { "name":"machine 2", "values":[ {"t":"instance 1","v":"38"}, {"t":"instance 2","v":"38"}, {"t":"instance 3","v":"329"}]}, { "name":"machine 3", "values":[ {"t":"instance 1","v":"276"}, {"t":"instance 2","v":"721"}, {"t":"instance 3","v":"660"}]}]} ]
i reading code (simplified illustration -- removed areas handling commas stringbuilder, etc):
if ("tagrpts".equals(fieldname)) { // tagrpts jparser.nexttoken(); // [ or null if (jparser.gettext() != "null") { while (jparser.nexttoken() != jsontoken.end_array) { // tag study array while (jparser.nexttoken() != jsontoken.end_object) { fieldname = jparser.getcurrentname(); if ("id".equals(fieldname)) { jparser.nexttoken(); atagreport.id = jparser.gettext();} if ("tags".equals(fieldname)) { stringbuilder sb = new stringbuilder(); sb.append("["); while(jparser.nexttoken() != jsontoken.end_array) { fieldname = jparser.getcurrentname(); if ("name".equals(fieldname)) { jparser.nexttoken(); sb.append("{\"name\":\"" + utility.encodejson(jparser.gettext()) + "\"");} if ("values".equals(fieldname)) { sb.append("\"values\":["); while(jparser.nexttoken() != jsontoken.end_array) { fieldname = jparser.getcurrentname(); if ("t".equals(fieldname)) { jparser.nexttoken(); sb.append("{\"t\":\"" + utility.encodejson(jparser.gettext()) + "\"");} if ("v".equals(fieldname)) { jparser.nexttoken(); sb.append(",\"v\":\"" + utility.encodejson(jparser.gettext()) + "\"}");} } sb.append("]}"); } } sb.append("]"); atagreport.tags = sb.tostring(); } } atagreport.savetagreport(); atagreport = new tagreport(); } } }
why don't utilize jsonparser.readvalueastree()
read sub-tree when @ it? , re-create json using objectmapper.writevalueasstring(rootnode)
.
java json jackson
No comments:
Post a Comment