Monday, October 10, 2011

JSON parse in java

google-json (1.7.1) is well designed and very fast with preinitializited new Gson() instance.


Gson gson = new Gson();


time in ms in 10 iterations


Data data = gson.fromJson(json, Data.class);



>>> alexey: GoogleJsonParse.main time =0= 62
>>> alexey: GoogleJsonParse.main time =1= 3
>>> alexey: GoogleJsonParse.main time =2= 6
>>> alexey: GoogleJsonParse.main time =3= 1
>>> alexey: GoogleJsonParse.main time =4= 1
>>> alexey: GoogleJsonParse.main time =5= 9
>>> alexey: GoogleJsonParse.main time =6= 1
>>> alexey: GoogleJsonParse.main time =7= 1
>>> alexey: GoogleJsonParse.main time =8= 2
>>> alexey: GoogleJsonParse.main time =9= 1


If you haven't object to cast the parsed data - you can use Collection or Map


Type typeOfT = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> data = gson.fromJson(json, typeOfT);

No comments: