I have a GsonFactory declared like this:
public class GsonFactory {
public Gson create() {
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(MyInterface.class, new MyInstanceCreator())
.create();
}
private static final GsonFactory instance = new GsonFactory();
private GsonFactory() {
}
public static Gson getInstance(){
return instance.create();
}
}
I also have the InstanceCreator class:
public class MyInstanceCreator implements InstanceCreator<MyInterface> {
#Override
public StrategySymbol createInstance(Type type) {
return new MyInterfaceImpl();
}
}
class MyClass {
private List<MyInterface> myList;
public List<MyInterface> getMyList() { return myList; }
public void setMyList(List<MyInterface> myList) { this.myList = myList; }
}
interface MyInterface {
Long getValue1();
void setValue1(Long value1);
}
class MyInterfaceImpl implements MyInterface {
private Long value1;
#Override
public Long getValue1() {
return value1;
}
#Override
public void setValue1(Long value1) {
this.value1 = value1
}
}
This code seems well implemented, but if I try to parse a JSON with value1:
MyClass obj = GsonFactory.getInstance().fromJson("{'myList': [{'value1':8}]}", MyClass.class);
The Object is returned with an instance of MyInterfaceImpl, but the field value1 is always null. As I could see, it seems like Gson looks for the fields in the interface (none) and not in the class implementing the interface.
Does anyone know how to solve this?
InstanceCreator can only create instances of a class that does not define a no-args constructor. It does not handle deserialization. Gson still won't be able to figure out the concrete data type of the object.
Solution. Custom deserializer for interface
You need to define a custom deserializer for your interface and register this new type adapter in factory.
public class MyInterfaceDeserializer implements JsonDeserializer<MyInterface> {
#Override
public MyInterface deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return jsonDeserializationContext.deserialize(jsonElement, MyInterfaceImpl.class);
}
}
public class GsonFactory {
public Gson create() {
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(MyInterface.class, new MyInterfaceDeserializer())
.create();
}
}
OR Generic deserializer
public class InterfaceDeserializer<T, E> implements JsonDeserializer<T> {
private final Class<E> implementationClass;
public InterfaceDeserializer(Class<E> implementationClass) {
this.implementationClass = implementationClass;
}
#Override
public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return jsonDeserializationContext.deserialize(jsonElement, implementationClass);
}
}
public class GsonFactory {
public Gson create() {
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(MyInterface.class, new InterfaceDeserializer<MyInterface, MyInterfaceImpl>(MyInterfaceImpl.class))
.create();
}
OR
public class MyInterfaceDeserializer implements JsonDeserializer<MyInterface> {
#Override
public MyInterface deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
MyInterface myInterface = new MyInterfaceImpl();
JsonObject jObject = jsonElement.getAsJsonObject();
myInterface.setValue1(jObject.get("value1").getAsLong());
return myInterface;
}
}
public class GsonFactory {
public Gson create() {
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(MyInterface.class, new MyInterfaceDeserializer())
.create();
}
}
Another option
Use RuntimeTypeAdapterFactory but this solution requires an extra type property in json to define the exact subtype, see the documentation.
Example:
public Gson create() {
RuntimeTypeAdapterFactory<MyInterface> runtimeTypeAdapterFactory = RuntimeTypeAdapterFactory.of(MyInterface.class).registerSubtype(MyInterfaceImpl.class, "MyInterfaceImpl");
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapterFactory(runtimeTypeAdapterFactory)
.create();
}
JSON must contain type field:
{
myList: [
{
value1: 8,
type: MyInterfaceImpl
}
]
}
Please note gson-extras library required for that option. This artifact is located at CronApp repository.
Related
Let's start from example:
If the data is correct, it should be ( the Beijing cities is empty )
{
"code":200,
"msg":"success",
"data":[
{
"id":1,
"name":"Beijing",
"cities":[]
},
{
"id":2,
"name":"Guangdong",
"cities":[
{
"id":1,
"name":"Guangzhou"
}
]
}
]
}
Now I got a wrong data. ( the Beijing cities is null )
{
"code":200,
"msg":"success",
"data":[
{
"id":1,
"name":"Beijing",
"cities":null
},
{
"id":2,
"name":"Guangdong",
"cities":[
{
"id":1,
"name":"Guangzhou"
}
]
}
]
}
I am using the Retrofit2 ResponseBodyConverter ,the entity class:
public class Result<T> {
private int code;
private String msg;
private T data;
// getters, setters
}
public class Province {
private int id;
private String name;
private List<City> cities;
}
public class City {
private int id;
private String name;
}
The data obtained after deserialization is like this:
but the data I need is like this:
In order to have better fault tolerance, when the data is list, I want to process it by myself.
First of all,I tried to use JsonDeserializer
Gson gson = new GsonBuilder()
.serializeNulls()
.registerTypeHierarchyAdapter(List.class, new GsonListAdapter())
.create();
static class GsonListAdapter implements JsonDeserializer<List<?>> {
#Override
public List<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
Type itemType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
List list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JsonElement element = array.get(i);
Object item = context.deserialize(element, itemType);
list.add(item);
}
return list;
} else {
return Collections.EMPTY_LIST;
}
}
}
JsonDeserializer is valid when the data is "", {}, and [],but data is null, it will not work.
Then I tried to use TypeAdapter
static class GsonListAdapter extends TypeAdapter<List<?>> {
#Override
public void write(JsonWriter out, List<?> value) throws IOException {
out.value(String.valueOf(value));
}
#Override
public List<?> read(JsonReader reader) throws IOException {
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
reader.skipValue();
return Collections.EMPTY_LIST;
}
return new Gson().fromJson(reader, new TypeToken<List<?>>() {}.getType());
}
}
In this way, no matter what the data is, it can work properly.We know that using TypeToken<List<?>> will give us the LinkedHashMap,So although TypeAdapter can work properly, but I don't know how to convert JsonReader to the List <?>.
So I wonder if there are other ways that I can handle the wrong list data? Or convert JsonReader to the List <?> data I want.
I found the CollectionTypeAdapterFactory in Gson source code.I tried to modify it,it has been tested and it is useful.
public class CollectionTypeAdapterFactory implements TypeAdapterFactory {
private final ConstructorConstructor constructorConstructor;
public CollectionTypeAdapterFactory(ConstructorConstructor constructorConstructor) {
this.constructorConstructor = constructorConstructor;
}
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return null;
}
Type elementType = $Gson$Types.getCollectionElementType(type, rawType);
TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType));
ObjectConstructor<T> constructor = constructorConstructor.get(typeToken);
#SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter
TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor);
return result;
}
private static final class Adapter<E> extends TypeAdapter<Collection<E>> {
private final TypeAdapter<E> elementTypeAdapter;
private final ObjectConstructor<? extends Collection<E>> constructor;
public Adapter(Gson context, Type elementType,
TypeAdapter<E> elementTypeAdapter,
ObjectConstructor<? extends Collection<E>> constructor) {
this.elementTypeAdapter =
new TypeAdapterRuntimeTypeWrapper<E>(context, elementTypeAdapter, elementType);
this.constructor = constructor;
}
public Collection<E> read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
//In the source code is return null, I changed to return an empty collection
return constructor.construct();
}
Collection<E> collection = constructor.construct();
in.beginArray();
while (in.hasNext()) {
E instance = elementTypeAdapter.read(in);
collection.add(instance);
}
in.endArray();
return collection;
}
public void write(JsonWriter out, Collection<E> collection) throws IOException {
if (collection == null) {
out.nullValue();
return;
}
out.beginArray();
for (E element : collection) {
elementTypeAdapter.write(out, element);
}
out.endArray();
}
}
}
In the source code the TypeAdapterRuntimeTypeWrapper is protected,We must make a copy.
public class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
private final Gson context;
private final TypeAdapter<T> delegate;
private final Type type;
TypeAdapterRuntimeTypeWrapper(Gson context, TypeAdapter<T> delegate, Type type) {
this.context = context;
this.delegate = delegate;
this.type = type;
}
#Override
public T read(JsonReader in) throws IOException {
return delegate.read(in);
}
#SuppressWarnings({"rawtypes", "unchecked"})
#Override
public void write(JsonWriter out, T value) throws IOException {
TypeAdapter chosen = delegate;
Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value);
if (runtimeType != type) {
TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
// The user registered a type adapter for the runtime type, so we will use that
chosen = runtimeTypeAdapter;
} else if (!(delegate instanceof ReflectiveTypeAdapterFactory.Adapter)) {
// The user registered a type adapter for Base class, so we prefer it over the
// reflective type adapter for the runtime type
chosen = delegate;
} else {
// Use the type adapter for runtime type
chosen = runtimeTypeAdapter;
}
}
chosen.write(out, value);
}
private Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
if (value != null
&& (type == Object.class || type instanceof TypeVariable<?> || type instanceof Class<?>)) {
type = value.getClass();
}
return type;
}
}
How to use
Gson gson = new GsonBuilder().serializeNulls()
.registerTypeAdapterFactory(
new CollectionTypeAdapterFactory(new ConstructorConstructor(new HashMap<>()))
)
.create();
Result<List<Province>> result = gson.fromJson(jsonStr, new TypeToken<Result<List<Province>>>() {}.getType());
prints:
Result{code=200, msg='success', data=[Province{id=1, name='Beijing', cities=[]}, Province{id=2, name='Guangdong', cities=[City{id=1, name='Guangzhou'}]}]}
I'm running a simple experiment test below.
public class MyTest {
#Test
public void testing() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(SubData.class, new SubDataImplInstanceCreator());
Gson gson = builder.create();
Dataclass data = new Dataclass();
data.key1 = "abc";
SubDataImpl subData = new SubDataImpl();
subData.hello = "ttt";
data.sub = subData;
String jsonValue = gson.toJson(data);
System.out.println(jsonValue);
Dataclass data2 = gson.fromJson(jsonValue, Dataclass.class);
System.out.println(gson.toJson(data2));
}
class Dataclass implements Serializable {
String key1;
SubData sub;
}
interface SubData {
String getHello();
}
class SubDataImpl implements SubData, Serializable {
String hello;
#Override
public String getHello() {
return hello;
}
}
public class SubDataImplInstanceCreator implements InstanceCreator<SubDataImpl> {
#Override
public SubDataImpl createInstance(Type type) {
return new SubDataImpl();
}
}
}
I'm expecting it to return
{"key1":"abc","sub":{"hello":"ttt"}}
{"key1":"abc","sub":{"hello":"ttt"}}
As they are essentially the same data that get serialized and deserialized.
However, when I run it, I got
{"key1":"abc","sub":{"hello":"ttt"}}
{"key1":"abc","sub":{}}
Why did I loose away my SubData value, after deserializing the Json String? Did I miss anything in my code?
It seems you have hit this bug , the suggested solution is to use a TypeAdapter for the interface.
Quick and dirty implementation (use it in place ofSubDataImplInstanceTypeAdapter)
public class SubDataImplInstanceTypeAdapter implements JsonDeserializer<SubDataImpl>, JsonSerializer<SubDataImpl> {
#Override
public SubDataImpl deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
SubDataImpl impl = new SubDataImpl();
JsonObject object = json.getAsJsonObject();
impl.setHello(object.get("hello").getAsString());
return impl;
}
#Override
public JsonElement serialize(SubDataImpl src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}
}
I'm using the below
public class MyTest {
#Test
public void testing() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(SubData.class, new SubDataTypeAdapter());
Gson gson = builder.create();
Dataclass data = new Dataclass();
data.key1 = "abc";
SubDataImpl subData = new SubDataImpl();
subData.hello = "ttt";
data.sub = subData;
String jsonValue = gson.toJson(data);
System.out.println(jsonValue);
Dataclass data2 = gson.fromJson(jsonValue, Dataclass.class);
System.out.println(gson.toJson(data2));
}
class SubDataTypeAdapter extends TypeAdapter<SubDataImpl> {
#Override
public void write(JsonWriter out, final SubDataImpl subData) throws IOException {
out.beginObject();
out.name("hello").value(subData.getHello());
out.endObject();
}
#Override
public SubDataImpl read(JsonReader in) throws IOException {
final SubDataImpl subData = new SubDataImpl();
in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "hello":
subData.hello = in.nextString();
break;
}
}
in.endObject();
return subData;
}
}
class Dataclass implements Serializable {
String key1;
SubData sub;
}
abstract class SubData {
abstract String getHello();
}
class SubDataImpl extends SubData implements Serializable {
String hello;
#Override
public String getHello() {
return hello;
}
}
}
Imagine I have two classes, MyClass and MyOtherClass. I've written a serializer for MyClass. Without it, trying to serialize MyOtherClass won't work (because MyClass isn't serializable without the serializer I've written).
package com.mycompany.javatest;
import com.google.gson.*;
import java.lang.reflect.*;
public class JavaTest {
static class MyClass {
private int someValue = 123;
}
static class MyOtherClass {
private MyClass mc = new MyClass();
}
static class MyClassSerializer implements JsonSerializer<MyClass> {
#Override
public JsonElement serialize(MyClass t, Type type, JsonSerializationContext jsc) {
JsonObject result = new JsonObject();
// (Doing some magic to serialize the object here...)
result.add("someValue", jsc.serialize(t.someValue));
return result;
}
}
static class MyOtherClassSerializer implements JsonSerializer<MyOtherClass> {
#Override
public JsonElement serialize(MyOtherClass t, Type type, JsonSerializationContext jsc) {
JsonObject result = new JsonObject();
result.add("mc", jsc.serialize(t.mc)); // <--- Will fail if not using the MyClassSerializer
return result;
}
}
public static void main(String[] args) {
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(MyOtherClassSerializer.class, new MyOtherClassSerializer());
Gson gson = gb.create();
MyOtherClass object = new MyOtherClass();
String json = gson.toJson(object, MyOtherClass.class); // <--- MyClassSerializer.serialize MUST be invoked, or this will fail
}
}
My question is, how can I enforce that MyClassSerializer is registered when MyOtherClassSerializer is registered? The obvious answer is to just register both type adapters, but I'd like to know if there is a way to enforce registering both when registering MyOtherClassSerializer. One option is to only allow the type adapters to be accessed by a "register" method like this, but I don't like this solution. I still want the MyClassSerializer to be accessible.
public void registerMyOtherClassSerializer(GsonBuilder builder) {
builder.registerTypeAdapter(MyClass.class, new MyClassSerializer());
builder.registerTypeAdapter(MyOtherClass.class, new MyOtherClassSerializer());
}
Thoughts?
Thanks to Thomas Kläger. This is what I ended up doing:
package com.mycompany.javatest;
import com.google.gson.*;
import com.google.gson.reflect.*;
import com.google.gson.stream.*;
import java.io.*;
public class JavaTest {
static class MyClass {
private final int someValue = 123;
}
static class MyOtherClass {
private final MyClass mc = new MyClass();
}
public static void main(String[] args) {
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapterFactory(new MyTypeAdapterFactory());
Gson gson = gb.create();
MyOtherClass object = new MyOtherClass();
String json = gson.toJson(object, MyOtherClass.class);
System.out.println(json);
}
static class MyTypeAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> tt) {
if (MyClass.class.isAssignableFrom(tt.getRawType())) {
return (TypeAdapter<T>) new MyClassAdapter();
}
return null;
}
private static class MyClassAdapter extends TypeAdapter<MyClass> {
#Override
public MyClass read(JsonReader reader) throws IOException {
throw new UnsupportedOperationException();
}
#Override
public void write(JsonWriter writer, MyClass t) throws IOException {
writer.beginObject();
writer.name("someValue");
writer.value(t.someValue); // (Doing some magic to serialize the object here...)
writer.endObject();
}
}
}
}
I have a situation where I need to customize the serialization/deserialization of some JSON. I have simplified this into a readable example. I have a Container class that holds objects implementing MyInterface. In my example ClassA, ClassB, IntegerHolder and StringHolder implement the interface. By adding the #JsonTypeInfo annotation to my interface (and container):
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
and registering types names for each class, I can successfully read/write these to/from this JSON:
{"type":"Container","items":
[ {"type":"classA","aValue":"AAA"},
{"type":"classB","bValue":"BBB"},
{"type":"intHolder","value":123},
{"type":"stringHolder","value":"abc"} ] }
That is all very nice :) My problem is that I want to customize the serialization of the intHolder and stringHolder because they are just wrappers around native types. My JSON will be frequently edited by hand and the primitive types will be used a LOT. So I want to simplify the JSON to:
{"type":"Container","items":
[ {"type":"classA","aValue":"AAA"},
{"type":"classB","bValue":"BBB"},
123,
"abc" ] }
I have written a Serializer and Deserializer (extending StdSeralizer and StdDeserializer), put them in a SimpleModule and registered it with the mapper (as illustrated here on SO) and in isolation, it works well. By that, I mean that I can serialize/deserialize the IntegerHolder and StringHolder if they are the only objects in the container, and then only if I remove the #JsonTypeInfo annotation from the interface. If I do not, then I get this failure while writing to JSON:
[main] ERROR MyTests - can't write the Container
com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type MyInterface (by serializer of type MyTests$MyInterfaceSerializer) (through reference chain: Container["items"])
at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1047)
at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:142)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serializeTypedContents(ObjectArraySerializer.java:316)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serializeContents(ObjectArraySerializer.java:217)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:201)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:25)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:552)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3387)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2747)
at MyTests.testItemSerializationDeserializationEquality(MyTests.java:51)
at MyTests.testSerialization(MyTests.java:41)
But of course, with the #JsonTypeInfo removed, Jackson doesn't know how to deserialize ClassA and ClassB...so that fails while reading the JSON with:
[main] INFO MyTests - {"type":"Container","items":[{"aValue":"AAA"},{"bValue":"BBB"},123,"abc"]}
[main] ERROR MyTests - can't read the Container
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of MyInterface, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: java.io.ByteArrayInputStream#37883b97; line: 1, column: 45] (through reference chain: Container["items"]->Object[][0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:857)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)
at MyTests$MyInterfaceDeserializer.deserialize(MyTests.java:163)
at MyTests$MyInterfaceDeserializer.deserialize(MyTests.java:139)
I feel like Jackson can do it and I'm close to getting Jackson configured to serialize/deserialize both sets of classes, but so far my attempts have not been fruitful.
Any pointers to get me going in the right direction would be most appreciated...thanks in advance!
Here are the 7 classes in my test example:
MyInterface.java
import com.fasterxml.jackson.annotation.*;
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface MyInterface
{
}
Container.java
import com.fasterxml.jackson.annotation.*;
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public class Container
{
public Container()
{
}
public Container(MyInterface... items)
{
this.items = items;
}
public MyInterface[] getItems()
{
return items;
}
public void setItems(MyInterface[] items)
{
this.items = items;
}
#Override
public boolean equals(Object obj)
{
for (int i = 0; i < items.length; i++)
if (!(items[i].equals(((Container)obj).items[i])))
return false;
return true;
}
private MyInterface[] items;
}
MyTests.java
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.std.*;
import com.fasterxml.jackson.databind.jsontype.*;
import com.fasterxml.jackson.databind.module.*;
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.std.*;
import org.junit.*;
import org.slf4j.*;
import java.io.*;
public class MyTests
{
#Test
public void testSerialization()
{
ClassA a = new ClassA();
a.setaValue("AAA");
ClassB b = new ClassB();
b.setbValue("BBB");
IntegerHolderClass int_holder = new IntegerHolderClass();
int_holder.setValue(123);
StringHolderClass string_holder = new StringHolderClass();
string_holder.setValue("abc");
// Testing with ONLY the non-customized classes works fine with the #JsonTypeInfo annotation on MyInterface
// if the custom de/serializers are not registered via the module
// testItemSerializationDeserializationEquality(new Container(a, b), Container.class);
// Testing with ONLY the customized classes works fine with the custom de/serializers registered via the module
// if the #JsonTypeInfo annotation on MyInterface is removed
// testItemSerializationDeserializationEquality(new Container(int_holder, string_holder), Container.class);
// This variation tests them all together...doesn't work under either scenario
testItemSerializationDeserializationEquality(new Container(a, b, int_holder, string_holder), Container.class);
}
private void testItemSerializationDeserializationEquality(Object original, Class expected_super_type)
{
ObjectMapper mapper = createMapper();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try
{
mapper.writeValue(outstream, original);
outstream.flush();
}
catch (IOException e)
{
LOG.error("can't write the " + original.getClass().getSimpleName(), e);
}
LOG.info(outstream.toString());
Object copy = null;
try
{
copy = mapper.readValue(new ByteArrayInputStream(outstream.toByteArray()), expected_super_type);
}
catch (Exception e)
{
LOG.error("can't read the " + original.getClass().getSimpleName(), e);
}
Assert.assertNotNull(copy);
Assert.assertTrue(copy.equals(original));
}
private ObjectMapper createMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerSubtypes(new NamedType(ClassA.class, "classA"));
mapper.registerSubtypes(new NamedType(ClassB.class, "classB"));
mapper.registerSubtypes(new NamedType(IntegerHolderClass.class, "intHolder"));
mapper.registerSubtypes(new NamedType(StringHolderClass.class, "stringHolder"));
SimpleModule module = new SimpleModule();
module.setDeserializerModifier(new BeanDeserializerModifier()
{
#Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer)
{
if (MyInterface.class.isAssignableFrom(beanDesc.getBeanClass()))
return new MyInterfaceDeserializer(deserializer);
return deserializer;
}
});
module.setSerializerModifier(new BeanSerializerModifier()
{
#Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer)
{
if (MyInterface.class.isAssignableFrom(beanDesc.getBeanClass()))
return new MyInterfaceSerializer(serializer);
return serializer;
}
});
mapper.registerModule(module);
return mapper;
}
static class MyInterfaceSerializer extends StdSerializer<MyInterface> implements ResolvableSerializer
{
public MyInterfaceSerializer(JsonSerializer<?> def)
{
super(MyInterface.class);
_default = (JsonSerializer<MyInterface>) def;
}
#Override
public void serialize(MyInterface value, JsonGenerator jgen, SerializerProvider provider) throws IOException
{
if (value instanceof IntegerHolderClass)
jgen.writeNumber(((IntegerHolderClass) value).getValue());
else if (value instanceof StringHolderClass)
jgen.writeString(((StringHolderClass) value).getValue());
else
_default.serialize(value, jgen, provider);
}
#Override
public void resolve(SerializerProvider provider) throws JsonMappingException
{
}
private final JsonSerializer<MyInterface> _default;
}
static class MyInterfaceDeserializer extends StdDeserializer<MyInterface> implements ResolvableDeserializer
{
public MyInterfaceDeserializer(JsonDeserializer<?> def)
{
super(MyInterface.class);
_default = def;
}
#Override
public MyInterface deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
TreeNode node = parser.getCodec().readTree(parser);
if (node instanceof TextNode)
{
StringHolderClass holder = new StringHolderClass();
holder.setValue(((TextNode) node).textValue());
return holder;
}
else if (node instanceof IntNode)
{
IntegerHolderClass holder = new IntegerHolderClass();
holder.setValue(((IntNode) node).intValue());
return holder;
}
return (MyInterface) _default.deserialize(parser, context);
}
#Override
public void resolve(DeserializationContext context) throws JsonMappingException
{
// ((ResolvableDeserializer)_default).resolve(context);
}
private final JsonDeserializer<?> _default;
}
final static Logger LOG = LoggerFactory.getLogger(MyTests.class);
}
ClassA.java
public class ClassA implements MyInterface
{
public String getaValue()
{
return _aValue;
}
public void setaValue(String aValue)
{
_aValue = aValue;
}
#Override
public boolean equals(Object obj)
{
return obj instanceof ClassA && _aValue.equals(((ClassA)obj)._aValue);
}
private String _aValue;
}
ClassB.java
public class ClassB implements MyInterface
{
public String getbValue()
{
return _bValue;
}
public void setbValue(String bValue)
{
_bValue = bValue;
}
#Override
public boolean equals(Object obj)
{
return obj instanceof ClassB && _bValue.equals(((ClassB)obj)._bValue);
}
private String _bValue;
}
StringHolderClass.java
public class StringHolderClass implements MyInterface
{
public String getValue()
{
return _value;
}
public void setValue(String value)
{
_value = value;
}
#Override
public boolean equals(Object obj)
{
return obj instanceof StringHolderClass && _value.equals(((StringHolderClass)obj)._value);
}
private String _value;
}
IntegerHolderClass.java
public class IntegerHolderClass implements MyInterface
{
public int getValue()
{
return _value;
}
public void setValue(int value)
{
_value = value;
}
#Override
public boolean equals(Object obj)
{
return obj instanceof IntegerHolderClass && _value.equals(((IntegerHolderClass)obj)._value);
}
private Integer _value;
}
Two options:
Custom deserializer for MyInterface and then you do not need the JsonTypeInfo - all the logic will be in the deserializer.
You can try and have IntegerHolder and StringHolder implement another interface let's say Holder and change the JsonTypeInfo annotation to:
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl=Holder.class)
And for Holder.class specify a deserializer.
To be clear, let introduse some model:
interface A {
boolean isSomeCase();
}
class AAdapter implements JsonSerializer<A> {
public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context) {
if (src.isSomeCase()) {
/* some logic */
return result;
} else {
JsonObject json = new JsonObject();
JsonElement valueJson = <???>; // TODO serialize src like POJO
json.add(src.getClass().getSimpleName(), valueJson);
return json;
}
}
}
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(A.class. new AAdapter())
.create();
How it is possible to serealize some instance of A, which isSomeCase() = false, like any other object, that is serialized by ReflectiveTypeAdapterFactory.Adapter.
You can write a custom TypeAdapterFactory and handle incoming object's isSomeCase() result in its TypeAdapter's write() method and apply your logic there:
public class ATypeAdapterFactory implements TypeAdapterFactory {
public TypeAdapter<A> create(Gson gson, TypeToken type) {
if (!A.class.isAssignableFrom(type.getRawType())) {
// Check if incoming raw type is an instance of A interface
return null;
}
final TypeAdapter<A> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<A>() {
#Override
public void write(JsonWriter out, A value) throws IOException {
if(value.isSomeCase()) {
// your custom logic here
out.beginObject();
out.name("x").value(0);
out.endObject();
} else {
// default serialization here
delegate.write(out, value);
}
}
#Override
public A read(JsonReader in) throws IOException {
return delegate.read(in);
}
};
}
}
Test:
final GsonBuilder gsonBuilder = new GsonBuilder();
// Register custom type adapter factory
gsonBuilder.registerTypeAdapterFactory(new ATypeAdapterFactory());
final Gson gson = gsonBuilder.create();
A aSomeCaseTrue = new AImpl(true);
System.out.print("aSomeCaseTrue:" + gson.toJson(aSomeCaseTrue));
// writes; aSomeCaseTrue:{"x":0}
A aSomeCaseFalse = new AImpl(false);
System.out.print("aSomeCaseFalse:" + gson.toJson(aSomeCaseFalse););
// writes; aSomeCaseFalse:{"someCase":false}
Extras:
1) Your interface:
interface A {
boolean isSomeCase();
}
2) A sample class which implements your sample interface:
class AImpl implements A {
boolean someCase;
public AImpl(boolean value) {
this.someCase = value;
}
#Override
public boolean isSomeCase() {
return someCase;
}
}