Deserializing arrays using GSON - java

So, I'm quite new to this Json world and well I'm trying to parse this Json below into a class in java using Gson, but I'm not sure if this is the correct way, because I want this to be a list of maps where the nomeArquivo would be the key in this map, can you guys help me to achive this? Or this way I posted is fine?
Test class
public class JsonTeste {
public static void main(String[] args) {
Gson gson = new Gson();
try (Reader reader = new FileReader("foobar.json")) {
List<FastqcJson[]> list = gson.fromJson(reader, new TypeToken<List<FastqcJson[]>>(){}.getType());
for (FastqcJson[] fastqcJsons : list) {
for (FastqcJson fastqcJson : fastqcJsons) {
System.out.println(fastqcJson);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Bean class
public class FastqcJson {
#SerializedName("name")
private String nomeArquivo;
#SerializedName("data")
private HashMap<Integer, Double> mediaBaseNumeros;
....
}
Printed Objects
FastqcJson [nomeArquivo=SRR3192396, mediaBaseNumeros={1=31.939449600540865, 2=32.05829640249262}]
FastqcJson [nomeArquivo=SRR3192397, mediaBaseNumeros={1=32.01549563582736, 2=32.13918804626231}]
Json File
[ [
{
"color": "#5cb85c",
"data": [
[
1,
31.939449600540865
],
[
2,
32.05829640249262
]
],
"name": "SRR3192396"
},
{
"color": "#5cb85c",
"data": [
[
1,
32.01549563582736
],
[
2,
32.13918804626231
]
],
"name": "SRR3192397"
}
]
]

There is no built-in way to do this since "data" is an array of arrays in its native JSON representation.
To do what you want to do you will need to create a wrapper type and write a custom deserializer:
public class MediaBase {
private HashMap<Integer, Double> numeros;
public MediaBase(HashMap<Integer, Double> numeros) {
this.numeros = numeros;
}
}
public class FastqcJson {
#SerializedName("name")
private String nomeArquivo;
#SerializedName("data")
private MediaBase mediaBaseNumeros;
....
}
public class MediaBaseAdapter extends TypeAdapter<MediaBase> {
#Override
public MediaBase read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
HashMap<Integer, Double> output = new HashMap<>();
reader.beginArray(); //Read "data" as array
while (reader.hasNext()) {
reader.beginArray(); //Read data array
int key = reader.nextInt();
double value = reader.nextDouble();
output.put(key, value);
reader.endArray();
}
reader.endArray();
return new MediaBase(output);
}
#Override
public void write(JsonWriter writer, MediaBase value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
// Inverse of reader
HashMap<Integer, Double> output = value.numeros;
writer.beginArray();
for (Map.Entry<Integer, Double> e : output.entries()) {
writer.beginArray();
writer.value(e.getKey());
writer.value(e.getValue());
writer.endArray();
}
writer.endArray();
}
}
Add this adapter during the creation of your GSON instance with GsonBuilder.registerTypeAdapter(MediaBase.class, new MediaBaseAdapter()) and the adaptor will correctly pack and unpack your datatype.
Do note that this is written from memory and I've not been able to verify that it compiles.

Related

Bucket lifecycle configuration using json

I am trying to apply Lifecycle Configurations on S3 bucket. Trying to apply using following JSON:
[{
"id": "tmpdelete",
"status": "Enabled",
"filter": {
"predicate": {
"prefix": "tmp"
}
},
"transitions": [{
"days": "1",
"storageClass": "GLACIER"
}],
"noncurrentVersionTransitions": [{
"days": "1",
"storageClass": "GLACIER"
}],
"expirationInDays": "2",
"noncurrentVersionExpirationInDays": "2",
"expiredObjectDeleteMarker": "true"
}]
When i am trying to map it with Rule[].class it is not working. I am using following code:
String json = above_json;
Rule[] rules = null;
Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithModifiers(Modifier.FINAL,
Modifier.TRANSIENT, Modifier.STATIC, Modifier.ABSTRACT).create();
rules = gson.fromJson(json, Rule[].class);
try {
amazonS3.setBucketLifecycleConfiguration(bucketName, new BucketLifecycleConfiguration().withRules(rules));
} catch (Exception e) {
throw e;
}
It throws error saying Failed to invoke public com.amazonaws.services.s3.model.lifecycle.LifecycleFilterPredicate() with no args. LifecycleFilterPredicate is an abstract class which implements Serializable and it doesn't have no-args contructor. How to solve this problem.?
Ok, I think I found your problem: when GSON tries to construct the objects from that json string into an actual object (or, in this case, a list of objects), the process fails because when it gets to the filter.predicate bit, it probably tries to do something like this:
LifecycleFilterPredicate predicate = new LifecycleFilterPredicate();
predicate.setPrefix("tmp");
Which doesn't work because LifecycleFilterPredicate doesn't have a public constructor without any arguments, as you've stated.
I think that, unfortunately, your only solution is to parse the JSON in a different way.
UPDATE
You'll need to make use of a GSON TypeAdapter as follows:
class LifecycleFilterPredicateAdapter extends TypeAdapter<LifecycleFilterPredicate>
{
#Override
public LifecycleFilterPredicate read(JsonReader reader)
throws IOException
{
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.beginObject();
if(!"prefix".equals(reader.nextName()))
{
return null;
}
String prefix = reader.nextString();
LifecyclePrefixPredicate predicate = new LifecyclePrefixPredicate(prefix);
reader.endObject();
return predicate;
}
#Override
public void write(JsonWriter writer, LifecycleFilterPredicate predicate)
throws IOException
{
//nothing here
}
}
...
Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithModifiers(Modifier.FINAL,
Modifier.TRANSIENT, Modifier.STATIC, Modifier.ABSTRACT)
.registerTypeAdapter(LifecycleFilterPredicate.class, new LifecycleFilterPredicateAdapter()).create();
I've tried it locally and don't get the exception anymore :)
I tried this and it worked for me
public class RuleInstanceCreator implements InstanceCreator<LifecycleFilterPredicate> {
#Override
public LifecycleFilterPredicate createInstance(Type type) {
return new LifecycleFilterPredicate() {
private static final long serialVersionUID = 1L;
#Override
public void accept(LifecyclePredicateVisitor lifecyclePredicateVisitor) {
}
};
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(LifecycleFilterPredicate.class, new LifecycleFilterPredicateAdapter()).create();
rules = gson.fromJson(json, Rule[].class);

Custom Marshalling of HashMap<String,Object> in JAXB

I have a small problem regarding the marshalling via JAXB.
Currently I have a HashMap of Objects
#XmlJavaTypeAdapter(HashMapAdapter.class)
private Map<String, Object> data;
beeing marshalled by the Custom HashMapAdapter
public class HashMapAdapter extends XmlAdapter<HashMapAdapter.AdaptedMap,
Map<String, Object>> {
#XmlRootElement
public static class AdaptedMap {
#XmlVariableNode("key")
List<Data> entries = new ArrayList<>();
}
public static class Data {
#XmlTransient
public String key;
#XmlValue
public Object value;
}
#Override
public Map<String, Object> unmarshal(AdaptedMap v) throws Exception {
throw new NotImplementedException();
}
#Override
public AdaptedMap marshal(Map<String, Object> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Data data = new Data();
data.key = entry.getKey();
data.value = entry.getValue();
adaptedMap.entries.add(data);
}
return adaptedMap;
}
}
The Marshalling is based on the following Post: http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html
The HashMap is filled with either Boolean, Long or String Values.
So regarding the Blog the expected JSON Output should be:
"data": {
"booleanValue": true,
"stringValue": "test",
"longValue": 1234
}
But the real outcome is:
"data": {
"longValue": {
"type": "long",
"value": 1234
},
"stringValue": {
"type": "string",
"value": "test"
},
"booleanValue": {
"type": "boolean",
"value": true
}
}
Im running on Payara Micro 174 and therefore on MOXy as JAXB provider.
Is it possible to get rid of the "type"/"value" nesting?
Best Regards
Simon
I do not have an MOxY implementation handy, could you try this and tell me if it works ?
public static class Data {
#XmlTransient
public String key;
#XmlElements({
#XmlElement(type=Long.class),
#XmlElement(type=String.class),
#XmlElement(type=Boolean.class)
})
#XmlPath(".")
public Object value;
}
EDIT :
The Output you get when using this Approach is:
"data": {
"stringValue": {
"value": test
},
"booleanValue": {
"value": true
},
"longValue": {
"value": 1234
}
}
Sadly this differs a little from the expected.

How to parse dynamic JSON with Retrofit 2 (+Nested objects)

I'm trying to parse JSON response that looks like this.
{
"Cryptsy": {
"AMC": [
"BTC"
],
"CIRC": [
"BTC"
],
"SYNC": [
"BTC"
]
},
"Bitstamp": {
"EUR": [
"USD"
],
"ETH": [
"USD",
"EUR"
],
"XRP": [
"USD",
"EUR",
"BTC"
]
},
// ...
// More objects...
// ...
}
As you can see, this one has the dynamic keys and each values are also objects with dynamic keys. I tried parse this using retrofit2 and GsonConverter but it causes the exception
W/System.err: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
I think it's because the JSON is nested and all objects don't have any fixed keys.
Here's my code.
PairListResponse.java
// This is the GSON model class
class PairListResponse {
private Map<String, Map<String, String[]>> exchangePairs;
PairListResponse() {
}
Map<String, Map<String, String[]>> getExchangePairs() {
return exchangePairs;
}
void setExchangePairs(Map<String, Map<String, String[]>> exchangePairs) {
this.exchangePairs = exchangePairs;
}
Map<String, String[]> getTradingPairs(String fromSymbol) {
return exchangePairs.get(fromSymbol);
}
}
PairListDeserializer.java
public class PairListDeserializer implements JsonDeserializer<PairListResponse> {
private static final String TAG = PairListDeserializer.class.getSimpleName();
#Override
public PairListResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final Map<String, Map<String, String[]>> exchangePairs = readPairMap(jsonObject);
PairListResponse result = new PairListResponse();
result.setExchangePairs(exchangePairs);
return result;
}
#Nullable
private Map<String, Map<String, String[]>> readPairMap(#NonNull final JsonObject jsonObject) {
// Initializing Hashmap for the outer object
final Map<String, Map<String, String[]>> result = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String exchange = entry.getKey();
String fromSymbol;
String[] toSymbols;
JsonObject fsymbolObj = entry.getValue().getAsJsonObject();
// Initializing Hashmap for inner objects
final Map<String, String[]> pairsPerCoin = new HashMap<>();
for (Map.Entry<String, JsonElement> inner_entry : fsymbolObj.entrySet()) {
fromSymbol = inner_entry.getKey();
toSymbols = toStringArray(inner_entry.getValue().getAsJsonArray());
pairsPerCoin.put(fromSymbol, toSymbols);
}
result.put(exchange, pairsPerCoin);
}
return result;
}
private static String[] toStringArray(JsonArray array) {
if (array == null) return null;
String[] arr = new String[array.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = array.get(i).toString();
}
return arr;
}
}
Thanks in advance!
Sorry, I made a dumbest mistake!
In my retrofit API call, I forgot to set the correct model class name.
public interface TradingPairAPICall {
#GET("exchanges")
Call<String> getAllPairList();
}
In fact, it needs to be
Call<PairListResponse> getAllPairList();
I changed it and it successfully worked.

