Unfortunately there doesn't seem to be a way in the current implementation to do this. If you look at the Gson source and more specifically at the JsonObject implementation you will see that the underlying data structure is a linked hash map. The get call simply invokes the get on the map, which in turn uses the hash code and equals method of your key to find the object you are looking for.
Up vote 1 down vote favorite share g+ share fb share tw.
Code shown below works well when JSON object contains jsonKey as it was passed to the method. I wonder ... if there is a way to get a value assigned to a case insensitive representation of a key? Example: public String getOutputEventDescription(JsonElement outputEvent) throws ParserException { return retrieveString(outputEvent, DESCRIPTION); } Should work regardless whether DESCRIPTION is defined as "Description", "description" or "DeScRipTIOn" protected String retrieveString(JsonElement e, String jsonKey) throws ParserException { JsonElement value = e.getAsJsonObject().
Get(jsonKey); if (value == null) { throw new ParserException("Key not found: " + jsonKey); } if (value.getAsString().trim().isEmpty()) { throw new ParserException("Key is empty: " + jsonKey); } return e.getAsJsonObject(). Get(jsonKey).getAsString(); } java json gson case-insensitive link|improve this question edited Jun 13 '11 at 15:56 asked Jun 13 '11 at 15:37JAM3,3861739 98% accept rate.
Unfortunately there doesn't seem to be a way in the current implementation to do this. If you look at the Gson source and more specifically at the JsonObject implementation you will see that the underlying data structure is a linked hash map. The get call simply invokes the get on the map, which in turn uses the hash code and equals method of your key to find the object you are looking for.
The only way around is to enforce some naming conventions for you keys. The easiest way would be to force all the keys to lowercase. If you need mixed case keys then you will have more difficulty and will need to write a more sophisticated algorithm for transforming the keys instead of simply calling jsonKey.toLowerCase().
Unfortunately, registering a FieldNamingStrategy with the GsonBuilder wouldn't do much good, as it translates only in the opposite-than-desired direction: from the Java field name to the JSON element name. It cannot be reasonably used for your purposes. (In Detail: The result of the translation request ends at FieldNamingStrategy.
TranslateName(Field), where the translated name is used to get the associated JSON element from a JsonObject, which has a LinkedHashMap, called members, mapping JSON element names to their associated values. The translated name is used as the parameter to the get(String) method of members, and Gson provides no mechanism for this final call to be made case insensitive. The members map is populated with calls to JsonObject.
Add(String, JsonElement), made from Streams. ParseRecursive(JsonReader), with the JSON element name retrieved from the JsonReader used as the key to 'members'. (JsonReader uses the characters exactly as they are in the JSON, with the exception where the escape character '\' is found.
) Throughout this call stack, Gson provides no mechanism for the keys used to populate members to be altered, e.g. , to be made all lower case or all upper case. A FieldNamingPolicy works in the same way. ) A reasonable solution might be to simply use a custom deserializer, along the following lines.
Input. Json: {"field":"one"}, {"Field":"two"}, {"FIELD":"three"}, {"fIElD":"four"} Foo. Java: import java.io.
FileReader; import java.lang.reflect. Type; import java.util.Map. Entry; import com.google.gson.
Gson; import com.google.gson. GsonBuilder; import com.google.gson. JsonDeserializationContext; import com.google.gson.
JsonDeserializer; import com.google.gson. JsonElement; import com.google.gson. JsonObject; import com.google.gson.
JsonParseException; public class Foo { public static void main(String args) throws Exception { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder. RegisterTypeAdapter(MyClass. Class, new MyTypeAdapter()); Gson gson = gsonBuilder.create(); MyClass myObjects = gson.
FromJson(new FileReader("input. Json"), MyClass. Class); System.out.
Println(gson. ToJson(myObjects)); } } class MyClass { String field; } class MyTypeAdapter implements JsonDeserializer { @Override public MyClass deserialize(JsonElement json, Type myClassType, JsonDeserializationContext context) throws JsonParseException { // json = {"field":"one"} JsonObject originalJsonObject = json.getAsJsonObject(); JsonObject replacementJsonObject = new JsonObject(); for (Entry elementEntry : originalJsonObject.entrySet()) { String key = elementEntry.getKey(); JsonElement value = originalJsonObject. Get(key); key = key.toLowerCase(); replacementJsonObject.
Add(key, value); } return new Gson(). FromJson(replacementJsonObject, MyClass. Class); } } Alternatively, you could first process the raw JSON to alter all of the element names to be the same case, all lower or all upper.
Then, pass the altered JSON to Gson for deserialization. This would of course slow down JSON processing. If you're able to change Gson code for your project, then probably the part to change for the most efficient result is the call to name = nextString((char) quote); in JsonReader.
Since nextString(char) is also used to get the JSON element value, I'd probably just make a copy of it for getting the name, and then make small changes to force the element names to all lower or all upper case. Of course, this approach then locks your project to one release of Gson, else you'd need to repeat this change to upgrade to a newer Gson release. With Jackson, the situation appears unfortunately similar.
Translations with a PropertyNamingStrategy work in unfortunately much the same way: they translate from the Java field name to the JSON element name. None of the available JsonParser. Feature alterations would customize a JsonParser to force JSON element names to all upper or all lower case.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.