Does Gson have something like @JsonProperty for methods?

Jackson has an @JsonProperty ("name") annotation that can be applied to methods - the return value of the method will be assigned to the "name" parameter in JSON.

I found out that Gson has the @SerializedName annotation, but cannot be used with methods. Is there a way to get @JsonProperty functionality for methods in Gson?

+6
source share
4 answers

Try

@SerializedName ("serialized_fld_name")

+18
source

No no. As far as I remember, there is a note from the main developer on the mailing list that Gson is also unlikely to be so enhanced.

+1
source

I had the same issue with Gson and @SerializedName does not help in my case. So I used org.codehaus.jackson.map.ObjectMapper

ObjectMapper mapper = new ObjectMapper();
String responseJson = mapper.writeValueAsString(object);
+1
source

The solution in Gson is a similar annotation called @SerializedName, which can be used to provide names that match the original JSON.

A simple example is shown below:

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

Source

0
source

All Articles