Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: gson performance

I am trying to use gson to do my object mapping on the android emulator.

It has been ridiculously slow when processing json data around 208 kb. I do not have any hierarchies in my json.

After the object mapping is done, i can see it that gson created around 500 records.

It is taking it over 3 minutes on the android emulator to map the input json.

I have annotated my entity which comprises of strings and couple of floats.

An I missing something?

Any ideas, best practices would greatly help.

Are there any ways of quickly object mapping the json data?

        URL myURL = new URL(url);
        /* Open a connection to that URL. */
        URLConnection ucon = myURL.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        long tickCount = System.currentTimeMillis();
        Policy[] policies = new Gson().fromJson(reader, Policy[].class);
        long endCount = System.currentTimeMillis() - tickCount;
        Log.d("Time to pull policies in milliseconds", "" + endCount);
like image 262
CF_Maintainer Avatar asked Jul 16 '10 00:07

CF_Maintainer


People also ask

Is GSON fast?

Gson is the slowest at deserializing JSONs. It's almost twice as slow as Moshi and JSONObject and slower than Jackson by more than twice in that regard. Another thing to take note at is Gson is the only library producing larger JSON Strings than the other solutions.

Is GSON faster than Jackson?

Last benchmarks Jackson was the winner, but this time GSON is by far the fastest, with JSONP being a close second and then followed by Jackson and then JSON.

Which is better GSON or Moshi?

Moshi is way faster than Gson(Link1, ) and uses less memory, This is due to its usage of Okio which can predict or expect ahead of the time the keys which helps on ignoring the unknown or unwanted fields while parsing a stream (A good article on this).

Which is best Jackson or GSON?

ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.


2 Answers

I've seen questions like this come up before, and the general consensus is that Jackson is much faster than Gson. See the following links for more information:

  • Jackson Vs. Gson
  • Replace standard Android JSON parser for better performance?
  • http://www.cowtowncoder.com/blog/archives/2009/12/entry_345.html
  • https://stackoverflow.com/questions/338586/a-better-java-json-library

Here is one which specifically discusses Android: http://ubikapps.net/?p=525

like image 171
Matt Solnit Avatar answered Oct 12 '22 09:10

Matt Solnit


Have you tried the mixing the GSON streaming parser with the Gson object? http://sites.google.com/site/gson/streaming (look for the Mixed read example).

This approach may help since Gson reads in an entire parse tree and then acts on it. With a large array list, reading in all elements and attempting to parse may cause lot of memory swaps (or thrashing). This approach will read in one element at a time.

Hope this helps.

like image 37
Joel Avatar answered Oct 12 '22 09:10

Joel