Skip to content

Commit

Permalink
Added javadoc comment in JsonAdapter explaining the usage for fields.
Browse files Browse the repository at this point in the history
Added a (broken) test the ensure a field annotation for JsonAdapter supersedes a class annotation.
  • Loading branch information
inder123 committed Mar 9, 2014
1 parent f88eee0 commit 67af0dd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
19 changes: 17 additions & 2 deletions gson/src/main/java/com/google/gson/annotations/JsonAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.annotation.Target;

import com.google.gson.TypeAdapter;
import com.google.gson.functional.JsonAdapterAnnotationOnFieldsTest.Part;
import com.google.gson.functional.JsonAdapterAnnotationOnFieldsTest.PartJsonAdapter;

/**
* An annotation that indicates the Gson {@link TypeAdapter} to use with a class or a field.
Expand Down Expand Up @@ -59,11 +61,24 @@
* </pre>
*
* Since User class specified UserJsonAdapter.class in &#64JsonAdapter annotation, it
* will automatically be invoked to serialize/deserialize User instances.
* will automatically be invoked to serialize/deserialize User instances. <br>
*
* If the UserJsonAdapter needs a constructor other than a no-args constructor, you must register
* an {@link com.google.gson.InstanceCreator} for it.
*
*
* <p> Here is an example of how to apply this annotation to a field.
* <pre>
* private static final class Gadget {
* &#64JsonAdapter(UserJsonAdapter2.class)
* final User user;
* Gadget(User user) {
* this.user = user;
* }
* }
* </pre>
* The above annotation will ensure UserJsonAdapter2 supersedes UserJsonAdapter for the user
* field of the Gadget class.
*
* @since 2.3
*
* @author Inderjeet Singh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testJsonAdapterInvoked() {
public void testRegisteredTypeAdapterOverridesFieldAnnotation() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Part.class, new TypeAdapter<Part>() {
@Override public void write(JsonWriter out, Part user) throws IOException {
@Override public void write(JsonWriter out, Part part) throws IOException {
out.value("registeredAdapter");
}
@Override public Part read(JsonReader in) throws IOException {
Expand All @@ -56,6 +56,14 @@ public void testRegisteredTypeAdapterOverridesFieldAnnotation() {
assertEquals("registeredAdapterValue", gadget.part.name);
}

public void testFieldAnnotationSupersedesClassAnnotation() {
Gson gson = new Gson();
String json = gson.toJson(new Computer2(new User("Inderjeet Singh")));
assertEquals("{\"user\":\"userJsonAdapter2\"}", json);
Computer2 target = gson.fromJson("{'user':'userJsonAdapter2Value'}", Computer2.class);
assertEquals("userJsonAdapter2Value", target.user.name);
}

private static final class Gadget {
@JsonAdapter(PartJsonAdapter.class)
final Part part;
Expand All @@ -82,7 +90,6 @@ private static class PartJsonAdapter extends TypeAdapter<Part> {
}

private static final class Computer {
@JsonAdapter(UserJsonAdapter.class)
final User user;
Computer(User user) {
this.user = user;
Expand Down Expand Up @@ -119,4 +126,21 @@ private static class UserJsonAdapter extends TypeAdapter<User> {
return new User(firstName + " " + lastName);
}
}

private static final class Computer2 {
// overrides the JsonAdapter annotation of User with this
@JsonAdapter(UserJsonAdapter2.class)
final User user;
Computer2(User user) {
this.user = user;
}
}
private static final class UserJsonAdapter2 extends TypeAdapter<User> {
@Override public void write(JsonWriter out, User user) throws IOException {
out.value("userJsonAdapter2");
}
@Override public User read(JsonReader in) throws IOException {
return new User(in.nextString());
}
}
}

0 comments on commit 67af0dd

Please sign in to comment.