Does Gson have something like @JsonProperty for methods?

前端 未结 4 1608
温柔的废话
温柔的废话 2021-02-19 18:22

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

相关标签:
4条回答
  • 2021-02-19 19:06

    Try

    @SerializedName("serialized_fld_name")

    0 讨论(0)
  • 2021-02-19 19:08

    No, there is not. As I recall, there is a post in the mailing list from a core developer that Gson won't likely be so enhanced, either.

    0 讨论(0)
  • 2021-02-19 19:10

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

    Source

    0 讨论(0)
  • 2021-02-19 19:13

    I had same problem with Gson and @SerializedName doesn't help in my case. So I used org.codehaus.jackson.map.ObjectMapper

    ObjectMapper mapper = new ObjectMapper();
    String responseJson = mapper.writeValueAsString(object);
    
    0 讨论(0)
提交回复
热议问题