@Test public void writeList() throws Exception { Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new ImmutableListTypeAdapterFactory()) .create(); ImmutableList<Integer> list = ImmutableList.of(1, 2, 3); String json = gson.toJson(list); assertEquals(new Gson().toJson(list), json); }
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ImmutableList.class.equals(type.getRawType())) { return null; } return (TypeAdapter<T>) createImmutableListTypeAdapter(gson, type); }
ImmutableListTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ImmutableList.class.equals(type.getRawType())) { return null; } return (TypeAdapter<T>) createImmutableListTypeAdapter(gson, type); } }
ImmutableListTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ImmutableList.class.equals(type.getRawType())) { return null; } return (TypeAdapter<T>) createImmutableListTypeAdapter(gson, type); }  }
ImmutableListTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ImmutableList.class.equals(type.getRawType())) { return null; } return (TypeAdapter<T>) createImmutableListTypeAdapter(gson, type); }  @Override TypeAdapter<T> create(Gson gson, TypeToken<T> type); }
ImmutableListTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ImmutableList.class.equals(type.getRawType())) { return null; } return (TypeAdapter<T>) createImmutableListTypeAdapter(gson, type); }  @Override TypeAdapter<T> create(Gson gson, TypeToken<T> type);  }