Annotation Type JsonAlias
-
@Target({ANNOTATION_TYPE,FIELD,METHOD,PARAMETER}) @Retention(RUNTIME) public @interface JsonAlias
Annotation that can be used to define one or more alternative names for a property, accepted during deserialization as alternative to the official name. Alias information is also exposed during POJO introspection, but has no effect during serialization where primary name is always used.Examples:
public class Info { @JsonAlias({ "n", "Name" }) public String name; }
NOTE: Order of alias declaration has no effect. All properties are assigned in the order they come from incoming JSON document. If same property is assigned more than once with different value, later will remain. For example, deserializing
public class Person { @JsonAlias({ "name", "fullName" }) public String name; }
from{ "fullName": "Faster Jackson", "name": "Jackson" }
will have value "Jackson".Also, can be used with enums where incoming JSON properties may not match the defined enum values. For instance, if you have an enum called
Size
with valuesSMALL
,MEDIUM
, andLARGE
, you can use this annotation to define alternate values for each enum value. This way, the deserialization process can map the incoming JSON values to the correct enum values.Sample implementation:
public enum Size { @JsonAlias({ "small", "s", "S" }) SMALL, @JsonAlias({ "medium", "m", "M" }) MEDIUM, @JsonAlias({ "large", "l", "L" }) LARGE }
During deserialization, any of these JSON structures will be valid and correctly mapped to the MEDIUM enum value: {"size": "m"}, {"size": "medium"}, or {"size": "M"}.
- Since:
- 2.9
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.String[]
value
One or more secondary names to accept as aliases to the official name.
-