@Retention(value=RUNTIME) @Target(value=FIELD) public @interface SerializedName
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 { @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.
FieldNamingPolicy
public abstract String value
Copyright © 2008-2013 Google, Inc.. All Rights Reserved.