made toJson/fromJson/toJsonTree methods public in TypeAdapter.

made Gson.getNextAdapter method public.
This commit is contained in:
Inderjeet Singh 2011-12-31 08:52:59 +00:00
parent 49525c8d64
commit 6c78bf5247
5 changed files with 39 additions and 47 deletions

View File

@ -40,7 +40,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1-SNAPSHOT</version>
<version>2.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -363,13 +363,14 @@ public final class Gson {
}
}
static {
GsonInternalAccess.INSTANCE = new GsonInternalAccess() {
@Override public <T> TypeAdapter<T> getNextAdapter(
Gson gson, TypeAdapterFactory skipPast, TypeToken<T> type) {
/**
* TODO: needs documentation
* @since 2.2
*/
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 (factory == skipPast) {
skipPastFound = true;
@ -377,16 +378,13 @@ public final class Gson {
continue;
}
TypeAdapter<T> candidate = factory.create(gson, type);
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.

View File

@ -76,7 +76,7 @@ final class TreeTypeAdapter<T> extends TypeAdapter<T> {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, skipPast, typeToken));
: (delegate = gson.getNextAdapter(skipPast, typeToken));
}
/**

View File

@ -135,8 +135,9 @@ public abstract class TypeAdapter<T> {
* writing.
*
* @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);
write(writer, value);
}
@ -208,8 +209,9 @@ public abstract class TypeAdapter<T> {
* writing.
*
* @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();
toJson(stringWriter, value);
return stringWriter.toString();
@ -220,8 +222,9 @@ public abstract class TypeAdapter<T> {
*
* @param value the Java object to convert. May be null.
* @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 {
JsonTreeWriter jsonWriter = new JsonTreeWriter();
jsonWriter.setLenient(true);
@ -247,8 +250,9 @@ public abstract class TypeAdapter<T> {
* {@code JsonReader} and call {@link #read(JsonReader)} for lenient reading.
*
* @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);
reader.setLenient(true); // TODO: non-lenient?
return read(reader);
@ -261,8 +265,9 @@ public abstract class TypeAdapter<T> {
* JsonReader} and call {@link #read(JsonReader)} for lenient reading.
*
* @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));
}
@ -270,8 +275,9 @@ public abstract class TypeAdapter<T> {
* Converts {@code jsonTree} to a Java object.
*
* @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 {
JsonReader jsonReader = new JsonTreeReader(jsonTree);
jsonReader.setLenient(true);

View File

@ -16,9 +16,14 @@
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.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
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.JsonToken;
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.
@ -217,7 +217,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
List<V> values = new ArrayList<V>(map.size());
for (Map.Entry<K, V> entry : map.entrySet()) {
JsonElement keyElement = toJsonTree(keyTypeAdapter, entry.getKey());
JsonElement keyElement = keyTypeAdapter.toJsonTree(entry.getKey());
keys.add(keyElement);
values.add(entry.getValue());
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);
}
}
}