Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of GSON over normal JSON parse

The application I am working on is primarily based on manipulating JSON data obtained from the server. Traditional JSON parser extracts values, sets required POJOs and passes on to UI handler to render. This part is working well for now.

I have heard of GSON library and run through its implementation steps. As per my understanding, it (GSON usage) requires the following.

  1. JSON data in proper syntax.
  2. Model class objects matching JSON response.
  3. GSON injector or code snippet to fetch JSON from the server and feeds to GSON.

The above approach sounds rather like object mapping. However, I am unaware about how efficient is GSON compared to old-fashioned JSON parsing; particularly with complex JSONs. And its implications on memory usage?

What do you think?

like image 273
Renjith Avatar asked Mar 11 '13 16:03

Renjith


People also ask

Is GSON better than JSON?

GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.

What is the difference between GSON and JSON?

The JSON format was originally specified by Douglas Crockford. On the other hand, GSON is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Is GSON faster than Jackson?

The tally for fastest library on number of files won is: GSON – 14. JSONP – 5. Jackson – 1.

Which of the following GSON methods are useful for implementing JSON?

Gson is first constructed using GsonBuilder and then, toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.


2 Answers

GSON has been successfully used in several android apps that are in Google Play today. The benefit you get with GSON is that object mapping can save the time spent writing code. As for the implications on memory usage, you can use the fromJson() method call that takes a streaming JSONReader to minimize the String data that is kept in memory, failing which you can try to parse the json data using a JSONReader yourself.

like image 111
Deepak Bala Avatar answered Oct 30 '22 00:10

Deepak Bala


the GSON's goals is well described on official page:

Gson Goals:

  • Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
  • Allow pre-existing unmodifiable objects to be converted to and from JSON
  • Extensive support of Java Generics
  • Allow custom representations for objects
  • Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)

https://code.google.com/p/google-gson/

like image 40
pavko_a Avatar answered Oct 29 '22 23:10

pavko_a