In the previous Gson blog post, we've introduced how you can change the (de)serialization naming of a model property. If your server expects (or sends) properties with a different name, you can use @SerializedName
to work around the difference.
In this blog post, we'll show you how you can map multiple names to one property. This is useful if your app interacts with more than one API, who describe the same thing with different names. You can still use one Java class as model!
Of course, this will not be the only post in our Gson series. If you're interested in the other topics, check out our series outline:
Gson Series Overview
- How to Change the Naming of Fields with @SerializedName
- Multiple Deserialization Names with @SerializedName
- How to Ignore Fields with @Expose
- Mapping of Enums
- Mapping of Circular References
- Generics
- Custom Serialization for Simplification (Part 1)
- Changing the Default Serialization with Custom Serialization (Part 2)
- Custom Deserialization Basics
- Custom Instance Creator
- Customizing (De)Serialization via @JsonAdapter
- Custom Deserialization for Calculated Fields
- On-The-Fly-Parsing With Streams
- ProGuard Configuration
Extended Model Annotation @SerializedName
In the first @SerializedName
blog post we've shown you the following use:
public class UserSimple {
@SerializedName("fullName")
String name;
String email;
boolean isDeveloper;
int age;
}
You add the annotation to a model property and pass the serialized and deserialized name as a string.
But this is not everything! SerializedName
accepts two parameters: value
and alternate
. The former is the default parameter. If you pass just a string, you'll set the value
and leave alternate
as a null
value. But you can specifically pass both parameters:
public class UserSimpleSerializedName {
@SerializedName(value = "fullName", alternate = "username")
private String name;
private String email;
private boolean isDeveloper;
private int age;
}
Once again, the value
changes the default serialization and the default deserialization! Thus, if Gson is creating a JSON from your Java model class, it'll use the value
as the name.
The alternate
are only used as additional option during deserialization. Gson will check the JSON for all names you specify and try to find one to map it to the annotated property. For the model class above Gson would check if the incoming JSON would have a fullName
or username
value. Either one would be mapped to the name
property:
{
'fullName': 'Norman',
'email': 'norman@futurestud.io'
}
{
'username': 'Norman',
'email': 'norman@futurestud.io'
}
Both JSON would result in the same Java object.
If there are multiple fields that match one property, Gson will use the one that is processed last. For example, in the following JSON the name
property would be set to Marcus
, since that value comes later:
{
'username': 'Norman',
'fullName': 'Marcus',
'email': 'norman@futurestud.io'
}
If your server creates inconsistent JSONs, you never know which property gets mapped.
Outlook
In this blog post, you've learned how you can further utilize @SerializedName
in order to deal with multiple names for a single property.
Remember, this is limited to deserialization. Gson will map various JSON values to your Java objects, but always create the same JSON during serialization.
Remember, this option to have multiple names is limited to deserialization. Gson will map various JSON values to your Java objects, but always create the same JSON during serialization (which you specify with value
).
If you've feedback or a question, let us know in the comments or on twitter @futurestud_io.
Make it rock & enjoy coding!