views:

228

answers:

2

I'm using Gson to convert json data I get to a Java object. It works pretty well in all my tests. The problem is that our real objects have some properties named like is_online. Gson only maps them if they are named totally equal, it would be nice to have Gson convert the names to Java camel case isOnline.

It seems this is possible while creating the json data, camel case is converted to underscore separated words in json. But I can't find a way to specify this the other way round.

A: 

Bear in mind your example is an edge case. If you have a property 'foo' its getter should be named 'getFoo', and if you have a property named 'foo_bar' its getter should be named 'getFooBar', however, in your example you're mapping a boolean and booleans have special case naming conventions in java. A primitive boolean property named online should have a getter named 'isOnline', NOT 'getOnline' or even worse, 'getIsOnline'. A boolean wrapper object (i.e. Boolean) should not follow this special case and a property named 'online' should have a getter named 'getOnline'.

Hence, having boolean properties with 'is' in the name is an edge case, where you'll want to strip out this particular prefix during your conversion. In the reverse direction, your code may want to inspect the json object for both a raw property name as well as a 'is_XXX' version.

Jherico
+1  A: 

I think what you want is here. Using annotations you can tell GSON that the mySuperCoolField is actually called this_field_is_fun in the JSON and it will unpack it correctly. At least I think it works for deserialization too.

If that doesn't work, you can use custom JsonSerializer/JsonDeserializers, which work great, but you have to update them for changes in your class (like when you add a field). You lose the auto-magic.

The easiest thing to do (which would be ugly, but very clean and simple if the first suggestion doesn't work) would be to simply name the field in a way to make GSON happy, and add extra accessor methods with the names you like, e.g.

public boolean isXXX() {return this.is_XXX;}
MBCook
the easy thing is what I'm doing at the moment and it works just fine. All the ugly not typical java styled code is hidden in the data classes and nobody from outside will see it. But it is still nagging me a little bit :)
Janusz