Roll back JsonSerializationContext and JsonDeserializationContext to their 1.7.2 API.

This commit is contained in:
Jesse Wilson 2011-10-01 00:58:25 +00:00
parent 70abd0ba87
commit de835d4dcd
4 changed files with 27 additions and 44 deletions

View File

@ -100,7 +100,7 @@ import java.util.Map;
*/
public final class Gson {
@SuppressWarnings("unchecked")
static final ParameterizedTypeHandlerMap EMPTY_MAP =
static final ParameterizedTypeHandlerMap EMPTY_MAP =
new ParameterizedTypeHandlerMap().makeUnmodifiable();
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
@ -254,8 +254,7 @@ public final class Gson {
builder.factory(factory);
}
builder.factory(new GsonToMiniGsonTypeAdapterFactory(serializers, deserializers,
new JsonDeserializationContext(this), new JsonSerializationContext(this), serializeNulls))
builder.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers, serializeNulls))
.factory(TypeAdapters.URL_FACTORY)
.factory(TypeAdapters.URI_FACTORY)
.factory(TypeAdapters.UUID_FACTORY)

View File

@ -32,15 +32,28 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory {
private final JsonSerializationContext serializationContext;
private final boolean serializeNulls;
GsonToMiniGsonTypeAdapterFactory(ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
public GsonToMiniGsonTypeAdapterFactory(final Gson gson,
ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
JsonDeserializationContext deserializationContext,
JsonSerializationContext serializationContext, boolean serializeNulls) {
boolean serializeNulls) {
this.serializers = serializers;
this.deserializers = deserializers;
this.serializeNulls = serializeNulls;
this.deserializationContext = deserializationContext;
this.serializationContext = serializationContext;
this.deserializationContext = new JsonDeserializationContext() {
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return gson.fromJson(json, typeOfT);
}
};
this.serializationContext = new JsonSerializationContext() {
public JsonElement serialize(Object src) {
return gson.toJsonTree(src);
}
public JsonElement serialize(Object src, Type typeOfSrc) {
return gson.toJsonTree(src, typeOfSrc);
}
};
}
public <T> TypeAdapter<T> create(final MiniGson context, final TypeToken<T> typeToken) {

View File

@ -26,38 +26,19 @@ import java.lang.reflect.Type;
* @author Inderjeet Singh
* @author Joel Leitch
*/
public class JsonDeserializationContext {
private final Gson gson;
JsonDeserializationContext(Gson gson) {
this.gson = gson;
}
/**
* TODO: remove this from the public API
*/
@Deprecated
public <T> T construct(Type type) {
throw new UnsupportedOperationException();
}
public Object constructArray(Type type, int length) {
throw new UnsupportedOperationException();
}
public interface JsonDeserializationContext {
/**
* Invokes default deserialization on the specified object. It should never be invoked on
* the element received as a parameter of the
* {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} method. Doing
* so will result in an infinite loop since Gson will in-turn call the custom deserializer again.
*
* @param json the parse tree.
* @param typeOfT type of the expected return value.
* @param <T> The type of the deserialized object.
* @return An object of type typeOfT.
* @throws JsonParseException if the parse tree does not contain expected data.
*/
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return gson.fromJson(json, typeOfT);
}
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
}

View File

@ -25,13 +25,7 @@ import java.lang.reflect.Type;
* @author Inderjeet Singh
* @author Joel Leitch
*/
public class JsonSerializationContext {
private final Gson gson;
JsonSerializationContext(Gson gson) {
this.gson = gson;
}
public interface JsonSerializationContext {
/**
* Invokes default serialization on the specified object.
@ -39,9 +33,7 @@ public class JsonSerializationContext {
* @param src the object that needs to be serialized.
* @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}.
*/
public JsonElement serialize(Object src) {
return gson.toJsonTree(src);
}
public JsonElement serialize(Object src);
/**
* Invokes default serialization on the specified object passing the specific type information.
@ -53,7 +45,5 @@ public class JsonSerializationContext {
* @param typeOfSrc the actual genericized type of src object.
* @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}.
*/
public JsonElement serialize(Object src, Type typeOfSrc) {
return gson.toJsonTree(src, typeOfSrc);
}
}
public JsonElement serialize(Object src, Type typeOfSrc);
}