made toJson/fromJson/toJsonTree methods public in TypeAdapter.
made Gson.getNextAdapter method public.
This commit is contained in:
parent
49525c8d64
commit
6c78bf5247
@ -40,7 +40,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.1-SNAPSHOT</version>
|
<version>2.2-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -363,29 +363,27 @@ public final class Gson {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
/**
|
||||||
GsonInternalAccess.INSTANCE = new GsonInternalAccess() {
|
* TODO: needs documentation
|
||||||
@Override public <T> TypeAdapter<T> getNextAdapter(
|
* @since 2.2
|
||||||
Gson gson, TypeAdapterFactory skipPast, TypeToken<T> type) {
|
*/
|
||||||
boolean skipPastFound = false;
|
public <T> TypeAdapter<T> getNextAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) {
|
||||||
|
boolean skipPastFound = false;
|
||||||
|
|
||||||
for (TypeAdapterFactory factory : gson.factories) {
|
for (TypeAdapterFactory factory : factories) {
|
||||||
if (!skipPastFound) {
|
if (!skipPastFound) {
|
||||||
if (factory == skipPast) {
|
if (factory == skipPast) {
|
||||||
skipPastFound = true;
|
skipPastFound = true;
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
TypeAdapter<T> candidate = factory.create(gson, type);
|
|
||||||
if (candidate != null) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
throw new IllegalArgumentException("GSON cannot serialize " + type);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
TypeAdapter<T> candidate = factory.create(this, type);
|
||||||
|
if (candidate != null) {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("GSON cannot serialize " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,7 +76,7 @@ final class TreeTypeAdapter<T> extends TypeAdapter<T> {
|
|||||||
TypeAdapter<T> d = delegate;
|
TypeAdapter<T> d = delegate;
|
||||||
return d != null
|
return d != null
|
||||||
? d
|
? d
|
||||||
: (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, skipPast, typeToken));
|
: (delegate = gson.getNextAdapter(skipPast, typeToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,8 +135,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
* writing.
|
* writing.
|
||||||
*
|
*
|
||||||
* @param value the Java object to convert. May be null.
|
* @param value the Java object to convert. May be null.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final void toJson(Writer out, T value) throws IOException {
|
public final void toJson(Writer out, T value) throws IOException {
|
||||||
JsonWriter writer = new JsonWriter(out);
|
JsonWriter writer = new JsonWriter(out);
|
||||||
write(writer, value);
|
write(writer, value);
|
||||||
}
|
}
|
||||||
@ -208,8 +209,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
* writing.
|
* writing.
|
||||||
*
|
*
|
||||||
* @param value the Java object to convert. May be null.
|
* @param value the Java object to convert. May be null.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final String toJson(T value) throws IOException {
|
public final String toJson(T value) throws IOException {
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
toJson(stringWriter, value);
|
toJson(stringWriter, value);
|
||||||
return stringWriter.toString();
|
return stringWriter.toString();
|
||||||
@ -220,8 +222,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
*
|
*
|
||||||
* @param value the Java object to convert. May be null.
|
* @param value the Java object to convert. May be null.
|
||||||
* @return the converted JSON tree. May be {@link JsonNull}.
|
* @return the converted JSON tree. May be {@link JsonNull}.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final JsonElement toJsonTree(T value) {
|
public final JsonElement toJsonTree(T value) {
|
||||||
try {
|
try {
|
||||||
JsonTreeWriter jsonWriter = new JsonTreeWriter();
|
JsonTreeWriter jsonWriter = new JsonTreeWriter();
|
||||||
jsonWriter.setLenient(true);
|
jsonWriter.setLenient(true);
|
||||||
@ -247,8 +250,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
* {@code JsonReader} and call {@link #read(JsonReader)} for lenient reading.
|
* {@code JsonReader} and call {@link #read(JsonReader)} for lenient reading.
|
||||||
*
|
*
|
||||||
* @return the converted Java object. May be null.
|
* @return the converted Java object. May be null.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final T fromJson(Reader in) throws IOException {
|
public final T fromJson(Reader in) throws IOException {
|
||||||
JsonReader reader = new JsonReader(in);
|
JsonReader reader = new JsonReader(in);
|
||||||
reader.setLenient(true); // TODO: non-lenient?
|
reader.setLenient(true); // TODO: non-lenient?
|
||||||
return read(reader);
|
return read(reader);
|
||||||
@ -261,8 +265,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
* JsonReader} and call {@link #read(JsonReader)} for lenient reading.
|
* JsonReader} and call {@link #read(JsonReader)} for lenient reading.
|
||||||
*
|
*
|
||||||
* @return the converted Java object. May be null.
|
* @return the converted Java object. May be null.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final T fromJson(String json) throws IOException {
|
public final T fromJson(String json) throws IOException {
|
||||||
return fromJson(new StringReader(json));
|
return fromJson(new StringReader(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,8 +275,9 @@ public abstract class TypeAdapter<T> {
|
|||||||
* Converts {@code jsonTree} to a Java object.
|
* Converts {@code jsonTree} to a Java object.
|
||||||
*
|
*
|
||||||
* @param jsonTree the Java object to convert. May be {@link JsonNull}.
|
* @param jsonTree the Java object to convert. May be {@link JsonNull}.
|
||||||
|
* @since 2.2
|
||||||
*/
|
*/
|
||||||
/*public*/ final T fromJsonTree(JsonElement jsonTree) {
|
public final T fromJsonTree(JsonElement jsonTree) {
|
||||||
try {
|
try {
|
||||||
JsonReader jsonReader = new JsonTreeReader(jsonTree);
|
JsonReader jsonReader = new JsonTreeReader(jsonTree);
|
||||||
jsonReader.setLenient(true);
|
jsonReader.setLenient(true);
|
||||||
|
@ -16,9 +16,14 @@
|
|||||||
|
|
||||||
package com.google.gson.internal.bind;
|
package com.google.gson.internal.bind;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonIOException;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.google.gson.TypeAdapter;
|
import com.google.gson.TypeAdapter;
|
||||||
@ -32,11 +37,6 @@ import com.google.gson.reflect.TypeToken;
|
|||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapts maps to either JSON objects or JSON arrays.
|
* Adapts maps to either JSON objects or JSON arrays.
|
||||||
@ -217,7 +217,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
|
|||||||
|
|
||||||
List<V> values = new ArrayList<V>(map.size());
|
List<V> values = new ArrayList<V>(map.size());
|
||||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||||
JsonElement keyElement = toJsonTree(keyTypeAdapter, entry.getKey());
|
JsonElement keyElement = keyTypeAdapter.toJsonTree(entry.getKey());
|
||||||
keys.add(keyElement);
|
keys.add(keyElement);
|
||||||
values.add(entry.getValue());
|
values.add(entry.getValue());
|
||||||
hasComplexKeys |= keyElement.isJsonArray() || keyElement.isJsonObject();
|
hasComplexKeys |= keyElement.isJsonArray() || keyElement.isJsonObject();
|
||||||
@ -262,16 +262,4 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove this when TypeAdapter.toJsonTree() is public
|
|
||||||
private static <T> JsonElement toJsonTree(TypeAdapter<T> typeAdapter, T value) {
|
|
||||||
try {
|
|
||||||
JsonTreeWriter jsonWriter = new JsonTreeWriter();
|
|
||||||
jsonWriter.setLenient(true);
|
|
||||||
typeAdapter.write(jsonWriter, value);
|
|
||||||
return jsonWriter.get();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new JsonIOException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user