Skip to content

Commit

Permalink
Fixed runtime type resolve from AdaptedObjectSerializer, when seriali…
Browse files Browse the repository at this point in the history
…zer is cached.

Signed-off-by: Roman Grigoriadi <bravehorsie@gmail.com>
  • Loading branch information
bravehorsie committed Jun 9, 2017
1 parent 1dd4437 commit 431dd54
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void serialize(T obj, JsonGenerator generator, SerializationContext ctx)
private JsonbSerializer<A> resolveSerializer(Marshaller ctx, A adapted) {
final ContainerSerializerProvider cached = ctx.getMappingContext().getSerializerProvider(adapted.getClass());
if (cached != null) {
return (JsonbSerializer<A>) cached.provideSerializer(new JsonbPropertyInfo().withWrapper(this).withRuntimeType(adapted.getClass()).withJsonBindingModel(model));
return (JsonbSerializer<A>) cached.provideSerializer(new JsonbPropertyInfo().withWrapper(this).withRuntimeType(model.getType()).withJsonBindingModel(model));
}
return (JsonbSerializer<A>) new SerializerBuilder(ctx.getJsonbContext()).withObjectClass(adapted.getClass()).withModel(model).withWrapper(this).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.json.bind.serializer.JsonbSerializer;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -150,10 +151,10 @@ private Optional<AbstractValueTypeSerializer<?>> getSupportedTypeSerializer(Clas
}

private Type resolveRuntimeType() {
if (genericType != null) {
if (genericType != null && genericType != Object.class) {
return genericType;
}
if (getModel() != null) {
if (getModel() != null && ! (getModel().getType() instanceof TypeVariable)) {
return getModel().getType();
}
return objectClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.eclipse.yasson.adapters;

import org.eclipse.yasson.adapters.model.LocalPolymorphicAdapter;
import org.junit.Ignore;
import org.junit.Test;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Roman Grigoriadi (roman.grigoriadi@oracle.com) on 08/06/2017.
*/
public class PolymorphicAdapterTest {

public static class AnimalAdapter extends LocalPolymorphicAdapter<Animal> {
public AnimalAdapter() {
super(Dog.class, Cat.class);
}
}

public static class Animal {
public Animal(String name) {
this.name = name;
}

public Animal() {
this.name = "NoName animal";
}
public String name;
}

public static class Dog extends Animal {
public Dog() {}
public Dog(String dogProperty) {
this.dogProperty = dogProperty;
}
public String dogProperty;
}

public static class Cat extends Animal {
public Cat() {}
public Cat(String catProperty) {
this.catProperty = catProperty;
}
public String catProperty;
}

public static class Animals {
public List<Animal> listOfAnimals = new ArrayList<>();
}

@Test
@Ignore
//add some context of adapters in stack to avoid adapter cycling.
public void testAdapter() {
JsonbConfig config = new JsonbConfig().withAdapters(new AnimalAdapter()).withFormatting(true);
Jsonb jsonb = JsonbBuilder.create(config);

Animals animals = new Animals();
animals.listOfAnimals.add(new Dog("Hunting"));
animals.listOfAnimals.add(new Dog("Watching"));
animals.listOfAnimals.add(new Cat("Sleeping"));
animals.listOfAnimals.add(new Cat("Playing"));

System.out.println("Wrapper object: ");
final String s = jsonb.toJson(animals, new ArrayList<Animal>(){}.getClass().getGenericSuperclass());
System.out.println(s);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.eclipse.yasson.adapters.model;

import javax.json.bind.adapter.JsonbAdapter;
import java.util.stream.Stream;

/**
* Created by Roman Grigoriadi (roman.grigoriadi@oracle.com) on 08/06/2017.
*/
public class LocalPolymorphicAdapter<T> implements JsonbAdapter<T, LocalTypeWrapper<T>> {

private final String[] allowedClasses;

/**
* Create new instance.
*
* @param allowedClasses allowed classes for loading by name
*/
public LocalPolymorphicAdapter(final Class... allowedClasses) {
this.allowedClasses = Stream.of(allowedClasses).map(Class::getName).toArray(value -> new String[allowedClasses.length]);
}

/**
* Returns all classes which are allowed for loading.
*
* @return allowed classes for loading by name
*/
public String[] getAllowedClasses() {
return allowedClasses;
}

@Override
public LocalTypeWrapper<T> adaptToJson(T obj) throws Exception {
System.out.println("AdaptingToJson: " + obj);
LocalTypeWrapper<T> wrapper = new LocalTypeWrapper<>();
wrapper.setClassName(obj.getClass().getName());
wrapper.setInstance(obj);
return wrapper;
}

@Override
public T adaptFromJson(LocalTypeWrapper<T> obj) throws Exception {
System.out.println("ADaptingFromJson: " + obj);
return obj.getInstance();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.eclipse.yasson.adapters.model;

/**
* Created by Roman Grigoriadi (roman.grigoriadi@oracle.com) on 08/06/2017.
*/
public class LocalTypeWrapper<E> {
private String className;
private E instance;

/**
* Gets class name.
*
* @return Class name.
*/
public String getClassName() {
return className;
}

/**
* Sets class name.
*
* @param className Class name to set.
*/
public void setClassName(String className) {
this.className = className;
}

/**
* Gets instance.
*
* @return Instance.
*/
public E getInstance() {
return instance;
}

/**
* Sets instance.
*
* @param instance Instance to set.
*/
public void setInstance(E instance) {
this.instance = instance;
}

}

0 comments on commit 431dd54

Please sign in to comment.