custom-collections
This commit is contained in:
parent
339d3dd4d0
commit
90c9eadda1
|
@ -247,7 +247,6 @@ public final class Gson {
|
|||
.factory(TypeAdapters.STRING_BUFFER_FACTORY)
|
||||
.typeAdapter(BigDecimal.class, new BigDecimalTypeAdapter())
|
||||
.typeAdapter(BigInteger.class, new BigIntegerTypeAdapter())
|
||||
.factory(new CollectionTypeAdapterFactory(constructorConstructor))
|
||||
.factory(TypeAdapters.JSON_ELEMENT_FACTORY)
|
||||
.factory(ObjectTypeAdapter.FACTORY);
|
||||
|
||||
|
@ -255,7 +254,9 @@ public final class Gson {
|
|||
builder.factory(factory);
|
||||
}
|
||||
|
||||
builder.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers))
|
||||
builder
|
||||
.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers))
|
||||
.factory(new CollectionTypeAdapterFactory(constructorConstructor))
|
||||
.factory(TypeAdapters.URL_FACTORY)
|
||||
.factory(TypeAdapters.URI_FACTORY)
|
||||
.factory(TypeAdapters.UUID_FACTORY)
|
||||
|
|
|
@ -17,6 +17,11 @@
|
|||
package com.google.gson.functional;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.common.MoreAsserts;
|
||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
@ -283,6 +288,20 @@ public class CollectionTest extends TestCase {
|
|||
HasArrayListField copy = gson.fromJson("{\"longs\":[1,3]}", HasArrayListField.class);
|
||||
assertEquals(Arrays.asList(1L, 3L), copy.longs);
|
||||
}
|
||||
|
||||
public void testUserCollectionTypeAdapter() {
|
||||
Type listOfString = new TypeToken<List<String>>() {}.getType();
|
||||
Object stringListSerializer = new JsonSerializer<List<String>>() {
|
||||
public JsonElement serialize(List<String> src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.get(0) + ";" + src.get(1));
|
||||
}
|
||||
};
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(listOfString, stringListSerializer)
|
||||
.create();
|
||||
assertEquals("\"ab;cd\"", gson.toJson(Arrays.asList("ab", "cd"), listOfString));
|
||||
}
|
||||
|
||||
static class HasArrayListField {
|
||||
ArrayList<Long> longs = new ArrayList<Long>();
|
||||
|
|
Loading…
Reference in New Issue
Block a user