GSON tip: @JsonProperty equivalent


Jackson has a very useful annotation @JsonProperty that gives developer the option to provide a name that will be used to map data from JSON to the value object. This is very useful when REST API you are interacting with does not follow conventions like some fields are caps, some camel case, and other using underscore. Today, I had a similar need but I was working with GSON library instead of Jackson. The solution in Gson is a similar annotation called @SerializedName that you can use to provide names that match the source JSON. A simple example is shown below.

public class Message{
    @SerializedName("ID")
    private String id;
    @SerializedName("NFd")
    private int fileDescriptors;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s