2011-05-14 04:04:17 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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.typeadapters;
|
|
|
|
|
2012-04-14 21:04:14 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2011-11-21 06:09:14 +01:00
|
|
|
import com.google.gson.Gson;
|
2011-05-14 04:04:17 +02:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
2011-11-21 06:09:14 +01:00
|
|
|
import com.google.gson.TypeAdapter;
|
2011-12-23 19:27:13 +01:00
|
|
|
import com.google.gson.TypeAdapterFactory;
|
2011-09-28 21:14:46 +02:00
|
|
|
import com.google.gson.internal.Streams;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2011-05-14 04:04:17 +02:00
|
|
|
|
2011-06-06 07:40:13 +02:00
|
|
|
/**
|
|
|
|
* Adapts values whose runtime type may differ from their declaration type. This
|
|
|
|
* is necessary when a field's type is not the same type that GSON should create
|
|
|
|
* when deserializing that field. For example, consider these types:
|
|
|
|
* <pre> {@code
|
|
|
|
* abstract class Shape {
|
|
|
|
* int x;
|
|
|
|
* int y;
|
|
|
|
* }
|
|
|
|
* class Circle extends Shape {
|
|
|
|
* int radius;
|
|
|
|
* }
|
|
|
|
* class Rectangle extends Shape {
|
|
|
|
* int width;
|
|
|
|
* int height;
|
|
|
|
* }
|
|
|
|
* class Diamond extends Shape {
|
|
|
|
* int width;
|
|
|
|
* int height;
|
|
|
|
* }
|
|
|
|
* class Drawing {
|
|
|
|
* Shape bottomShape;
|
|
|
|
* Shape topShape;
|
|
|
|
* }
|
|
|
|
* }</pre>
|
|
|
|
* <p>Without additional type information, the serialized JSON is ambiguous. Is
|
|
|
|
* the bottom shape in this drawing a rectangle or a diamond? <pre> {@code
|
|
|
|
* {
|
|
|
|
* "bottomShape": {
|
|
|
|
* "width": 10,
|
|
|
|
* "height": 5,
|
|
|
|
* "x": 0,
|
|
|
|
* "y": 0
|
|
|
|
* },
|
|
|
|
* "topShape": {
|
|
|
|
* "radius": 2,
|
|
|
|
* "x": 4,
|
|
|
|
* "y": 1
|
|
|
|
* }
|
|
|
|
* }}</pre>
|
|
|
|
* This class addresses this problem by adding type information to the
|
|
|
|
* serialized JSON and honoring that type information when the JSON is
|
|
|
|
* deserialized: <pre> {@code
|
|
|
|
* {
|
|
|
|
* "bottomShape": {
|
|
|
|
* "type": "Diamond",
|
|
|
|
* "width": 10,
|
|
|
|
* "height": 5,
|
|
|
|
* "x": 0,
|
|
|
|
* "y": 0
|
|
|
|
* },
|
|
|
|
* "topShape": {
|
|
|
|
* "type": "Circle",
|
|
|
|
* "radius": 2,
|
|
|
|
* "x": 4,
|
|
|
|
* "y": 1
|
|
|
|
* }
|
|
|
|
* }}</pre>
|
|
|
|
* Both the type field name ({@code "type"}) and the type labels ({@code
|
|
|
|
* "Rectangle"}) are configurable.
|
|
|
|
*
|
|
|
|
* <h3>Registering Types</h3>
|
2016-02-19 15:14:55 +01:00
|
|
|
* Create a {@code RuntimeTypeAdapterFactory} by passing the base type and type field
|
2011-09-28 21:14:46 +02:00
|
|
|
* name to the {@link #of} factory method. If you don't supply an explicit type
|
|
|
|
* field name, {@code "type"} will be used. <pre> {@code
|
2016-05-24 21:45:55 +02:00
|
|
|
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory
|
2016-02-19 15:14:55 +01:00
|
|
|
* = RuntimeTypeAdapterFactory.of(Shape.class, "type");
|
2011-06-06 07:40:13 +02:00
|
|
|
* }</pre>
|
|
|
|
* Next register all of your subtypes. Every subtype must be explicitly
|
|
|
|
* registered. This protects your application from injection attacks. If you
|
|
|
|
* don't supply an explicit type label, the type's simple name will be used.
|
|
|
|
* <pre> {@code
|
|
|
|
* shapeAdapter.registerSubtype(Rectangle.class, "Rectangle");
|
|
|
|
* shapeAdapter.registerSubtype(Circle.class, "Circle");
|
|
|
|
* shapeAdapter.registerSubtype(Diamond.class, "Diamond");
|
|
|
|
* }</pre>
|
2016-05-24 21:45:55 +02:00
|
|
|
* Finally, register the type adapter factory in your application's GSON builder:
|
2011-06-06 07:40:13 +02:00
|
|
|
* <pre> {@code
|
|
|
|
* Gson gson = new GsonBuilder()
|
2016-05-25 20:49:39 +02:00
|
|
|
* .registerTypeAdapterFactory(shapeAdapterFactory)
|
2011-06-06 07:40:13 +02:00
|
|
|
* .create();
|
|
|
|
* }</pre>
|
|
|
|
* Like {@code GsonBuilder}, this API supports chaining: <pre> {@code
|
2016-05-24 21:45:55 +02:00
|
|
|
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class)
|
2011-06-06 07:40:13 +02:00
|
|
|
* .registerSubtype(Rectangle.class)
|
|
|
|
* .registerSubtype(Circle.class)
|
|
|
|
* .registerSubtype(Diamond.class);
|
|
|
|
* }</pre>
|
|
|
|
*/
|
2011-12-23 19:27:13 +01:00
|
|
|
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
|
2011-05-14 04:04:17 +02:00
|
|
|
private final Class<?> baseType;
|
|
|
|
private final String typeFieldName;
|
|
|
|
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>();
|
|
|
|
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<Class<?>, String>();
|
2018-07-20 23:34:45 +02:00
|
|
|
private final boolean maintainType;
|
2011-05-14 04:04:17 +02:00
|
|
|
|
2018-07-20 23:34:45 +02:00
|
|
|
private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName, boolean maintainType) {
|
2011-06-06 07:40:13 +02:00
|
|
|
if (typeFieldName == null || baseType == null) {
|
|
|
|
throw new NullPointerException();
|
|
|
|
}
|
2011-05-14 04:04:17 +02:00
|
|
|
this.baseType = baseType;
|
|
|
|
this.typeFieldName = typeFieldName;
|
2018-07-20 23:34:45 +02:00
|
|
|
this.maintainType = maintainType;
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 23:34:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new runtime type adapter using for {@code baseType} using {@code
|
|
|
|
* typeFieldName} as the type field name. Type field names are case sensitive.
|
|
|
|
* {@code maintainType} flag decide if the type will be stored in pojo or not.
|
|
|
|
*/
|
|
|
|
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) {
|
|
|
|
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, maintainType);
|
|
|
|
}
|
|
|
|
|
2011-06-06 07:40:13 +02:00
|
|
|
/**
|
2011-09-28 21:14:46 +02:00
|
|
|
* Creates a new runtime type adapter using for {@code baseType} using {@code
|
|
|
|
* typeFieldName} as the type field name. Type field names are case sensitive.
|
2011-06-06 07:40:13 +02:00
|
|
|
*/
|
2011-09-28 21:14:46 +02:00
|
|
|
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
|
2018-07-20 23:34:45 +02:00
|
|
|
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, false);
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 07:40:13 +02:00
|
|
|
/**
|
2011-09-28 21:14:46 +02:00
|
|
|
* Creates a new runtime type adapter for {@code baseType} using {@code "type"} as
|
|
|
|
* the type field name.
|
2011-06-06 07:40:13 +02:00
|
|
|
*/
|
2011-09-28 21:14:46 +02:00
|
|
|
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
|
2018-07-20 23:34:45 +02:00
|
|
|
return new RuntimeTypeAdapterFactory<T>(baseType, "type", false);
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 07:40:13 +02:00
|
|
|
/**
|
|
|
|
* Registers {@code type} identified by {@code label}. Labels are case
|
|
|
|
* sensitive.
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if either {@code type} or {@code label}
|
|
|
|
* have already been registered on this type adapter.
|
|
|
|
*/
|
2011-09-28 21:14:46 +02:00
|
|
|
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
|
2011-06-06 07:40:13 +02:00
|
|
|
if (type == null || label == null) {
|
|
|
|
throw new NullPointerException();
|
|
|
|
}
|
2011-05-14 04:04:17 +02:00
|
|
|
if (subtypeToLabel.containsKey(type) || labelToSubtype.containsKey(label)) {
|
|
|
|
throw new IllegalArgumentException("types and labels must be unique");
|
|
|
|
}
|
|
|
|
labelToSubtype.put(label, type);
|
|
|
|
subtypeToLabel.put(type, label);
|
2011-05-27 20:47:31 +02:00
|
|
|
return this;
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 07:40:13 +02:00
|
|
|
/**
|
|
|
|
* Registers {@code type} identified by its {@link Class#getSimpleName simple
|
|
|
|
* name}. Labels are case sensitive.
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if either {@code type} or its simple name
|
|
|
|
* have already been registered on this type adapter.
|
|
|
|
*/
|
2011-09-28 21:14:46 +02:00
|
|
|
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
|
2011-05-27 20:47:31 +02:00
|
|
|
return registerSubtype(type, type.getSimpleName());
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 21:04:14 +02:00
|
|
|
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
|
2011-09-28 21:14:46 +02:00
|
|
|
if (type.getRawType() != baseType) {
|
|
|
|
return null;
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:14:46 +02:00
|
|
|
final Map<String, TypeAdapter<?>> labelToDelegate
|
|
|
|
= new LinkedHashMap<String, TypeAdapter<?>>();
|
|
|
|
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
|
|
|
|
= new LinkedHashMap<Class<?>, TypeAdapter<?>>();
|
|
|
|
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
|
2012-04-14 21:04:14 +02:00
|
|
|
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
|
2011-09-28 21:14:46 +02:00
|
|
|
labelToDelegate.put(entry.getKey(), delegate);
|
|
|
|
subtypeToDelegate.put(entry.getValue(), delegate);
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
2011-09-28 21:14:46 +02:00
|
|
|
|
2012-04-14 21:04:14 +02:00
|
|
|
return new TypeAdapter<R>() {
|
|
|
|
@Override public R read(JsonReader in) throws IOException {
|
2011-12-03 20:46:25 +01:00
|
|
|
JsonElement jsonElement = Streams.parse(in);
|
2018-07-20 23:34:45 +02:00
|
|
|
JsonElement labelJsonElement;
|
|
|
|
if (maintainType) {
|
|
|
|
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
|
|
|
|
} else {
|
|
|
|
labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:14:46 +02:00
|
|
|
if (labelJsonElement == null) {
|
|
|
|
throw new JsonParseException("cannot deserialize " + baseType
|
|
|
|
+ " because it does not define a field named " + typeFieldName);
|
|
|
|
}
|
|
|
|
String label = labelJsonElement.getAsString();
|
|
|
|
@SuppressWarnings("unchecked") // registration requires that subtype extends T
|
2012-04-14 21:04:14 +02:00
|
|
|
TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
|
2011-09-28 21:14:46 +02:00
|
|
|
if (delegate == null) {
|
|
|
|
throw new JsonParseException("cannot deserialize " + baseType + " subtype named "
|
|
|
|
+ label + "; did you forget to register a subtype?");
|
|
|
|
}
|
2012-01-01 13:42:20 +01:00
|
|
|
return delegate.fromJsonTree(jsonElement);
|
2011-09-28 21:14:46 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 21:04:14 +02:00
|
|
|
@Override public void write(JsonWriter out, R value) throws IOException {
|
2011-09-28 21:14:46 +02:00
|
|
|
Class<?> srcType = value.getClass();
|
|
|
|
String label = subtypeToLabel.get(srcType);
|
|
|
|
@SuppressWarnings("unchecked") // registration requires that subtype extends T
|
2012-04-14 21:04:14 +02:00
|
|
|
TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
|
2011-09-28 21:14:46 +02:00
|
|
|
if (delegate == null) {
|
|
|
|
throw new JsonParseException("cannot serialize " + srcType.getName()
|
|
|
|
+ "; did you forget to register a subtype?");
|
|
|
|
}
|
2012-01-01 13:42:20 +01:00
|
|
|
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
|
2018-07-30 19:43:52 +02:00
|
|
|
|
|
|
|
if (maintainType) {
|
|
|
|
Streams.write(jsonObject, out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:14:46 +02:00
|
|
|
JsonObject clone = new JsonObject();
|
2018-07-30 19:43:52 +02:00
|
|
|
|
|
|
|
if (jsonObject.has(typeFieldName)) {
|
|
|
|
throw new JsonParseException("cannot serialize " + srcType.getName()
|
|
|
|
+ " because it already defines a field named " + typeFieldName);
|
2018-07-20 23:34:45 +02:00
|
|
|
}
|
2018-07-30 19:43:52 +02:00
|
|
|
clone.add(typeFieldName, new JsonPrimitive(label));
|
2018-07-20 23:34:45 +02:00
|
|
|
|
2011-09-28 21:14:46 +02:00
|
|
|
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
|
|
|
|
clone.add(e.getKey(), e.getValue());
|
|
|
|
}
|
2011-12-03 20:46:25 +01:00
|
|
|
Streams.write(clone, out);
|
2011-09-28 21:14:46 +02:00
|
|
|
}
|
2015-08-06 01:27:18 +02:00
|
|
|
}.nullSafe();
|
2011-05-14 04:04:17 +02:00
|
|
|
}
|
|
|
|
}
|