Skip to content

Commit

Permalink
[eclipse-ee4j#617]And once again the copyright
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Pinsky <[email protected]>
  • Loading branch information
api-from-the-ion committed Oct 12, 2023
1 parent 526f98e commit c92319f
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import static org.eclipse.yasson.Jsonbs.nullableJsonb;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -121,6 +122,7 @@ public void serialize(Box box, JsonGenerator out, SerializationContext ctx) {
out.writeEnd();
};


@Test
public void testClassLevelAnnotation() {
Crate crate = new Crate();
Expand Down Expand Up @@ -173,46 +175,50 @@ public void testClassLevelAnnotationOnGenericRoot() {
* Tests JSONB deserialization of arbitrary type invoked from a Deserializer.
*/
@Test
public void testDeserializerDeserializationByType() {
public void testDeserializerDeserializationByType() throws Exception {
JsonbConfig config = new JsonbConfig().withDeserializers(new CrateDeserializer());
Jsonb jsonb = JsonbBuilder.create(config);
try (Jsonb jsonb = JsonbBuilder.create(config)) {

Box box = createPojoWithDates();
Box box = createPojoWithDates();

String expected = "{\"boxStr\":\"Box string\",\"crate\":{\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\",\"date\":\"14.05.2015 || 11:10:01\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"date\":\"2015-05-14T11:10:01\"},\"secondBoxStr\":\"Second box string\"}";
String expected =
"{\"boxStr\":\"Box string\",\"crate\":{\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\",\"date\":\"14.05.2015 || 11:10:01\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"date\":\"2015-05-14T11:10:01\"},\"secondBoxStr\":\"Second box string\"}";

Box result = jsonb.fromJson(expected, Box.class);
Box result = jsonb.fromJson(expected, Box.class);

//deserialized by deserializationContext.deserialize(Class c)
assertEquals(box.crate.crateInner.crateInnerBigDec, result.crate.crateInner.crateInnerBigDec);
assertEquals(box.crate.crateInner.crateInnerStr, result.crate.crateInner.crateInnerStr);
//deserialized by deserializationContext.deserialize(Class c)
assertEquals(box.crate.crateInner.crateInnerBigDec, result.crate.crateInner.crateInnerBigDec);
assertEquals(box.crate.crateInner.crateInnerStr, result.crate.crateInner.crateInnerStr);

assertEquals("List inner 0", result.crate.crateInnerList.get(0).crateInnerStr);
assertEquals("List inner 1", result.crate.crateInnerList.get(1).crateInnerStr);
assertEquals("List inner 0", result.crate.crateInnerList.get(0).crateInnerStr);
assertEquals("List inner 1", result.crate.crateInnerList.get(1).crateInnerStr);

//set by deserializer statically
assertEquals(new BigDecimal("123"), result.crate.crateBigDec);
assertEquals("abc", result.crate.crateStr);
//set by deserializer statically
assertEquals(new BigDecimal("123"), result.crate.crateBigDec);
assertEquals("abc", result.crate.crateStr);
}
}

/**
* Tests JSONB serialization of arbitrary type invoked from a Serializer.
*/
@Test
public void testSerializerSerializationOfType() {
public void testSerializerSerializationOfType() throws Exception {
JsonbConfig config = new JsonbConfig().withSerializers(new CrateSerializer());
Jsonb jsonb = JsonbBuilder.create(config);
String expected = "{\"boxStr\":\"Box string\",\"crate\":{\"crateStr\":\"REPLACED crate str\",\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"crateBigDec\":54321},\"secondBoxStr\":\"Second box string\"}";
Box pojo = createPojo();

assertEquals(expected, jsonb.toJson(pojo));

Box result = jsonb.fromJson(expected, Box.class);
assertEquals(new BigDecimal("54321"), result.crate.crateBigDec);
//result.crate.crateStr is mapped to crate_str by jsonb property
assertNull(result.crate.crateStr);
assertEquals(pojo.crate.crateInner.crateInnerStr, result.crate.crateInner.crateInnerStr);
assertEquals(pojo.crate.crateInner.crateInnerBigDec, result.crate.crateInner.crateInnerBigDec);
try (Jsonb jsonb = JsonbBuilder.create(config)) {
String expected =
"{\"boxStr\":\"Box string\",\"crate\":{\"crateStr\":\"REPLACED crate str\",\"crateInner\":{\"crateInnerBigDec\":10,\"crate_inner_str\":\"Single inner\"},\"crateInnerList\":[{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 0\"},{\"crateInnerBigDec\":10,\"crate_inner_str\":\"List inner 1\"}],\"crateBigDec\":54321},\"secondBoxStr\":\"Second box string\"}";
Box pojo = createPojo();

assertEquals(expected, jsonb.toJson(pojo));

Box result = jsonb.fromJson(expected, Box.class);
assertEquals(new BigDecimal("54321"), result.crate.crateBigDec);
//result.crate.crateStr is mapped to crate_str by jsonb property
assertNull(result.crate.crateStr);
assertEquals(pojo.crate.crateInner.crateInnerStr, result.crate.crateInner.crateInnerStr);
assertEquals(pojo.crate.crateInner.crateInnerBigDec, result.crate.crateInner.crateInnerBigDec);
}
}

@Test
Expand Down

0 comments on commit c92319f

Please sign in to comment.