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;
}

Discover more from Shekhar Gulati

Subscribe to get the latest posts sent to your email.

Leave a comment