| 1 | /* |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.gson.annotations; |
| 18 | |
| 19 | import java.lang.annotation.ElementType; |
| 20 | import java.lang.annotation.Retention; |
| 21 | import java.lang.annotation.RetentionPolicy; |
| 22 | import java.lang.annotation.Target; |
| 23 | |
| 24 | /** |
| 25 | * An annotation that indicates this member should be serialized to JSON with |
| 26 | * the provided name value as its field name. |
| 27 | * |
| 28 | * <p>This annotation will override any {@link com.google.gson.FieldNamingPolicy}, including |
| 29 | * the default field naming policy, that may have been set on the {@link com.google.gson.Gson} |
| 30 | * instance. A different naming policy can set using the {@code GsonBuilder} class. See |
| 31 | * {@link com.google.gson.GsonBuilder#setFieldNamingPolicy(com.google.gson.FieldNamingPolicy)} |
| 32 | * for more information.</p> |
| 33 | * |
| 34 | * <p>Here is an example of how this annotation is meant to be used:</p> |
| 35 | * <pre> |
| 36 | * public class SomeClassWithFields { |
| 37 | * @SerializedName("name") private final String someField; |
| 38 | * private final String someOtherField; |
| 39 | * |
| 40 | * public SomeClassWithFields(String a, String b) { |
| 41 | * this.someField = a; |
| 42 | * this.someOtherField = b; |
| 43 | * } |
| 44 | * } |
| 45 | * </pre> |
| 46 | * |
| 47 | * <p>The following shows the output that is generated when serializing an instance of the |
| 48 | * above example class:</p> |
| 49 | * <pre> |
| 50 | * SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b"); |
| 51 | * Gson gson = new Gson(); |
| 52 | * String jsonRepresentation = gson.toJson(objectToSerialize); |
| 53 | * System.out.println(jsonRepresentation); |
| 54 | * |
| 55 | * ===== OUTPUT ===== |
| 56 | * {"name":"a","someOtherField":"b"} |
| 57 | * </pre> |
| 58 | * |
| 59 | * <p>NOTE: The value you specify in this annotation must be a valid JSON field name.</p> |
| 60 | * |
| 61 | * @see com.google.gson.FieldNamingPolicy |
| 62 | * |
| 63 | * @author Inderjeet Singh |
| 64 | * @author Joel Leitch |
| 65 | */ |
| 66 | @Retention(RetentionPolicy.RUNTIME) |
| 67 | @Target(ElementType.FIELD) |
| 68 | public @interface SerializedName { |
| 69 | |
| 70 | /** |
| 71 | * @return the desired name of the field when it is serialized |
| 72 | */ |
| 73 | String value(); |
| 74 | } |