From 9be0fd9ecc91ba0eacf885faeec5f3d1d82fa99f Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 18 Mar 2012 17:55:15 +0000 Subject: [PATCH] Make the BigDecimal and BigInteger type adapters user-overrideable. --- gson/src/main/java/com/google/gson/Gson.java | 4 +-- .../functional/DefaultTypeAdaptersTest.java | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 84dc68b8..6e58c512 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -211,8 +211,6 @@ public final class Gson { factories.add(TypeAdapters.CHARACTER_FACTORY); factories.add(TypeAdapters.STRING_BUILDER_FACTORY); factories.add(TypeAdapters.STRING_BUFFER_FACTORY); - factories.add(TypeAdapters.newFactory(BigDecimal.class, TypeAdapters.BIG_DECIMAL)); - factories.add(TypeAdapters.newFactory(BigInteger.class, TypeAdapters.BIG_INTEGER)); factories.add(TypeAdapters.JSON_ELEMENT_FACTORY); factories.add(ObjectTypeAdapter.FACTORY); @@ -220,6 +218,8 @@ public final class Gson { factories.addAll(typeAdapterFactories); // built-in type adapters that can be overridden + factories.add(TypeAdapters.newFactory(BigDecimal.class, TypeAdapters.BIG_DECIMAL)); + factories.add(TypeAdapters.newFactory(BigInteger.class, TypeAdapters.BIG_INTEGER)); factories.add(new CollectionTypeAdapterFactory(constructorConstructor)); factories.add(TypeAdapters.URL_FACTORY); factories.add(TypeAdapters.URI_FACTORY); diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index d7d3380a..2321f443 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -16,6 +16,7 @@ package com.google.gson.functional; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; @@ -266,6 +267,22 @@ public class DefaultTypeAdaptersTest extends TestCase { ClassWithBigInteger actual = gson.fromJson(json, ClassWithBigInteger.class); assertEquals(expected.value, actual.value); } + + public void testOverrideBigIntegerTypeAdapter() throws Exception { + gson = new GsonBuilder() + .registerTypeAdapter(BigInteger.class, new NumberAsStringAdapter(BigInteger.class)) + .create(); + assertEquals("\"123\"", gson.toJson(new BigInteger("123"), BigInteger.class)); + assertEquals(new BigInteger("123"), gson.fromJson("\"123\"", BigInteger.class)); + } + + public void testOverrideBigDecimalTypeAdapter() throws Exception { + gson = new GsonBuilder() + .registerTypeAdapter(BigDecimal.class, new NumberAsStringAdapter(BigDecimal.class)) + .create(); + assertEquals("\"1.1\"", gson.toJson(new BigDecimal("1.1"), BigDecimal.class)); + assertEquals(new BigDecimal("1.1"), gson.fromJson("\"1.1\"", BigDecimal.class)); + } public void testSetSerialization() throws Exception { Gson gson = new Gson(); @@ -674,4 +691,21 @@ public class DefaultTypeAdaptersTest extends TestCase { } } } + + static class NumberAsStringAdapter extends TypeAdapter { + private final Constructor constructor; + NumberAsStringAdapter(Class type) throws Exception { + this.constructor = type.getConstructor(String.class); + } + @Override public void write(JsonWriter out, Number value) throws IOException { + out.value(value.toString()); + } + @Override public Number read(JsonReader in) throws IOException { + try { + return constructor.newInstance(in.nextString()); + } catch (Exception e) { + throw new AssertionError(e); + } + } + } }