2008-09-01 05:13:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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;
|
|
|
|
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.ConstructorConstructor;
|
2011-11-22 08:37:13 +01:00
|
|
|
import com.google.gson.internal.Excluder;
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.Primitives;
|
2011-08-03 04:40:18 +02:00
|
|
|
import com.google.gson.internal.Streams;
|
2011-08-03 03:19:26 +02:00
|
|
|
import com.google.gson.internal.bind.ArrayTypeAdapter;
|
2011-08-12 04:20:48 +02:00
|
|
|
import com.google.gson.internal.bind.BigDecimalTypeAdapter;
|
|
|
|
import com.google.gson.internal.bind.BigIntegerTypeAdapter;
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.bind.CollectionTypeAdapterFactory;
|
2011-09-26 23:44:08 +02:00
|
|
|
import com.google.gson.internal.bind.DateTypeAdapter;
|
2011-09-30 09:24:07 +02:00
|
|
|
import com.google.gson.internal.bind.JsonElementReader;
|
2011-10-23 22:28:04 +02:00
|
|
|
import com.google.gson.internal.bind.JsonElementWriter;
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.bind.MapTypeAdapterFactory;
|
2011-09-09 05:31:16 +02:00
|
|
|
import com.google.gson.internal.bind.ObjectTypeAdapter;
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
|
2011-09-28 19:56:54 +02:00
|
|
|
import com.google.gson.internal.bind.SqlDateTypeAdapter;
|
2011-09-26 23:44:08 +02:00
|
|
|
import com.google.gson.internal.bind.TimeTypeAdapter;
|
2011-08-03 03:19:26 +02:00
|
|
|
import com.google.gson.internal.bind.TypeAdapters;
|
2011-08-03 02:47:36 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonToken;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
import com.google.gson.stream.MalformedJsonException;
|
2011-09-26 23:44:08 +02:00
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigInteger;
|
2011-11-20 16:23:08 +01:00
|
|
|
import java.util.ArrayList;
|
2011-09-28 21:14:46 +02:00
|
|
|
import java.util.Collections;
|
2011-11-20 16:23:08 +01:00
|
|
|
import java.util.HashMap;
|
2011-09-26 23:44:08 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2011-08-03 02:47:36 +02:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* This is the main class for using Gson. Gson is typically used by first constructing a
|
|
|
|
* Gson instance and then invoking {@link #toJson(Object)} or {@link #fromJson(String, Class)}
|
|
|
|
* methods on it.
|
|
|
|
*
|
|
|
|
* <p>You can create a Gson instance by invoking {@code new Gson()} if the default configuration
|
|
|
|
* is all you need. You can also use {@link GsonBuilder} to build a Gson instance with various
|
|
|
|
* configuration options such as versioning support, pretty printing, custom
|
|
|
|
* {@link JsonSerializer}s, {@link JsonDeserializer}s, and {@link InstanceCreator}s.</p>
|
|
|
|
*
|
|
|
|
* <p>Here is an example of how Gson is used for a simple Class:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* Gson gson = new Gson(); // Or use new GsonBuilder().create();
|
|
|
|
* MyType target = new MyType();
|
|
|
|
* String json = gson.toJson(target); // serializes target to Json
|
|
|
|
* MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
|
|
|
|
* </pre></p>
|
|
|
|
*
|
|
|
|
* <p>If the object that your are serializing/deserializing is a {@code ParameterizedType}
|
|
|
|
* (i.e. contains at least one type parameter and may be an array) then you must use the
|
|
|
|
* {@link #toJson(Object, Type)} or {@link #fromJson(String, Type)} method. Here is an
|
|
|
|
* example for serializing and deserialing a {@code ParameterizedType}:
|
|
|
|
*
|
|
|
|
* <pre>
|
2009-09-22 21:10:53 +02:00
|
|
|
* Type listType = new TypeToken<List<String>>() {}.getType();
|
|
|
|
* List<String> target = new LinkedList<String>();
|
2008-09-01 05:13:32 +02:00
|
|
|
* target.add("blah");
|
|
|
|
*
|
|
|
|
* Gson gson = new Gson();
|
|
|
|
* String json = gson.toJson(target, listType);
|
2009-09-22 21:10:53 +02:00
|
|
|
* List<String> target2 = gson.fromJson(json, listType);
|
2008-09-01 05:13:32 +02:00
|
|
|
* </pre></p>
|
|
|
|
*
|
|
|
|
* <p>See the <a href="https://sites.google.com/site/gson/gson-user-guide">Gson User Guide</a>
|
|
|
|
* for a more complete set of examples.</p>
|
|
|
|
*
|
|
|
|
* @see com.google.gson.reflect.TypeToken
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public final class Gson {
|
2011-11-21 06:42:30 +01:00
|
|
|
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
|
2009-10-07 11:23:14 +02:00
|
|
|
|
2009-03-20 19:02:35 +01:00
|
|
|
private static final String JSON_NON_EXECUTABLE_PREFIX = ")]}'\n";
|
2009-03-17 22:15:10 +01:00
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
/**
|
|
|
|
* This thread local guards against reentrant calls to getAdapter(). In
|
|
|
|
* certain object graphs, creating an adapter for a type may recursively
|
|
|
|
* require an adapter for the same type! Without intervention, the recursive
|
|
|
|
* lookup would stack overflow. We cheat by returning a proxy type adapter.
|
|
|
|
* The proxy is wired up once the initial adapter has been created.
|
|
|
|
*/
|
|
|
|
private final ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>> calls
|
|
|
|
= new ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>>() {
|
|
|
|
@Override protected Map<TypeToken<?>, FutureTypeAdapter<?>> initialValue() {
|
|
|
|
return new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private final List<TypeAdapter.Factory> factories;
|
2011-09-11 09:04:56 +02:00
|
|
|
private final ConstructorConstructor constructorConstructor;
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
private final boolean serializeNulls;
|
2010-09-02 02:15:23 +02:00
|
|
|
private final boolean htmlSafe;
|
2009-03-17 22:15:10 +01:00
|
|
|
private final boolean generateNonExecutableJson;
|
2010-11-02 00:03:41 +01:00
|
|
|
private final boolean prettyPrinting;
|
2009-03-17 22:15:10 +01:00
|
|
|
|
2011-11-20 20:55:01 +01:00
|
|
|
final JsonDeserializationContext deserializationContext = new JsonDeserializationContext() {
|
|
|
|
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
|
|
|
|
return (T) fromJson(json, typeOfT);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
final JsonSerializationContext serializationContext = new JsonSerializationContext() {
|
|
|
|
public JsonElement serialize(Object src) {
|
|
|
|
return toJsonTree(src);
|
|
|
|
}
|
|
|
|
public JsonElement serialize(Object src, Type typeOfSrc) {
|
|
|
|
return toJsonTree(src, typeOfSrc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* Constructs a Gson object with default configuration. The default configuration has the
|
|
|
|
* following settings:
|
|
|
|
* <ul>
|
|
|
|
* <li>The JSON generated by <code>toJson</code> methods is in compact representation. This
|
|
|
|
* means that all the unneeded white-space is removed. You can change this behavior with
|
|
|
|
* {@link GsonBuilder#setPrettyPrinting()}. </li>
|
|
|
|
* <li>The generated JSON omits all the fields that are null. Note that nulls in arrays are
|
|
|
|
* kept as is since an array is an ordered list. Moreover, if a field is not null, but its
|
|
|
|
* generated JSON is empty, the field is kept. You can configure Gson to serialize null values
|
|
|
|
* by setting {@link GsonBuilder#serializeNulls()}.</li>
|
|
|
|
* <li>Gson provides default serialization and deserialization for Enums, {@link Map},
|
|
|
|
* {@link java.net.URL}, {@link java.net.URI}, {@link java.util.Locale}, {@link java.util.Date},
|
|
|
|
* {@link java.math.BigDecimal}, and {@link java.math.BigInteger} classes. If you would prefer
|
|
|
|
* to change the default representation, you can do so by registering a type adapter through
|
|
|
|
* {@link GsonBuilder#registerTypeAdapter(Type, Object)}. </li>
|
2009-10-07 11:23:14 +02:00
|
|
|
* <li>The default Date format is same as {@link java.text.DateFormat#DEFAULT}. This format
|
2008-10-13 20:40:20 +02:00
|
|
|
* ignores the millisecond portion of the date during serialization. You can change
|
2008-09-01 05:13:32 +02:00
|
|
|
* this by invoking {@link GsonBuilder#setDateFormat(int)} or
|
|
|
|
* {@link GsonBuilder#setDateFormat(String)}. </li>
|
|
|
|
* <li>By default, Gson ignores the {@link com.google.gson.annotations.Expose} annotation.
|
|
|
|
* You can enable Gson to serialize/deserialize only those fields marked with this annotation
|
|
|
|
* through {@link GsonBuilder#excludeFieldsWithoutExposeAnnotation()}. </li>
|
|
|
|
* <li>By default, Gson ignores the {@link com.google.gson.annotations.Since} annotation. You
|
|
|
|
* can enable Gson to use this annotation through {@link GsonBuilder#setVersion(double)}.</li>
|
|
|
|
* <li>The default field naming policy for the output Json is same as in Java. So, a Java class
|
|
|
|
* field <code>versionNumber</code> will be output as <code>"versionNumber@quot;</code> in
|
|
|
|
* Json. The same rules are applied for mapping incoming Json to the Java classes. You can
|
|
|
|
* change this policy through {@link GsonBuilder#setFieldNamingPolicy(FieldNamingPolicy)}.</li>
|
|
|
|
* <li>By default, Gson excludes <code>transient</code> or <code>static</code> fields from
|
|
|
|
* consideration for serialization and deserialization. You can change this behavior through
|
|
|
|
* {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.</li>
|
|
|
|
* </ul>
|
|
|
|
*/
|
2011-09-30 19:56:40 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2008-09-01 05:13:32 +02:00
|
|
|
public Gson() {
|
2011-11-22 08:37:13 +01:00
|
|
|
this(Excluder.DEFAULT, FieldNamingPolicy.IDENTITY,
|
2011-11-23 07:16:55 +01:00
|
|
|
Collections.<Type, InstanceCreator<?>>emptyMap(), false, false, DEFAULT_JSON_NON_EXECUTABLE,
|
|
|
|
true, false, false, LongSerializationPolicy.DEFAULT,
|
2011-09-28 21:14:46 +02:00
|
|
|
Collections.<TypeAdapter.Factory>emptyList());
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2011-11-22 08:37:13 +01:00
|
|
|
Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingPolicy,
|
2011-11-23 07:16:55 +01:00
|
|
|
final Map<Type, InstanceCreator<?>> instanceCreators, boolean serializeNulls,
|
2011-09-12 07:51:17 +02:00
|
|
|
boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe,
|
|
|
|
boolean prettyPrinting, boolean serializeSpecialFloatingPointValues,
|
2011-09-28 21:14:46 +02:00
|
|
|
LongSerializationPolicy longSerializationPolicy,
|
|
|
|
List<TypeAdapter.Factory> typeAdapterFactories) {
|
2011-09-11 09:04:56 +02:00
|
|
|
this.constructorConstructor = new ConstructorConstructor(instanceCreators);
|
2008-09-01 05:13:32 +02:00
|
|
|
this.serializeNulls = serializeNulls;
|
2009-03-17 22:15:10 +01:00
|
|
|
this.generateNonExecutableJson = generateNonExecutableGson;
|
2010-09-02 02:15:23 +02:00
|
|
|
this.htmlSafe = htmlSafe;
|
2010-11-02 00:03:41 +01:00
|
|
|
this.prettyPrinting = prettyPrinting;
|
2011-08-03 02:28:02 +02:00
|
|
|
|
2011-11-21 06:42:30 +01:00
|
|
|
TypeAdapter.Factory reflectiveTypeAdapterFactory = new ReflectiveTypeAdapterFactory(
|
2011-11-22 08:37:13 +01:00
|
|
|
constructorConstructor, fieldNamingPolicy, excluder);
|
2011-08-05 02:25:49 +02:00
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
ConstructorConstructor constructorConstructor = new ConstructorConstructor();
|
|
|
|
List<TypeAdapter.Factory> factories = new ArrayList<TypeAdapter.Factory>();
|
2011-11-23 14:39:06 +01:00
|
|
|
|
|
|
|
// built-in type adapters that cannot be overridden
|
2011-11-20 16:23:08 +01:00
|
|
|
factories.add(TypeAdapters.STRING_FACTORY);
|
|
|
|
factories.add(TypeAdapters.INTEGER_FACTORY);
|
|
|
|
factories.add(TypeAdapters.BOOLEAN_FACTORY);
|
|
|
|
factories.add(TypeAdapters.BYTE_FACTORY);
|
|
|
|
factories.add(TypeAdapters.SHORT_FACTORY);
|
|
|
|
factories.add(TypeAdapters.newFactory(long.class, Long.class,
|
|
|
|
longAdapter(longSerializationPolicy)));
|
|
|
|
factories.add(TypeAdapters.newFactory(double.class, Double.class,
|
|
|
|
doubleAdapter(serializeSpecialFloatingPointValues)));
|
|
|
|
factories.add(TypeAdapters.newFactory(float.class, Float.class,
|
|
|
|
floatAdapter(serializeSpecialFloatingPointValues)));
|
2011-11-22 08:37:13 +01:00
|
|
|
factories.add(excluder);
|
2011-11-20 16:23:08 +01:00
|
|
|
factories.add(TypeAdapters.NUMBER_FACTORY);
|
|
|
|
factories.add(TypeAdapters.CHARACTER_FACTORY);
|
|
|
|
factories.add(TypeAdapters.STRING_BUILDER_FACTORY);
|
|
|
|
factories.add(TypeAdapters.STRING_BUFFER_FACTORY);
|
|
|
|
factories.add(TypeAdapters.newFactory(BigDecimal.class, new BigDecimalTypeAdapter()));
|
|
|
|
factories.add(TypeAdapters.newFactory(BigInteger.class, new BigIntegerTypeAdapter()));
|
|
|
|
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
|
|
|
|
factories.add(ObjectTypeAdapter.FACTORY);
|
2011-09-28 21:14:46 +02:00
|
|
|
|
2011-11-23 14:39:06 +01:00
|
|
|
// user's type adapters
|
|
|
|
factories.addAll(typeAdapterFactories);
|
2011-09-28 21:14:46 +02:00
|
|
|
|
2011-11-23 14:39:06 +01:00
|
|
|
// built-in type adapters that can be overridden
|
2011-11-20 16:23:08 +01:00
|
|
|
factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
|
|
|
|
factories.add(TypeAdapters.URL_FACTORY);
|
|
|
|
factories.add(TypeAdapters.URI_FACTORY);
|
|
|
|
factories.add(TypeAdapters.UUID_FACTORY);
|
|
|
|
factories.add(TypeAdapters.LOCALE_FACTORY);
|
|
|
|
factories.add(TypeAdapters.INET_ADDRESS_FACTORY);
|
|
|
|
factories.add(TypeAdapters.BIT_SET_FACTORY);
|
|
|
|
factories.add(DateTypeAdapter.FACTORY);
|
|
|
|
factories.add(TypeAdapters.CALENDAR_FACTORY);
|
|
|
|
factories.add(TimeTypeAdapter.FACTORY);
|
|
|
|
factories.add(SqlDateTypeAdapter.FACTORY);
|
|
|
|
factories.add(TypeAdapters.TIMESTAMP_FACTORY);
|
|
|
|
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
|
|
|
|
factories.add(ArrayTypeAdapter.FACTORY);
|
|
|
|
factories.add(TypeAdapters.ENUM_FACTORY);
|
|
|
|
factories.add(reflectiveTypeAdapterFactory);
|
|
|
|
|
|
|
|
this.factories = Collections.unmodifiableList(factories);
|
2011-08-05 00:57:36 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 06:39:29 +02:00
|
|
|
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
|
2011-08-05 01:12:49 +02:00
|
|
|
if (serializeSpecialFloatingPointValues) {
|
|
|
|
return TypeAdapters.DOUBLE;
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
return new TypeAdapter<Number>() {
|
2011-08-05 01:12:49 +02:00
|
|
|
@Override public Double read(JsonReader reader) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (reader.peek() == JsonToken.NULL) {
|
2011-10-14 05:20:05 +02:00
|
|
|
reader.nextNull();
|
2011-09-11 09:04:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2011-08-05 01:12:49 +02:00
|
|
|
return reader.nextDouble();
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
@Override public void write(JsonWriter writer, Number value) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (value == null) {
|
2011-11-23 14:39:06 +01:00
|
|
|
writer.nullValue();
|
2011-09-11 09:04:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
double doubleValue = value.doubleValue();
|
|
|
|
checkValidFloatingPoint(doubleValue);
|
2011-08-05 01:12:49 +02:00
|
|
|
writer.value(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:39:29 +02:00
|
|
|
private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointValues) {
|
2011-08-05 01:12:49 +02:00
|
|
|
if (serializeSpecialFloatingPointValues) {
|
|
|
|
return TypeAdapters.FLOAT;
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
return new TypeAdapter<Number>() {
|
2011-08-05 01:12:49 +02:00
|
|
|
@Override public Float read(JsonReader reader) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (reader.peek() == JsonToken.NULL) {
|
2011-10-14 05:20:05 +02:00
|
|
|
reader.nextNull();
|
2011-09-11 09:04:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2011-08-05 01:12:49 +02:00
|
|
|
return (float) reader.nextDouble();
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
@Override public void write(JsonWriter writer, Number value) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (value == null) {
|
2011-11-23 14:39:06 +01:00
|
|
|
writer.nullValue();
|
2011-09-11 09:04:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
float floatValue = value.floatValue();
|
|
|
|
checkValidFloatingPoint(floatValue);
|
2011-08-05 01:12:49 +02:00
|
|
|
writer.value(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkValidFloatingPoint(double value) {
|
|
|
|
if (Double.isNaN(value) || Double.isInfinite(value)) {
|
|
|
|
throw new IllegalArgumentException(value
|
|
|
|
+ " is not a valid double value as per JSON specification. To override this"
|
|
|
|
+ " behavior, use GsonBuilder.serializeSpecialDoubleValues() method.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:39:29 +02:00
|
|
|
private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
|
2011-08-05 00:57:36 +02:00
|
|
|
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
|
|
|
|
return TypeAdapters.LONG;
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
return new TypeAdapter<Number>() {
|
|
|
|
@Override public Number read(JsonReader reader) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (reader.peek() == JsonToken.NULL) {
|
2011-10-14 05:20:05 +02:00
|
|
|
reader.nextNull();
|
2011-09-11 09:04:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2011-08-05 00:57:36 +02:00
|
|
|
return reader.nextLong();
|
|
|
|
}
|
2011-09-09 06:39:29 +02:00
|
|
|
@Override public void write(JsonWriter writer, Number value) throws IOException {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (value == null) {
|
2011-11-23 14:39:06 +01:00
|
|
|
writer.nullValue();
|
2011-09-11 09:04:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-05 00:57:36 +02:00
|
|
|
writer.value(value.toString());
|
|
|
|
}
|
|
|
|
};
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
2009-10-07 11:23:14 +02:00
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the type adapter for {@code} type.
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if this GSON cannot serialize and
|
|
|
|
* deserialize {@code type}.
|
|
|
|
*/
|
|
|
|
public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
|
|
|
|
// TODO: cache?
|
|
|
|
|
|
|
|
Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
|
|
|
|
@SuppressWarnings("unchecked") // the key and value type parameters always agree
|
|
|
|
FutureTypeAdapter<T> ongoingCall = (FutureTypeAdapter<T>) threadCalls.get(type);
|
|
|
|
if (ongoingCall != null) {
|
|
|
|
return ongoingCall;
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
|
|
|
|
threadCalls.put(type, call);
|
|
|
|
try {
|
|
|
|
for (TypeAdapter.Factory factory : factories) {
|
|
|
|
TypeAdapter<T> candidate = factory.create(this, type);
|
|
|
|
if (candidate != null) {
|
|
|
|
call.setDelegate(candidate);
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("GSON cannot handle " + type);
|
|
|
|
} finally {
|
|
|
|
threadCalls.remove(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a type adapter for {@code} type that isn't {@code skipPast}. This
|
|
|
|
* can be used for type adapters to compose other, simpler type adapters.
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if this GSON cannot serialize and
|
|
|
|
* deserialize {@code type}.
|
|
|
|
*/
|
|
|
|
public <T> TypeAdapter<T> getNextAdapter(TypeAdapter.Factory skipPast, TypeToken<T> type) {
|
|
|
|
boolean skipPastFound = false;
|
|
|
|
|
|
|
|
for (TypeAdapter.Factory factory : factories) {
|
|
|
|
if (!skipPastFound) {
|
|
|
|
if (factory == skipPast) {
|
|
|
|
skipPastFound = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeAdapter<T> candidate = factory.create(this, type);
|
|
|
|
if (candidate != null) {
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("GSON cannot serialize " + type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type adapter for {@code} type.
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if this GSON cannot serialize and
|
|
|
|
* deserialize {@code type}.
|
|
|
|
*/
|
|
|
|
public <T> TypeAdapter<T> getAdapter(Class<T> type) {
|
|
|
|
return getAdapter(TypeToken.get(type));
|
|
|
|
}
|
|
|
|
|
2009-04-03 23:33:01 +02:00
|
|
|
/**
|
2009-10-07 11:23:14 +02:00
|
|
|
* This method serializes the specified object into its equivalent representation as a tree of
|
2009-10-05 20:17:52 +02:00
|
|
|
* {@link JsonElement}s. This method should be used when the specified object is not a generic
|
|
|
|
* type. This method uses {@link Class#getClass()} to get the type for the specified object, but
|
|
|
|
* the {@code getClass()} loses the generic type information because of the Type Erasure feature
|
2009-04-03 23:33:01 +02:00
|
|
|
* of Java. Note that this method works fine if the any of the object fields are of generic type,
|
|
|
|
* just the object itself should not be of a generic type. If the object is of generic type, use
|
2009-10-05 20:17:52 +02:00
|
|
|
* {@link #toJsonTree(Object, Type)} instead.
|
2009-04-03 23:33:01 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
* @return Json representation of {@code src}.
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public JsonElement toJsonTree(Object src) {
|
|
|
|
if (src == null) {
|
2011-05-25 18:55:57 +02:00
|
|
|
return JsonNull.INSTANCE;
|
2009-04-03 23:33:01 +02:00
|
|
|
}
|
2009-10-09 03:35:45 +02:00
|
|
|
return toJsonTree(src, src.getClass());
|
2009-04-03 23:33:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method serializes the specified object, including those of generic types, into its
|
2009-10-07 11:23:14 +02:00
|
|
|
* equivalent representation as a tree of {@link JsonElement}s. This method must be used if the
|
|
|
|
* specified object is a generic type. For non-generic objects, use {@link #toJsonTree(Object)}
|
|
|
|
* instead.
|
2009-04-03 23:33:01 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
* @param typeOfSrc The specific genericized type of src. You can obtain
|
|
|
|
* this type by using the {@link com.google.gson.reflect.TypeToken} class. For example,
|
|
|
|
* to get the type for {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @return Json representation of {@code src}
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
2011-10-20 23:32:46 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"}) // the caller is required to make src and typeOfSrc consistent
|
2009-04-03 23:33:01 +02:00
|
|
|
public JsonElement toJsonTree(Object src, Type typeOfSrc) {
|
2011-10-23 22:28:04 +02:00
|
|
|
JsonElementWriter writer = new JsonElementWriter();
|
|
|
|
toJson(src, typeOfSrc, writer);
|
|
|
|
return writer.get();
|
2009-04-03 23:33:01 +02:00
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* This method serializes the specified object into its equivalent Json representation.
|
|
|
|
* This method should be used when the specified object is not a generic type. This method uses
|
|
|
|
* {@link Class#getClass()} to get the type for the specified object, but the
|
|
|
|
* {@code getClass()} loses the generic type information because of the Type Erasure feature
|
|
|
|
* of Java. Note that this method works fine if the any of the object fields are of generic type,
|
|
|
|
* just the object itself should not be of a generic type. If the object is of generic type, use
|
|
|
|
* {@link #toJson(Object, Type)} instead. If you want to write out the object to a
|
2008-11-26 20:13:14 +01:00
|
|
|
* {@link Writer}, use {@link #toJson(Object, Appendable)} instead.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
* @return Json representation of {@code src}.
|
|
|
|
*/
|
|
|
|
public String toJson(Object src) {
|
|
|
|
if (src == null) {
|
2011-05-25 18:55:57 +02:00
|
|
|
return toJson(JsonNull.INSTANCE);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
2009-10-09 03:35:45 +02:00
|
|
|
return toJson(src, src.getClass());
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method serializes the specified object, including those of generic types, into its
|
|
|
|
* equivalent Json representation. This method must be used if the specified object is a generic
|
|
|
|
* type. For non-generic objects, use {@link #toJson(Object)} instead. If you want to write out
|
2008-11-26 20:13:14 +01:00
|
|
|
* the object to a {@link Appendable}, use {@link #toJson(Object, Type, Appendable)} instead.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
* @param typeOfSrc The specific genericized type of src. You can obtain
|
|
|
|
* this type by using the {@link com.google.gson.reflect.TypeToken} class. For example,
|
|
|
|
* to get the type for {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @return Json representation of {@code src}
|
|
|
|
*/
|
|
|
|
public String toJson(Object src, Type typeOfSrc) {
|
|
|
|
StringWriter writer = new StringWriter();
|
2011-09-30 09:08:44 +02:00
|
|
|
toJson(src, typeOfSrc, writer);
|
2008-09-01 05:13:32 +02:00
|
|
|
return writer.toString();
|
|
|
|
}
|
2009-08-07 22:58:41 +02:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* This method serializes the specified object into its equivalent Json representation.
|
|
|
|
* This method should be used when the specified object is not a generic type. This method uses
|
|
|
|
* {@link Class#getClass()} to get the type for the specified object, but the
|
|
|
|
* {@code getClass()} loses the generic type information because of the Type Erasure feature
|
|
|
|
* of Java. Note that this method works fine if the any of the object fields are of generic type,
|
|
|
|
* just the object itself should not be of a generic type. If the object is of generic type, use
|
2008-11-26 20:13:14 +01:00
|
|
|
* {@link #toJson(Object, Type, Appendable)} instead.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
* @param writer Writer to which the Json representation needs to be written
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
2008-09-01 05:13:32 +02:00
|
|
|
* @since 1.2
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public void toJson(Object src, Appendable writer) throws JsonIOException {
|
2011-01-22 23:43:43 +01:00
|
|
|
if (src != null) {
|
|
|
|
toJson(src, src.getClass(), writer);
|
|
|
|
} else {
|
2011-05-25 18:55:57 +02:00
|
|
|
toJson(JsonNull.INSTANCE, writer);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method serializes the specified object, including those of generic types, into its
|
|
|
|
* equivalent Json representation. This method must be used if the specified object is a generic
|
2008-11-26 20:13:14 +01:00
|
|
|
* type. For non-generic objects, use {@link #toJson(Object, Appendable)} instead.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
* @param typeOfSrc The specific genericized type of src. You can obtain
|
|
|
|
* this type by using the {@link com.google.gson.reflect.TypeToken} class. For example,
|
|
|
|
* to get the type for {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @param writer Writer to which the Json representation of src needs to be written.
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
2008-09-01 05:13:32 +02:00
|
|
|
* @since 1.2
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOException {
|
2011-09-30 09:08:44 +02:00
|
|
|
try {
|
|
|
|
JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
|
|
|
|
toJson(src, typeOfSrc, jsonWriter);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new JsonIOException(e);
|
|
|
|
}
|
2009-08-07 22:58:41 +02:00
|
|
|
}
|
|
|
|
|
2010-09-02 02:15:23 +02:00
|
|
|
/**
|
|
|
|
* Writes the JSON representation of {@code src} of type {@code typeOfSrc} to
|
|
|
|
* {@code writer}.
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
2010-09-02 02:15:23 +02:00
|
|
|
*/
|
2011-09-30 19:08:35 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-03 20:46:29 +01:00
|
|
|
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException {
|
2011-11-20 16:23:08 +01:00
|
|
|
TypeAdapter<?> adapter = getAdapter(TypeToken.get(typeOfSrc));
|
2011-09-30 09:08:44 +02:00
|
|
|
boolean oldLenient = writer.isLenient();
|
|
|
|
writer.setLenient(true);
|
|
|
|
boolean oldHtmlSafe = writer.isHtmlSafe();
|
|
|
|
writer.setHtmlSafe(htmlSafe);
|
|
|
|
boolean oldSerializeNulls = writer.getSerializeNulls();
|
|
|
|
writer.setSerializeNulls(serializeNulls);
|
|
|
|
try {
|
|
|
|
((TypeAdapter<Object>) adapter).write(writer, src);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new JsonIOException(e);
|
|
|
|
} finally {
|
|
|
|
writer.setLenient(oldLenient);
|
|
|
|
writer.setHtmlSafe(oldHtmlSafe);
|
|
|
|
writer.setSerializeNulls(oldSerializeNulls);
|
|
|
|
}
|
2010-09-02 02:15:23 +02:00
|
|
|
}
|
|
|
|
|
2009-08-07 22:58:41 +02:00
|
|
|
/**
|
|
|
|
* Converts a tree of {@link JsonElement}s into its equivalent JSON representation.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
2009-10-05 20:17:52 +02:00
|
|
|
* @param jsonElement root of a tree of {@link JsonElement}s
|
2009-08-07 22:58:41 +02:00
|
|
|
* @return JSON String representation of the tree
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
|
|
|
public String toJson(JsonElement jsonElement) {
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
toJson(jsonElement, writer);
|
|
|
|
return writer.toString();
|
|
|
|
}
|
2009-10-07 11:23:14 +02:00
|
|
|
|
2009-08-07 22:58:41 +02:00
|
|
|
/**
|
2009-10-05 20:17:52 +02:00
|
|
|
* Writes out the equivalent JSON for a tree of {@link JsonElement}s.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
2009-10-05 20:17:52 +02:00
|
|
|
* @param jsonElement root of a tree of {@link JsonElement}s
|
2009-08-07 22:58:41 +02:00
|
|
|
* @param writer Writer to which the Json representation needs to be written
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
2009-08-07 22:58:41 +02:00
|
|
|
* @since 1.4
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException {
|
2008-11-26 20:13:14 +01:00
|
|
|
try {
|
2011-09-30 09:08:44 +02:00
|
|
|
JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
|
2010-11-02 00:03:41 +01:00
|
|
|
toJson(jsonElement, jsonWriter);
|
2009-08-07 22:58:41 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2011-09-30 09:08:44 +02:00
|
|
|
/**
|
|
|
|
* Returns a new JSON writer configured for this GSON and with the non-execute
|
|
|
|
* prefix if that is configured.
|
|
|
|
*/
|
|
|
|
private JsonWriter newJsonWriter(Writer writer) throws IOException {
|
|
|
|
if (generateNonExecutableJson) {
|
|
|
|
writer.write(JSON_NON_EXECUTABLE_PREFIX);
|
|
|
|
}
|
|
|
|
JsonWriter jsonWriter = new JsonWriter(writer);
|
|
|
|
if (prettyPrinting) {
|
|
|
|
jsonWriter.setIndent(" ");
|
|
|
|
}
|
|
|
|
jsonWriter.setSerializeNulls(serializeNulls);
|
|
|
|
return jsonWriter;
|
|
|
|
}
|
|
|
|
|
2010-09-02 02:15:23 +02:00
|
|
|
/**
|
|
|
|
* Writes the JSON for {@code jsonElement} to {@code writer}.
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
2010-09-02 02:15:23 +02:00
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException {
|
2010-09-02 02:15:23 +02:00
|
|
|
boolean oldLenient = writer.isLenient();
|
|
|
|
writer.setLenient(true);
|
|
|
|
boolean oldHtmlSafe = writer.isHtmlSafe();
|
|
|
|
writer.setHtmlSafe(htmlSafe);
|
2011-09-30 09:08:44 +02:00
|
|
|
boolean oldSerializeNulls = writer.getSerializeNulls();
|
|
|
|
writer.setSerializeNulls(serializeNulls);
|
2010-09-02 02:15:23 +02:00
|
|
|
try {
|
2011-10-01 03:10:11 +02:00
|
|
|
Streams.write(jsonElement, writer);
|
2010-09-02 02:15:23 +02:00
|
|
|
} catch (IOException e) {
|
2010-10-19 01:58:09 +02:00
|
|
|
throw new JsonIOException(e);
|
2010-09-02 02:15:23 +02:00
|
|
|
} finally {
|
|
|
|
writer.setLenient(oldLenient);
|
|
|
|
writer.setHtmlSafe(oldHtmlSafe);
|
2011-09-30 09:08:44 +02:00
|
|
|
writer.setSerializeNulls(oldSerializeNulls);
|
2010-09-02 02:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* This method deserializes the specified Json into an object of the specified class. It is not
|
|
|
|
* suitable to use if the specified class is a generic type since it will not have the generic
|
|
|
|
* type information because of the Type Erasure feature of Java. Therefore, this method should not
|
|
|
|
* be used if the desired type is a generic type. Note that this method works fine if the any of
|
|
|
|
* the fields of the specified object are generics, just the object itself should not be a
|
|
|
|
* generic type. For the cases when the object is of generic type, invoke
|
|
|
|
* {@link #fromJson(String, Type)}. If you have the Json in a {@link Reader} instead of
|
|
|
|
* a String, use {@link #fromJson(Reader, Class)} instead.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
* @param json the string from which the object is to be deserialized
|
|
|
|
* @param classOfT the class of T
|
|
|
|
* @return an object of type T from the string
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
2008-09-01 05:13:32 +02:00
|
|
|
* classOfT
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
|
2010-11-01 23:46:20 +01:00
|
|
|
Object object = fromJson(json, (Type) classOfT);
|
2011-03-30 15:59:06 +02:00
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deserializes the specified Json into an object of the specified type. This method
|
|
|
|
* is useful if the specified object is a generic type. For non-generic objects, use
|
|
|
|
* {@link #fromJson(String, Class)} instead. If you have the Json in a {@link Reader} instead of
|
|
|
|
* a String, use {@link #fromJson(Reader, Type)} instead.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
* @param json the string from which the object is to be deserialized
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @return an object of type T from the string
|
|
|
|
* @throws JsonParseException if json is not a valid representation for an object of type typeOfT
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
|
2010-10-22 18:06:59 +02:00
|
|
|
if (json == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-11-13 01:04:39 +01:00
|
|
|
StringReader reader = new StringReader(json);
|
|
|
|
T target = (T) fromJson(reader, typeOfT);
|
|
|
|
return target;
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deserializes the Json read from the specified reader into an object of the
|
|
|
|
* specified class. It is not suitable to use if the specified class is a generic type since it
|
|
|
|
* will not have the generic type information because of the Type Erasure feature of Java.
|
|
|
|
* Therefore, this method should not be used if the desired type is a generic type. Note that
|
|
|
|
* this method works fine if the any of the fields of the specified object are generics, just the
|
|
|
|
* object itself should not be a generic type. For the cases when the object is of generic type,
|
|
|
|
* invoke {@link #fromJson(Reader, Type)}. If you have the Json in a String form instead of a
|
|
|
|
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
* @param json the reader producing the Json from which the object is to be deserialized.
|
|
|
|
* @param classOfT the class of T
|
|
|
|
* @return an object of type T from the string
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem reading from the Reader
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
2008-09-01 05:13:32 +02:00
|
|
|
* @since 1.2
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
|
2010-11-10 03:02:57 +01:00
|
|
|
JsonReader jsonReader = new JsonReader(json);
|
|
|
|
Object object = fromJson(jsonReader, classOfT);
|
2010-11-25 00:22:13 +01:00
|
|
|
assertFullConsumption(object, jsonReader);
|
2011-03-30 15:59:06 +02:00
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deserializes the Json read from the specified reader into an object of the
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
|
|
|
* non-generic objects, use {@link #fromJson(Reader, Class)} instead. If you have the Json in a
|
|
|
|
* String form instead of a {@link Reader}, use {@link #fromJson(String, Type)} instead.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
* @param json the reader producing Json from which the object is to be deserialized
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @return an object of type T from the json
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonIOException if there was a problem reading from the Reader
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
2008-09-01 05:13:32 +02:00
|
|
|
* @since 1.2
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
|
2010-11-10 03:02:57 +01:00
|
|
|
JsonReader jsonReader = new JsonReader(json);
|
2011-11-13 21:04:29 +01:00
|
|
|
T object = (T) fromJson(jsonReader, typeOfT);
|
2010-11-25 00:22:13 +01:00
|
|
|
assertFullConsumption(object, jsonReader);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertFullConsumption(Object obj, JsonReader reader) {
|
|
|
|
try {
|
|
|
|
if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) {
|
|
|
|
throw new JsonIOException("JSON document was not fully consumed.");
|
|
|
|
}
|
|
|
|
} catch (MalformedJsonException e) {
|
|
|
|
throw new JsonSyntaxException(e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new JsonIOException(e);
|
|
|
|
}
|
2010-09-02 02:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the next JSON value from {@code reader} and convert it to an object
|
|
|
|
* of type {@code typeOfT}.
|
2010-11-03 20:46:29 +01:00
|
|
|
* Since Type is not parameterized by T, this method is type unsafe and should be used carefully
|
|
|
|
*
|
|
|
|
* @throws JsonIOException if there was a problem writing to the Reader
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
2010-09-02 02:15:23 +02:00
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
|
2011-09-11 09:04:56 +02:00
|
|
|
boolean isEmpty = true;
|
2010-09-02 02:15:23 +02:00
|
|
|
boolean oldLenient = reader.isLenient();
|
|
|
|
reader.setLenient(true);
|
|
|
|
try {
|
2011-09-11 09:04:56 +02:00
|
|
|
reader.peek();
|
|
|
|
isEmpty = false;
|
2011-11-20 16:23:08 +01:00
|
|
|
TypeAdapter<T> typeAdapter = (TypeAdapter<T>) getAdapter(TypeToken.get(typeOfT));
|
2011-08-19 05:13:06 +02:00
|
|
|
return typeAdapter.read(reader);
|
2011-09-11 09:04:56 +02:00
|
|
|
} catch (EOFException e) {
|
|
|
|
/*
|
|
|
|
* For compatibility with JSON 1.5 and earlier, we return null for empty
|
|
|
|
* documents instead of throwing.
|
|
|
|
*/
|
|
|
|
if (isEmpty) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw new JsonSyntaxException(e);
|
2011-10-24 03:32:46 +02:00
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
throw new JsonSyntaxException(e);
|
2011-08-19 05:13:06 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
|
|
|
|
throw new JsonSyntaxException(e);
|
2010-09-02 02:15:23 +02:00
|
|
|
} finally {
|
|
|
|
reader.setLenient(oldLenient);
|
|
|
|
}
|
2009-02-04 02:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deserializes the Json read from the specified parse tree into an object of the
|
|
|
|
* specified type. It is not suitable to use if the specified class is a generic type since it
|
|
|
|
* will not have the generic type information because of the Type Erasure feature of Java.
|
|
|
|
* Therefore, this method should not be used if the desired type is a generic type. Note that
|
|
|
|
* this method works fine if the any of the fields of the specified object are generics, just the
|
|
|
|
* object itself should not be a generic type. For the cases when the object is of generic type,
|
2009-10-07 11:23:14 +02:00
|
|
|
* invoke {@link #fromJson(JsonElement, Type)}.
|
2009-02-04 02:56:28 +01:00
|
|
|
* @param <T> the type of the desired object
|
2009-10-07 11:23:14 +02:00
|
|
|
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
|
2009-02-04 02:56:28 +01:00
|
|
|
* be deserialized
|
|
|
|
* @param classOfT The class of T
|
|
|
|
* @return an object of type T from the json
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
2009-02-04 02:56:28 +01:00
|
|
|
* @since 1.3
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
|
2010-11-01 23:46:20 +01:00
|
|
|
Object object = fromJson(json, (Type) classOfT);
|
2011-03-30 15:59:06 +02:00
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
2009-02-04 02:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method deserializes the Json read from the specified parse tree into an object of the
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
2009-10-07 11:23:14 +02:00
|
|
|
* non-generic objects, use {@link #fromJson(JsonElement, Class)} instead.
|
2009-02-04 02:56:28 +01:00
|
|
|
*
|
|
|
|
* @param <T> the type of the desired object
|
2009-10-07 11:23:14 +02:00
|
|
|
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
|
2009-02-04 02:56:28 +01:00
|
|
|
* be deserialized
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
* <pre>
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
* </pre>
|
|
|
|
* @return an object of type T from the json
|
2010-11-03 20:46:29 +01:00
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
2009-02-04 02:56:28 +01:00
|
|
|
* @since 1.3
|
|
|
|
*/
|
2010-11-03 20:46:29 +01:00
|
|
|
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
|
2009-05-14 22:18:45 +02:00
|
|
|
if (json == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-11-13 21:04:29 +01:00
|
|
|
return (T) fromJson(new JsonElementReader(json), typeOfT);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
static class FutureTypeAdapter<T> extends TypeAdapter<T> {
|
|
|
|
private TypeAdapter<T> delegate;
|
|
|
|
|
|
|
|
public void setDelegate(TypeAdapter<T> typeAdapter) {
|
|
|
|
if (delegate != null) {
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
delegate = typeAdapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public T read(JsonReader reader) throws IOException {
|
|
|
|
if (delegate == null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
return delegate.read(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void write(JsonWriter writer, T value) throws IOException {
|
|
|
|
if (delegate == null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
delegate.write(writer, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 11:23:14 +02:00
|
|
|
@Override
|
2008-09-16 20:28:31 +02:00
|
|
|
public String toString() {
|
2009-01-20 02:36:54 +01:00
|
|
|
StringBuilder sb = new StringBuilder("{")
|
|
|
|
.append("serializeNulls:").append(serializeNulls)
|
2011-11-23 07:16:55 +01:00
|
|
|
.append("factories:").append(factories)
|
2011-09-11 09:04:56 +02:00
|
|
|
.append(",instanceCreators:").append(constructorConstructor)
|
2009-01-20 02:36:54 +01:00
|
|
|
.append("}");
|
|
|
|
return sb.toString();
|
2008-09-16 20:28:31 +02:00
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|