0

I use Retrofit library for calling request and Gson Сonverter to convert to the class object. But API JSON response not "clear" and I need correct some fields manually (for example, convert html to readable text). But setters are not caused during the conversion.

Is it possible during the creation of the object set fields values only through its setters?

Community
  • 1
  • 1
Mikhail
  • 2,612
  • 3
  • 22
  • 37

1 Answers1

0

You don't have to set a Setters / Constructors , you should only use annotations.

For example if your server response is as follows:

{
    "count": 12,
    "devices": [{
        "adb_url": null,
        "owner": null,
        "ready": true,
        "serial": "XXXXXX",
        "model": "GT-N7100",
        "present": true
    }, {
        "adb_url": null,
        "owner": null,
        "ready": true,
        "serial": "XXXXXX",
        "model": "GT-I9500",
        "present": true
    }]
}

You have to create a Device class and a Devices Response class as follows:

Device Class:

import com.google.gson.annotations.SerializedName;

public class Device {

    @SerializedName("adb_url")
    String adbUrl;
    @SerializedName("owner")
    String owner;
    @SerializedName("ready")
    Boolean isReady;
    @SerializedName("serial")
    String serial;
    @SerializedName("model")
    String model;
    @SerializedName("present")
    Boolean present;

    public Device(String adbUrl, String owner, Boolean isReady, String serial, String model, Boolean present) {
        this.adbUrl = adbUrl;
        this.owner = owner;
        this.isReady = isReady;
        this.serial = serial;
        this.model = model;
        this.present = present;
    }

}

Devices Response Class:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by avilevinshtein on 01/01/2017.
 */
public class DeviceResponse {
    @SerializedName("count")
    int deviceSize;

    @SerializedName("devices")
    List<Device> deviceList;

    public DeviceResponse() {
        deviceSize = 0;
        deviceList = new ArrayList<Device>();
    }

    public DeviceResponse(List<Device> deviceList) {
        this.deviceList = deviceList;
        deviceSize = deviceList.size();
    }

    public DeviceResponse(int deviceSize, List<Device> deviceList) {
        this.deviceSize = deviceSize;
        this.deviceList = deviceList;
    }

    public static DeviceResponse parseJSON(String response) {
        Gson gson = new GsonBuilder().create();
        DeviceResponse deviceResponse = gson.fromJson(response, DeviceResponse.class);
        return deviceResponse;
    }
}

P.S - I only used contractors for convenience.

If you have to create a POST message you should create a DeviceRequest as well that reflects you JSON structure passed in the body of the request.

Notice that the name inside @SerializedName("name of a json field") should be a real JSON key.

Avi Levin
  • 1,868
  • 23
  • 32
  • Thanks for the answer, but if I need to change some fields, how do I do this? In my case I get not exactly "clear" data (some fields contains html instead plain text) and I need correct them. Of course, I can correct them in the getter, but I would like to correct them directly in the object itself (at the moment of creation of the object). – Mikhail Jan 26 '17 at 09:52
  • @Mikhail, can you share a sample response? Is your response is a JSON format? – Avi Levin Jan 26 '17 at 11:24
  • @Mikhail, Gson can work with setters only from v2.3 (https://github.com/google/gson/issues/232). Retrofit currently working with a custom version 2.1. https://search.maven.org/#search%7Cga%7C1%7Ccom.squareup.retrofit2 If you wish to work with setters you'll have to use jackson or other converter. I personally worked in the past with jackson and it worked. – Avi Levin Jan 26 '17 at 11:43
  • @Mikhail, I read that in Gson 2.1, it's possible to do this as an extension with a TypeAdapterFactory based on ReflectiveTypeAdapterFactory. We may want to provide an extension. See this as an exmple http://stackoverflow.com/questions/22307382/how-do-i-implement-typeadapterfactory-in-gson – Avi Levin Jan 26 '17 at 11:55
  • @AviPeter here example of my json - http://codepad.org/SxsalBDK (look at "post_content"). Thank you for example, I will look him. – Mikhail Jan 26 '17 at 12:05
  • @Mikhail - I faced today with the same problem, I found this to be very helpfull http://www.javacreed.com/gson-deserialiser-example/ – Avi Levin Jan 30 '17 at 09:12