Annotation Interface SerializedName


@Retention(RUNTIME) @Target(FIELD) public @interface SerializedName
An annotation that indicates this member should be serialized to JSON with the provided name value as its field name.

This annotation will override any FieldNamingPolicy, including the default field naming policy, that may have been set on the Gson instance. A different naming policy can set using the GsonBuilder class. See GsonBuilder.setFieldNamingPolicy(com.google.gson.FieldNamingPolicy) for more information.

Here is an example of how this annotation is meant to be used:

public class SomeClassWithFields {
  invalid input: '&#64'SerializedName("name") private final String someField;
  private final String someOtherField;

  public SomeClassWithFields(String a, String b) {
    this.someField = a;
    this.someOtherField = b;
  }
}

The following shows the output that is generated when serializing an instance of the above example class:

SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b");
Gson gson = new Gson();
String jsonRepresentation = gson.toJson(objectToSerialize);
System.out.println(jsonRepresentation);

===== OUTPUT =====
{"name":"a","someOtherField":"b"}

NOTE: The value you specify in this annotation must be a valid JSON field name.

See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
     
  • Element Details

    • value

      String value
      Returns:
      the desired name of the field when it is serialized