Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Gson have something like @JsonProperty for methods?

Tags:

json

gson

jackson

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?

like image 836
pacoverflow Avatar asked Jan 23 '13 15:01

pacoverflow


People also ask

Is Gson better than Jackson?

ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.

Is Gson better than JSON?

Looking at the average result for all the test runs across all the files, GSON is the winner here, with JSON.

Does Gson use getters and setters?

For now, Gson is fields-based. I don't think shouldn't use getters and setters, Chris Shaffer explains this pretty good in this answer. Write a custom serializer/deserializer, that way you can use whatever method you want to write the values back into your class.

What is difference between JSON and Gson?

GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.


1 Answers

Try

@SerializedName("serialized_fld_name")

like image 115
Snehesh Mitra Avatar answered Nov 25 '22 16:11

Snehesh Mitra