Injecting json property based on condition using Jackson

I have a json format which I am converting into Java Object Model using Jackson API. I am using Jaxsonxml 2.1.5 parser. The json response is as shown below.
{
"response": {
"name": "states",
"total-records": "1",
"content": {
"data": {
"name": "OK",
"details": {
"id": "1234",
"name": "Oklahoma"
}
}
}
}
}
Now json response format has changed. If the total-records is 1 the details will be an object with id and name attributes. But if the total-records is more than 1 then the details will be an array of object like below:
{
"response": {
"name": "states",
"total-records": "4",
"content": {
"data": {
"name": "OK",
"details": [
{
"id": "1234",
"name": "Oklahoma"
},
{
"id": "1235",
"name": "Utah"
},
{
"id": "1236",
"name": "Texas"
},
{
"id": "1237",
"name": "Arizona"
}
]
}
}
}
}
My Java Mapper class looks like below with earlier json response.
#JsonIgnoreProperties(ignoreUnknown = true)
public class MapModelResponseList {
#JsonProperty("name")
private String name;
#JsonProperty("total-records")
private String records;
#JsonProperty(content")
private Model model;
public Model getModelResponse() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
}
Client Code
package com.test.deserializer;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com..schema.model.Person;
public class TestClient {
public static void main(String[] args) {
String response1="{\"id\":1234,\"name\":\"Pradeep\"}";
TestClient client = new TestClient();
try {
Person response = client.readJSONResponse(response1, Person.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public <T extends Object> T readJSONResponse(String response, Class<T> type) {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
T result = null;
try {
result = mapper.readValue(response, type);
} catch (Exception e) {
e.printStackTrace();
}
return (T) result;
}
}
Now based on the total-records how to handle to mapping to either a Model or list of Model Object. Please let me know.
You need a custom deserializer. The idea is to mix and match object processing with tree processing. Parse objects where possible but use the tree (JSONNode) for custom handling.
On the MapModelResponseList, remove the records property and add a List<Data> array where Data is just a holder class for the id/name pairs. You can get the total records by returning the size of this list.
In the deserializer, do the following:
public final class MapModelDeserializer extends BeanDeserializer {
public MapModelDeserializer(BeanDeserializerBase src) {
super(src);
}
protected void handleUnknownProperty(JsonParser jp, DeserializationContext ctxt, Object beanOrClass, String propName) throws IOException, JsonProcessingException {
if ("content".equals(propName)) {
MapModelResponseList response = (MapModelResponseList) beanOrClass;
// this probably needs null checks!
JsonNode details = (JsonNode) jp.getCodec().readTree(jp).get("data").get("details");
// read as array and create a Data object for each element
if (details.isArray()) {
List<Data> data = new java.util.ArrayList<Data>(details.size());
for (int i = 0; i < details.size(); i++) {
Data d = jp.getCodec().treeToValue(details.get(i), Data.class);
data.add(d);
}
response.setData(data);
}
// read a single object
else {
Data d = jp.getCodec().treeToValue(details, Data.class);
response.setData(java.util.Collections.singletonList(d));
}
super.handleUnknownProperty(jp, ctxt, beanOrClass, propName);
}
Note that you do not implement deserialize() - the default implementation is used to create the MapModelResponseList as normal. handleUknownProperty() is used to deal with the content element. Other data you don't care about is ignored due to #JsonIgnoreProperties(ignoreUnknown = true) in the super call.
This is a late answer, but I solve it in a different way. It can work by catching it in Object like this:
#JsonProperty("details")
public void setDetails(Object details) {
if (details instanceof List) {
setDetails((List) details);
} else if (details instanceof Map) {
setDetails((Map) details);
}
}
public void setDetails(List details) {
// your list handler here
}
public void setDetails(Map details) {
// your map handler here
}

Handling JSON response in Gerenic manner

I have two different Json responses(having different keys) generated out of two different requests :
Response 1 :
{
"response": {
"count": 2,
"programs": [
{
"title": "xyz1",
"desc": "ABCDEF1"
},
{
"title": "xyz2",
"desc": "ABCDEF2"
}
]
}
}
Response 2
{
"response": {
"count": 3,
"shows": [
{
"name": "PQR1",
"desc": "qwerty1"
},
{
"name": "PQR2",
"desc": "qwerty2"
},
{
"name": "PQR3",
"desc": "qwerty3"
}
]
}
}
As we can see the responses contain data with different keys. But Ultimately It could be transformed into (Array of) same Java object like this one:
Program {
String title;
int description;
}
I want to write single parsing logic that handles different key names and return Program list. How to achieve this efficiently?
Is there any library available to conveniently do this ?
You may choose the field in the getter when deserialized both of them (example works with GSON):
class Program {
private String title, name;
#SerializedName("desc") private String description;
private String getTitle() {
return title == null ? name : title;
}
// other getters, empty constructor and so on...
}
Also (again GSON), you can register your own TypeAdapter when creating Gson object.
// let Program have empty constructor (or no constructors at all), getters and setters
class ProgramAdapter extends TypeAdapter<Program> {
#Override
public Program read(final JsonReader in) throws IOException {
final Program obj = new Program();
in.beginObject();
while (in.hasNext()) {
String jsonTag = in.nextName();
if ("desc".equals(jsonTag)) {
obj.setDescription(in.nextString());
} else if ("title".equals(jsonTag)
|| "name".equals(jsonTag)) {
obj.setTitle(in.nextString());
}
}
in.endObject();
return obj;
}
#Override
public void write(final JsonWriter out, final Program obj)
throws IOException {
out.beginObject();
out.name("title").value(obj.getTitle());
out.name("desc").value(obj.getDescription());
out.endObject();
}
}
// then, when create `Gson` object:
Gson gson = new GsonBuilder().registerTypeAdapter(Program.class, new ProgramAdapter()).create();

Categories

Resources