-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed runtime type resolve from AdaptedObjectSerializer, when seriali…
…zer is cached. Signed-off-by: Roman Grigoriadi <bravehorsie@gmail.com>
- Loading branch information
1 parent
1dd4437
commit 431dd54
Showing
5 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/java/org/eclipse/yasson/adapters/PolymorphicAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/eclipse/yasson/adapters/model/LocalPolymorphicAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/eclipse/yasson/adapters/model/LocalTypeWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |