Skip to content

Commit

Permalink
Add a test reading json structure to a String inside JsonbSerializer.
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Grigoriadi <[email protected]>
  • Loading branch information
Roman Grigoriadi committed Jan 21, 2019
1 parent 75191e2 commit b90e2d6
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 78 deletions.
75 changes: 35 additions & 40 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>info.grigoriadi</groupId>
<artifactId>yasson-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>info.grigoriadi</groupId>
<artifactId>yasson-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>yasson-client</name>
<url>http://maven.apache.org</url>
<name>yasson-client</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.3</version>
<exclusions>
<exclusion>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
38 changes: 0 additions & 38 deletions src/test/java/info/grigoriadi/AppTest.java

This file was deleted.

193 changes: 193 additions & 0 deletions src/test/java/info/grigoriadi/ReadJsonStructureToStringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package info.grigoriadi;

import org.junit.Assert;
import org.junit.Test;

import javax.json.Json;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.serializer.DeserializationContext;
import javax.json.bind.serializer.JsonbDeserializer;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonParser;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ReadJsonStructureToStringTest {

@Test
public void testCustomSerializer() {
Dog bruno = new Dog();
bruno.setName("Bruno");

Dog pluto = new Dog();
pluto.setName("Pluto");
bruno.setFriends(new ArrayList<>());
bruno.getFriends().add(pluto);

AnimalContainer container = new AnimalContainer();
MetaData metaData = new MetaData();
metaData.setType(AnimalType.DOG);
container.setMetaData(metaData);
container.setAnimal(bruno);

String json = "{\"animal\":{\"name\":\"Bruno\",\"friends\":[{\"name\":\"Pluto\"}]},\"metaData\":{\"type\":\"DOG\"}}";
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withDeserializers(new AnimalContainerDeserializer()));

AnimalContainer result = jsonb.fromJson(json, AnimalContainer.class);
Assert.assertTrue(result.getAnimal() instanceof Dog);
Assert.assertEquals("Bruno", result.getAnimal().getName());
}

public static class AnimalContainerDeserializer implements JsonbDeserializer<AnimalContainer> {

//Cache jsonb instance to avoid parsing Dog.class with reflection every time.
Jsonb jsonb = JsonbBuilder.create();

@Override
public AnimalContainer deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
String animalJson = null;
AnimalContainer container = new AnimalContainer();

while (parser.hasNext()) {
JsonParser.Event next = parser.next();
switch (next) {
case KEY_NAME:
String field = parser.getString();
switch (field) {
case "metaData":
container.setMetaData(ctx.deserialize(MetaData.class, parser));
break;
case "animal":
animalJson = readJsonStructure(parser);
break;
default:
// ignore
break;
}
break;
case END_OBJECT:
if (container.getMetaData().getType() == AnimalType.DOG) {
container.setAnimal(jsonb.fromJson(animalJson, Dog.class));
return container;
}
}

}
throw new RuntimeException("Parse error");
}

private String readJsonStructure(JsonParser parser) {
StringWriter writer = new StringWriter();
JsonGenerator generator = Json.createGenerator(writer);
int level = 0;
while (parser.hasNext()) {
JsonParser.Event next = parser.next();
switch (next) {
case START_ARRAY:
level++;
generator.writeStartArray();
break;
case START_OBJECT:
level++;
generator.writeStartObject();
break;
case END_ARRAY:
case END_OBJECT:
generator.writeEnd();
level--;
if (level == 0) {
generator.close();
return writer.toString();
}
break;
case KEY_NAME:
generator.writeKey(parser.getString());
break;
case VALUE_NULL:
generator.writeNull();
break;
case VALUE_TRUE:
generator.write(true);
break;
case VALUE_FALSE:
generator.write(false);
break;
case VALUE_NUMBER:
generator.write(parser.getBigDecimal());
break;
case VALUE_STRING:
generator.write(parser.getString());
break;
}
}
throw new RuntimeException("JsonStructure not terminated");
}
}

public static class AnimalContainer {
private Animal animal;
private MetaData metaData;

public Animal getAnimal() {
return animal;
}

public void setAnimal(Animal animal) {
this.animal = animal;
}

public MetaData getMetaData() {
return metaData;
}

public void setMetaData(MetaData metaData) {
this.metaData = metaData;
}
}

public static class MetaData {
private AnimalType type;

public AnimalType getType() {
return type;
}

public void setType(AnimalType type) {
this.type = type;
}
}

public static abstract class Animal {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public static class Dog extends Animal {
private List<Dog> friends;

public List<Dog> getFriends() {
return friends;
}

public void setFriends(List<Dog> friends) {
this.friends = friends;
}
}

public enum AnimalType {
DOG;
}


}

0 comments on commit b90e2d6

Please sign in to comment.