Benefits of GSON over normal JSON parse - android

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.
JSON data in proper syntax.
Model class objects matching JSON response.
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?

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.

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/

Related

How do I use Retrofit2, RxJava2, Gson TypeAdapterFActory to map Gson correctly?

I'm currently using Retrofit2, RxJava2, Retrofit2 RxJava2 Adapter, RxAndroid. FIRST OF ALL, should I be using TypeAdapterFactory to deserialize and serialize my Gson instead of JsonSerializer (I heard the former is faster than the latter)?
My Gson has a very complicated structure:
Picture of JSON complicated structure
First, as I mentioned, I am planning on using TypeAdapterFactory. Is this the best performance solution to map all the different cases of the Gson I have? For example, sometimes just "data" dict, sometimes there's an "errors" field, sometimes "data" has just one field under it, sometimes "data" has another complex json data structure
Even still, I have to make Response objects (ComputerResponse etc) that encapsulate the corresponding Computer object (or map all the json fields to the POJO). I don't want to do this mapping every single time a ComputerResponse is returned, or maybe do the mapping once, in order to improve performance. How should I go about doing that?
I think you just need to create one model that it has all fields in the most complete way, you can create your method with this tool.

What are the advantages of creating pojo classes for Json response in android?

We can also use json response directly so what is the need of converting them in pojo classes
You can directly interact with the service, Dao, without any type checking. And this brings the application of Jackson API in spring.
So that every request can be easily handled as Models.

Passing Entity between activities (serializing)

I'm working on Android app with Google App Engine JAVA backend.
I've got Entity object from app engine backend, and I need to pass it from one Activity to another in Android app. Puting it into bundle as serializable extra throws exception, most probably because my Entity class doesn't implements Serializable (and I guess it can't implement it).
What would be the proper way to serializing entities objects at client side, for bundle packing purposes?
You better use GSON to do this.
Gson is a Java library that can be used to
1. Convert Java Objects into their JSON representation.
2. And also Convert a JSON string to an equivalent Java object.
Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
I found that Gson isn't all that reliable and it takes a lot to write the Adapter. I went to EventBus for help and it works great! :)

Advantages of Parcelable over JSON

I'm currently using JSON (org.json) to serialize one of my data classes. When I pass it in a Bundle or with an Intent, I just call .toString() on the sender side and then recreate the class on the receiving side. From everything I've read so far, I should not implement Java's Serializable due to performance concerns. I'm in the process of rewriting certain portions of the app and I was considering making my data classes implement Parcelable and transfer them that way. What would the advantages be if I did it that way? Would it be preferable if I used the Jackson JSON library instead? Most of the JSON work is based on the API; server responds only with JSON. I also store some of the JSON for caching on the app side.
I think JSON is by far the most convenient mechanism for typical POJOs; and it seems unlikely that performance should be significantly worse that with Parcelable. Parcelable implementation could be more compact; but if that is problematic, you could even compress cached JSON payloads. So I would probably try out JSON first and see how it works.

How to serialize a TreeMap and insert it to a SharedPreferences?

I'm using a TreeMap because it's the most efficient data structure for storing the information I need. I have to be able to persist the TreeMap for future use, so is there a way for me to store it in a SharedPreferences object, maybe serialize it before doing so?
I'm aware of this method here, but using ObjectSerializer from the Apache Pig project makes Eclipse crash. When I run the app, the status bar at the bottom never goes past the message "Launching [app name] 100%", until it throws the following error message:
Unable to execute dex: Java heap space
Unhandled event loop execution
Is there anything I can do to store the TreeMap inside the shared prefs?
If you want to serialize/deserialize, you can go for serializing TreeMap into JSON format as a string object and then store it whereever you want to store it. Later you would be able to deserialize it from JSON format.
You can use famous jackson library for it as jackson library is the industry standard and is free. See at http://wiki.fasterxml.com/JacksonHome.
Edit: JSON format also ensures that you can read the persisted data in other programs as it would be very much readable. Not sure if Jackson JSON library works on android. If so find out some other library for JSON in android.

Categories

Resources