github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgRpc/kmgRpcJava/java/src/com/google/gson/annotations/JsonAdapter.java (about) 1 /* 2 * Copyright (C) 2014 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 com.google.gson.TypeAdapter; 20 import com.google.gson.TypeAdapterFactory; 21 import java.lang.annotation.ElementType; 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 import java.lang.annotation.Target; 25 26 /** 27 * An annotation that indicates the Gson {@link TypeAdapter} to use with a class 28 * or field. 29 * 30 * <p>Here is an example of how this annotation is used:</p> 31 * <pre> 32 * @JsonAdapter(UserJsonAdapter.class) 33 * public class User { 34 * public final String firstName, lastName; 35 * private User(String firstName, String lastName) { 36 * this.firstName = firstName; 37 * this.lastName = lastName; 38 * } 39 * } 40 * public class UserJsonAdapter extends TypeAdapter<User> { 41 * @Override public void write(JsonWriter out, User user) throws IOException { 42 * // implement write: combine firstName and lastName into name 43 * out.beginObject(); 44 * out.name("name"); 45 * out.value(user.firstName + " " + user.lastName); 46 * out.endObject(); 47 * // implement the write method 48 * } 49 * @Override public User read(JsonReader in) throws IOException { 50 * // implement read: split name into firstName and lastName 51 * in.beginObject(); 52 * in.nextName(); 53 * String[] nameParts = in.nextString().split(" "); 54 * in.endObject(); 55 * return new User(nameParts[0], nameParts[1]); 56 * } 57 * } 58 * </pre> 59 * 60 * Since User class specified UserJsonAdapter.class in @JsonAdapter annotation, it 61 * will automatically be invoked to serialize/deserialize User instances. <br> 62 * 63 * <p> Here is an example of how to apply this annotation to a field. 64 * <pre> 65 * private static final class Gadget { 66 * @JsonAdapter(UserJsonAdapter2.class) 67 * final User user; 68 * Gadget(User user) { 69 * this.user = user; 70 * } 71 * } 72 * </pre> 73 * 74 * It's possible to specify different type adapters on a field, that 75 * field's type, and in the {@link com.google.gson.GsonBuilder}. Field 76 * annotations take precedence over {@code GsonBuilder}-registered type 77 * adapters, which in turn take precedence over annotated types. 78 * 79 * <p>The class referenced by this annotation must be either a {@link 80 * TypeAdapter} or a {@link TypeAdapterFactory}. Using the factory interface 81 * makes it possible to delegate to the enclosing {@code Gson} instance. 82 * 83 * @since 2.3 84 * 85 * @author Inderjeet Singh 86 * @author Joel Leitch 87 * @author Jesse Wilson 88 */ 89 // Note that the above example is taken from AdaptAnnotationTest. 90 @Retention(RetentionPolicy.RUNTIME) 91 @Target({ElementType.TYPE, ElementType.FIELD}) 92 public @interface JsonAdapter { 93 94 /** Either a {@link TypeAdapter} or {@link TypeAdapterFactory}. */ 95 Class<?> value(); 96 97 }