Serializing JSON with varing datatype with the same variable

Francis Zabala

I am trying out Opensignal API and have run into a wall when trying to use Jackson 2.0 to serialize the result. The networkRank can be an array or it can be a string

How could I set Jackson to serialize JSON properly, like if it's a string, save it to this String networkRank attribute else save it to List <NetworkRank> = new ArrayList<NetworkRank>()

These are result of their api: (Check out networkRank below)

{
    "apiVersion": "2",
    "latitude": "14.55669",
    "longitude": "121.370119",
    "distance": "10",
    "network_type": "10",
    "perMinuteCurrent": 0,
    "perMinuteLimit": 10,
    "perMonthCurrent": 7,
    "perMonthLimit": 2000,
    "networkRank": "No results for this area"   
}

and

{
    "apiVersion": "2",
    "latitude": "14.55669",
    "longitude": "121.370119",
    "distance": "10",
    "network_type": "10",
    "perMinuteCurrent": 0,
    "perMinuteLimit": 10,
    "perMonthCurrent": 7,
    "perMonthLimit": 2000,
    "networkRank": [
         {
         "networkName": "T-Mobile",
         "networkId": "310260",
  "type3G": {
    "networkName": "T-Mobile",
    "networkId": "310260",
    "networkType": "3",
    "averageRssiAsu": "10.934899",
    "averageRssiDb": "-91.130203",
    "sampleSizeRSSI": "437532",
    "downloadSpeed": "4532.8305",
    "uploadSpeed": "1124.9649",
    "pingTime": "148.8205",
    "reliability": "94.5944170771741"
  },
  "type4G": {
    "networkName": "EE",
    "networkId": "23430",
    "networkType": "4",
    "averageRsrpAsu": "33.865632",
    "averageRsrpDb": "-106.134368",
    "sampleSizeRSRP": "284041",
    "downloadSpeed": "9851.1000",
    "uploadSpeed": "4377.3200",
    "pingTime": "87.0000",
    "reliability": "96.2113095238094"
     }
   }
  ]  
}
Wilson

The simplest way to use a same field for either a json string or a array is probably to use a Object field. This solution do not need a custom serializer and deserializer. Following is an example class with Jackson annotation:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class NetworkStatistics {

    private String apiVersion;
    private String latitude;
    private String longitude;
    private String distance;
    @JsonProperty("network_type")
    private String networkType;
    private int perMinuteCurrent;
    private int perMinuteLimit;
    private int perMonthCurrent;
    private int perMonthLimit;
    private Object networkRank;

    public NetworkStatistics() {
    }

    // getters
}

Following is an example to deserialize a json string of the api response into NetworkStatistics instance:

ObjectMapper mapper = new ObjectMapper();
NetworkStatistics networkStatistics = mapper.readValue(json, NetworkStatistics.class);

and an example to serialize NetworkStatistics instance into json String:

String json = mapper.writeValueAsString(networkStatistics))

If networkRank in json is a string value, the instance field after deserialization will be a String field. If it is a json array, the instance field will be a java.util.ArrayList field.

Hope this can help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related