Does Gson have something like @JsonProperty for methods?

混江龙づ霸主 提交于 2020-12-02 06:35:39

问题


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.

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


回答1:


Try

@SerializedName("serialized_fld_name")




回答2:


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




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/14482662/does-gson-have-something-like-jsonproperty-for-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!