From 70965eae0397636368dbfcc606a1319722c4a3dd Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Fri, 12 Aug 2011 02:20:48 +0000 Subject: [PATCH] Adding new type adapters for BigInteger and BigDecimal types. --- gson/src/main/java/com/google/gson/Gson.java | 7 ++++ .../internal/bind/BigDecimalTypeAdapter.java | 41 +++++++++++++++++++ .../internal/bind/BigIntegerTypeAdapter.java | 41 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 gson/src/main/java/com/google/gson/internal/bind/BigDecimalTypeAdapter.java create mode 100644 gson/src/main/java/com/google/gson/internal/bind/BigIntegerTypeAdapter.java diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index b902d199..6399077e 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -18,6 +18,8 @@ package com.google.gson; import com.google.gson.internal.Streams; import com.google.gson.internal.bind.ArrayTypeAdapter; +import com.google.gson.internal.bind.BigDecimalTypeAdapter; +import com.google.gson.internal.bind.BigIntegerTypeAdapter; import com.google.gson.internal.bind.CollectionTypeAdapter; import com.google.gson.internal.bind.MiniGson; import com.google.gson.internal.bind.ReflectiveTypeAdapter; @@ -29,6 +31,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import com.google.gson.stream.MalformedJsonException; + import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -37,6 +40,8 @@ import java.io.Writer; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -231,6 +236,8 @@ public final class Gson { .factory(TypeAdapters.newFactory(long.class, Long.class, longAdapter(longSerializationPolicy))) .factory(TypeAdapters.STRING_FACTORY) + .typeAdapter(BigDecimal.class, new BigDecimalTypeAdapter()) + .typeAdapter(BigInteger.class, new BigIntegerTypeAdapter()) .factory(excludedTypeFactory) .factory(new GsonToMiniGsonTypeAdapter(serializers, deserializers, serializeNulls)) .factory(CollectionTypeAdapter.FACTORY) diff --git a/gson/src/main/java/com/google/gson/internal/bind/BigDecimalTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/BigDecimalTypeAdapter.java new file mode 100644 index 00000000..1d3d8301 --- /dev/null +++ b/gson/src/main/java/com/google/gson/internal/bind/BigDecimalTypeAdapter.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.internal.bind; + +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.math.BigDecimal; + +/** + * Adapts a BigDecimal type to and from its JSON representation. + * + * @author Joel Leitch + */ +public class BigDecimalTypeAdapter extends TypeAdapter { + + @Override + public BigDecimal read(JsonReader reader) throws IOException { + return new BigDecimal(reader.nextString()); + } + + @Override + public void write(JsonWriter writer, BigDecimal value) throws IOException { + writer.value(value); + } +} diff --git a/gson/src/main/java/com/google/gson/internal/bind/BigIntegerTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/BigIntegerTypeAdapter.java new file mode 100644 index 00000000..968cb8f6 --- /dev/null +++ b/gson/src/main/java/com/google/gson/internal/bind/BigIntegerTypeAdapter.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.internal.bind; + +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.math.BigInteger; + +/** + * Adapts a BigInteger type to and from its JSON representation. + * + * @author Joel Leitch + */ +public class BigIntegerTypeAdapter extends TypeAdapter { + + @Override + public BigInteger read(JsonReader reader) throws IOException { + return new BigInteger(reader.nextString()); + } + + @Override + public void write(JsonWriter writer, BigInteger value) throws IOException { + writer.value(value); + } +}