| Start |
|---|
| Parsing JSON with Gson The example shows parsing a JSON object using the Gson library from Google (opens new window). Parsing objects: Then where parsing needs to occur, use the following: Parsing a list: When retrieving a list of JSON objects, often you will want to parse them and convert them into Java objects. The JSON string that we will try to convert is the following: This particular JSON array contains three objects. In our Java code we'll want to map these objects to To convert the JSON array to a Converting a To convert the JSON array to an The Type object Alternatively, converting a |
| Adding a custom Converter to Gson Sometimes you need to serialize or deserialize some fields in a desired format, for example your backend may use the format "YYYY-MM-dd HH:mm" for dates and you want your POJOS to use the DateTime class in Joda Time. In order to automatically convert these strings into DateTimes object, you can use a custom converter. To make Gson use the newly created converter you need to assign it when creating the Gson object: In order to deserialize the date in that format you only have to define a field in the DateTime format: When Gson encounters a field of type DateTime, it will call your converter in order to deserialize the field. |
| Parsing a List Method 1 This is useful for most generic container classes, since you can't get the class of a parameterized type (ie: you can't call Method 2 Alternatively, you can always subclass the type you want, and then pass in that class. However this isn't always best practice, since it will return to you an object of type |
| Parsing JSON to Generic Class Object with Gson Suppose we have a JSON string: We can parse this JSON string into a But if we want parse it into a Here is the sample : Suppose we have two classes below: and we have a JSON string that should be parsed to a This example shows how to parse this JSON string to the related generic class object: If the JSON string should be parsed to a |
Adding Gson to your project To use latest version of Gson The below line will compile latest version of gson library everytime you compile, you do not have to change version. Pros: You can use latest features, speed and less bugs. |
| Parsing JSON property to enum with Gson If you want to parse a String to enum with Gson: {"status" : "open"} |
| JSON Serialization/Deserialization with AutoValue and Gson Import in your gradle root file Import in your gradle app file Create object with autovalue: Create your Gson converter with your GsonBuilder Deserialize Serialize Using Gson is a great way to simplify Serialization and Deserialization code by using POJO objects. The side effect is that reflection is costly performance wise. That's why using AutoValue-Gson to generate CustomTypeAdapter will avoid this reflection cost while staying very simple to update when an api change is happening. |
| Using Gson to load a JSON file from disk. This will load a JSON file from disk and convert it to the given type. |
| Using Gson as serializer with Retrofit First of all you need to add the Then, you have to add the converter factory when creating the Retrofit Service: You can add custom converters when creating the Gson object that you are passing to the factory. Allowing you to create custom type conversions. |
| Parsing json array to generic class using Gson Suppose we have a json : We can parse this array into a Custom Tweets (tweets list container) object manually, but it is easier to do it with Suppose we have two classes below: and if you need just parse a json array you can use this code in your parsing: |
| Custom JSON Deserializer using Gson Imagine you have all dates in all responses in some custom format, for instance Example of json: Suppose we have this class below: Custom deserializer: And the usage: Serialize and deserialize Jackson JSON strings with Date types This also applies to the case where you want to make Gson Date conversion compatible with Jackson, for example. Jackson usually serializes Date to "milliseconds since epoch" whereas Gson uses a readable format like To circumvent this, you can add a custom serializer and a custom deserializer: |
| Using Gson with inheritance Gson does not support inheritance out of the box. Let's say we have the following class hierarchy: And now we want to serialize an instance of Now, in another place, we receive this json string and want to deserialize it - but in compile time we only know it is supposed to be an instance of But GSON does not know How to solve this? You need to build your own First, add the following field to your base class And initialize it in the base class constructor Now add the following class: All there is left to do is hook everything up - And now, running the following code- Will print out 5. Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference