I'm working on a legacy Android project which uses a very old version of Jackson lib to parse JSONs brought from our web API.
I am currently working on a new feature for said project, and would like to use Retrofit with Gson lib as its JSON parser, since both libs provide more flexibility and a cleaner code if compared to our previous lib choices.
Problem is, I will need some of the legacy POJOs, and those already have Jackson annotations. Does putting Gson and Jackson annotations in the same class generate any conflicts? Or will I be forced to create mirror classes with Gson annotations in order to avoid potential issues?
Example:
import org.codehaus.jackson.annotate.JsonProperty;
import com.google.gson.annotations.SerializedName;
public class someClass {
@JsonProperty ("some_attribute") // Jackson annotation
@SerializedName("some_attribute") // Gson annotation
private String someAttribute;
}
Conclusion Both 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.
Last benchmarks Jackson was the winner, but this time GSON is by far the fastest, with JSONP being a close second and then followed by Jackson and then JSON.
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.
Annotations are just metadata. They don't have any behavior on their own.
What does have behavior is the corresponding component that processes the annotation. For example, Jackson's ObjectMapper
(its internals) reads the type you provide, introspects it to find any meaningful annotations, and uses them to serialize/deserialize. @SerializedName
isn't meaningful to Jackson, so it ignores it. Similarly, @JsonProperty
isn't meaningful to Gson, so it ignores it.
There won't be any issues*.
* Unless you write your own type adapters that for whatever reason use another library's annotations.
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