|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
@Retention(value=RUNTIME) @Target(value=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 { @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
Required Element Summary | |
---|---|
String |
value
|
Element Detail |
---|
public abstract String value
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |