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?
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.
Looking at the average result for all the test runs across all the files, GSON is the winner here, with JSON.
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.
GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.
Try
@SerializedName("serialized_fld_name")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With