I am trying to use Jackson to convert a JSON blob into an list of instantiated Java class objects. This is fairly straight forward whenever the JSON remains the same. However, each JSON object that comes back from the server starts with a unique UUID, which means that each object is different. It's like this:
{
"863090b2-3d29-4829-9e01-5b03a6d5df04": {
"name": "datasource 1",
"dataSourceId": "863090b2-3d29-4829-9e01-5b03a6d5df04",
"lastmodified": "2015-06-30T14:53:12"
},
"40de3a9e-e436-41b1-a37d-8c1d548293bc": {
"name": "datasource 2",
"dataSourceId": "40de3a9e-e436-41b1-a37d-8c1d548293bc",
"lastmodified": "2015-06-30T14:52:46"
},
"f042db5f-455d-4edb-b8b5-610767735e64": {
"name": "datasource 3",
"dataSourceId": "f042db5f-455d-4edb-b8b5-610767735e64",
"lastmodified": "2015-06-30T14:53:05"
},
"cd6323aa-ec27-4793-bb16-60729a556b97": {
"name": "datasource 4",
"dataSourceId": "cd6323aa-ec27-4793-bb16-60729a556b97",
"lastmodified": "2015-06-30T14:52:55"
}
}
Notice that each JSON object starts with a different name. If each JSON object were to be started with the same name (like "data") instead of with a UUID, this would be a cakewalk. Now I'm getting the following exception:
java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: [B@20e6c4dc; line: 1, column: 1]
This is the code segment I'm getting the exception from:
public List<Schema> getAllSchemas(String clientToken) {
String endpoint = ALL_SCHEMAS_ROUTE + clientToken;
byte[] responseBodyBytes = makeHttpRequest(endpoint, HttpMethod.GET, null).getBytes();
List<Schema> allSchemas = null;
try {
allSchemas = objectMapper.readValue(responseBodyBytes,
new TypeReference<List<Schema>>(){});
} catch (Exception e) {
throw new RuntimeException(e);
}
return allSchemas;
}
Please note that Jackson will dejsonify a blob that contains only one of these objects, but will not do the entire list.
My limitations are these:
Many thanks in advance for your help!
The most promising approach I can think of is to deserialize the whole thing as a Map<String, Schema>, so those IDs will not be treated as fields any more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With