ga

Thursday, June 16, 2011

I am doing an android app that will be reading JSON from the web. Initially I thought I was going to use Jackson, because I heard it was the fastest out there. Initially, I found the documentation and api a bit obtuse, but I figured it out and here is what I got:

HttpGet httpRequest = new HttpGet(ROTTEN_TOMATOES_IN_THEATERS);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();

ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
long start = System.currentTimeMillis();
RTContainer movie = mapper.readValue(is, RTContainer.class);
start = System.currentTimeMillis() - start;
Log.v(TAG, movie.toString() + "runtime = " + start);
is.close();

Fetching 5 movies I got times like this:

06-16 16:29:00.989: VERBOSE/JacksonJsonReader(814): com.appease.movies.rt.util.RTContainer@4066af28runtime = 1117
06-16 16:29:08.770: VERBOSE/JacksonJsonReader(814): com.appease.movies.rt.util.RTContainer@4050fbe0runtime = 1181
com.appease.movies.rt.util.RTContainer@40651f50runtime = 943
06-16 16:29:15.238: VERBOSE/JacksonJsonReader(814): com.appease.movies.rt.util.RTContainer@405f1638runtime = 883
06-16 16:29:18.099: VERBOSE/JacksonJsonReader(814): com.appease.movies.rt.util.RTContainer@4062f2b8runtime = 1099
16:29:20.537: VERBOSE/JacksonJsonReader(814): com.appease.movies.rt.util.RTContainer@40650230runtime = 879


So, I decided to test out Gson, code:

HttpGet httpRequest = new HttpGet(ROTTEN_TOMATOES_IN_THEATERS);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();

Reader reader = new InputStreamReader(is, "UTF-8");
Gson gson = (new GsonBuilder()).create();
long start = System.currentTimeMillis();
RTContainer movie = gson.fromJson(reader, RTContainer.class);
start = System.currentTimeMillis() - start;
Log.v(TAG, movie.toString() + "runtime = " + start);
is.close();

with times like this:
06-16 16:16:34.058: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@405b6550runtime = 789
06-16 16:16:38.508: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@40635008runtime = 614
06-16 16:16:41.247: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@4060fae8runtime = 755
06-16 16:16:43.607: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@405167c0runtime = 422
06-16 16:16:51.658: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@406421d0runtime = 414
06-16 16:16:54.078: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@405bc620runtime = 425
06-16 16:17:17.248: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@4063cca0runtime = 828
06-16 16:17:18.299: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@40679710runtime = 737
06-16 16:17:21.058: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@4059fc58runtime = 429
06-16 16:17:35.807: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@4052a5e0runtime = 385
06-16 16:17:39.348: VERBOSE/GsonReader(745): com.appease.movies.rt.util.RTContainer@40571c50runtime = 745

So, I am going to stick with Gson. The calls are being made in the same way via an async task with nothing else going on.