More code through the same fromJson path

This commit is contained in:
Jesse Wilson 2011-09-09 05:40:34 +00:00
parent da67003eef
commit 99801915aa
2 changed files with 7 additions and 16 deletions

View File

@ -35,6 +35,7 @@ import com.google.gson.stream.MalformedJsonException;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -557,14 +558,12 @@ public final class Gson {
return null; return null;
} }
try { try {
TypeAdapter<T> typeAdapter = (TypeAdapter<T>)miniGson.getAdapter(TypeToken.get(typeOfT)); StringReader reader = new StringReader(json);
return typeAdapter.fromJson(json); T target = (T) fromJson(reader, typeOfT);
return target;
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
throw new JsonSyntaxException(e); throw new JsonSyntaxException(e);
} catch (IOException e) {
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
throw new JsonSyntaxException(e);
} }
} }
@ -641,14 +640,10 @@ public final class Gson {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
if (reader == null) {
// TODO(inder): remove this null check since we didnt have it in a previously released version
return null;
}
boolean oldLenient = reader.isLenient(); boolean oldLenient = reader.isLenient();
reader.setLenient(true); reader.setLenient(true);
try { try {
TypeAdapter<T> typeAdapter = (TypeAdapter<T>)miniGson.getAdapter(TypeToken.get(typeOfT)); TypeAdapter<T> typeAdapter = (TypeAdapter<T>) miniGson.getAdapter(TypeToken.get(typeOfT));
return typeAdapter.read(reader); return typeAdapter.read(reader);
} catch (IOException e) { } catch (IOException e) {
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
@ -702,7 +697,7 @@ public final class Gson {
if (json == null) { if (json == null) {
return null; return null;
} }
return (T) miniGson.getAdapter(TypeToken.get(typeOfT)).fromJsonElement(json); return fromJson(new StringReader(json.toString()), typeOfT);
} }
@Override @Override

View File

@ -70,11 +70,7 @@ public abstract class TypeAdapter<T> {
public T fromJsonElement(JsonElement json) { public T fromJsonElement(JsonElement json) {
try { try {
StringWriter stringWriter = new StringWriter(); JsonReader jsonReader = new JsonReader(new StringReader(json.toString()));
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setLenient(true);
Streams.write(json, false, jsonWriter);
JsonReader jsonReader = new JsonReader(new StringReader(stringWriter.toString()));
jsonReader.setLenient(true); jsonReader.setLenient(true);
return read(jsonReader); return read(jsonReader);
} catch (IOException e) { } catch (IOException e) {