Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT

## com.google.gson.annotations
Annotation Type JsonAdapter

    @Retention(value=RUNTIME)
    @Target(value={TYPE,FIELD})
    public @interface JsonAdapter

An annotation that indicates the Gson `TypeAdapter` to use with a class or field.

Here is an example of how this annotation is used:

     @JsonAdapter(UserJsonAdapter.class)
     public class User {
       public final String firstName, lastName;
       private User(String firstName, String lastName) {
         this.firstName = firstName;
         this.lastName = lastName;
       }
     }
     public class UserJsonAdapter extends TypeAdapter<User> {
       @Override public void write(JsonWriter out, User user) throws IOException {
         // implement write: combine firstName and lastName into name
         out.beginObject();
         out.name("name");
         out.value(user.firstName + " " + user.lastName);
         out.endObject();
         // implement the write method
       }
       @Override public User read(JsonReader in) throws IOException {
         // implement read: split name into firstName and lastName
         in.beginObject();
         in.nextName();
         String[] nameParts = in.nextString().split(" ");
         in.endObject();
         return new User(nameParts[0], nameParts[1]);
       }
     }

Since User class specified UserJsonAdapter.class in @JsonAdapter annotation, it will automatically be invoked to serialize/deserialize User instances.

Here is an example of how to apply this annotation to a field.

     private static final class Gadget {
       @JsonAdapter(UserJsonAdapter2.class)
       final User user;
       Gadget(User user) {
         this.user = user;
       }
     }

It's possible to specify different type adapters on a field, that field's type, and in the `GsonBuilder`. Field annotations take precedence over `GsonBuilder`-registered type adapters, which in turn take precedence over annotated types.

The class referenced by this annotation must be either a `TypeAdapter` or a `TypeAdapterFactory`. Using the factory interface makes it possible to delegate to the enclosing `Gson` instance.

Since:
  2.3
Author:
  Inderjeet Singh, Joel Leitch, Jesse Wilson

Required Element Summary
`Class<?>` | `value` Either a `TypeAdapter` or `TypeAdapterFactory`.

Element Detail

### value

    public abstract Class<?> value

  Either a `TypeAdapter` or `TypeAdapterFactory`.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT

Copyright © 2008–2014 Google, Inc.. All rights reserved.