Using Gson, I am trying to force my json objects to have an array value with one element. Although I construct the json this way, the JsonParser creates a new JsonObject in place of my array of length 1. The majority of json in my system have value with arrays greater than length 1 and parse correctly.
I discovered this while attempting a utility method for brute forcing a JsonObject to JsonArray. It's not working out as I would expect.
static JsonArray bruteForceJsonArray(JsonObject object, String key) {
if(object.get(key).isJsonArray()) {
return object.get(key).getAsJsonArray();
} else {
System.out.println("broken: " + object.toString());
JsonObject temp = object.get(key).getAsJsonObject();
String fixed = "{'" + key + "':[" + temp.toString() + "]}";
System.out.println("pre-fix: " + fixed);
//parsing here reduces the array to an object - hot to prevent?
temp = new JsonParser().parse(fixed).getAsJsonObject();
System.out.println("fixed: " + object.toString());
return object.get(key).getAsJsonArray();
}
}
output:
broken: {"token":{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}}
pre-fix: {'token':[{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}]}
fixed: {"token":{"NER":"O","id":1.0,"word":".","CharacterOffsetEnd":765.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":764.0}}
An example of the majority json as they appear correctly in the system:
{"token":[{"Speaker":"PER0","POS":"IN","lemma":"except","CharacterOffsetBegin":742.0,"NER":"O","id":1.0,"word":"Except","CharacterOffsetEnd":748.0},{"CharacterOffsetEnd":752.0,"Speaker":"PER0","POS":"IN","lemma":"for","CharacterOffsetBegin":749.0,"NER":"O","id":2.0,"word":"for"},{"id":3.0,"word":"extra","CharacterOffsetEnd":758.0,"Speaker":"PER0","POS":"JJ","lemma":"extra","CharacterOffsetBegin":753.0,"NER":"O"},{"Speaker":"PER0","POS":"NN","lemma":"meat","CharacterOffsetBegin":759.0,"NER":"O","id":4.0,"word":"meat","CharacterOffsetEnd":763.0},{"CharacterOffsetEnd":764.0,"Speaker":"PER0","POS":".","lemma":".","CharacterOffsetBegin":763.0,"NER":"O","id":5.0,"word":"."}]}
Is there a clever way out of this problem - some way I can effectively force my objects into arrays so I can process them in stream without too much try/catch or if/else handling?