Skip to content

Commit

Permalink
[eclipse-ee4j#617]Use defaultJsonb where JsonbBuilder.create() withou…
Browse files Browse the repository at this point in the history
…t configuration is used

Signed-off-by: Anton Pinsky <anton.pinsky@ionos.com>
  • Loading branch information
api-from-the-ion committed Oct 15, 2023
1 parent 6fb1442 commit 062a161
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 61 deletions.
11 changes: 4 additions & 7 deletions src/test/java/org/eclipse/yasson/adapters/AdaptersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,11 @@ public void testAdaptedRootType() throws Exception {
}

@Test
public void testCustomAdapterInEnum() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
public void testCustomAdapterInEnum() {
Vegetables expected = Vegetables.TOMATO;

Vegetables expected = Vegetables.TOMATO;
String expectedJson = defaultJsonb.toJson(expected);

String expectedJson = jsonb.toJson(expected);

assertEquals(expected, jsonb.fromJson(expectedJson, Vegetables.class));
}
assertEquals(expected, defaultJsonb.fromJson(expectedJson, Vegetables.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,16 @@ public void collectionWrapperTest() {
}

@Test
public void multipleGenericLevels() throws Exception {
public void multipleGenericLevels() {
FinalMember member = new FinalMember();
member.setName("Jason");
FinalGenericWrapper concreteContainer = new FinalGenericWrapper();
concreteContainer.setMember(member);

String expected = "{\"member\":{\"name\":\"Jason\"}}";
try (Jsonb jsonb = JsonbBuilder.create()) {
assertEquals(expected, jsonb.toJson(concreteContainer));
FinalGenericWrapper finalGenericWrapper = jsonb.fromJson(expected, FinalGenericWrapper.class);
assertEquals(concreteContainer, finalGenericWrapper);
}
assertEquals(expected, defaultJsonb.toJson(concreteContainer));
FinalGenericWrapper finalGenericWrapper = defaultJsonb.fromJson(expected, FinalGenericWrapper.class);
assertEquals(concreteContainer, finalGenericWrapper);
}

@Test
Expand All @@ -451,18 +449,16 @@ public void lowerBoundTypeVariableInCollectionAttribute() throws Exception {

List<AnotherGenericTestClass<Integer, Shape>> asList = List.of(anotherGenericTestClass);

try (Jsonb jsonb = JsonbBuilder.create()) {
String toJson = jsonb.toJson(asList);
String toJson = defaultJsonb.toJson(asList);

Field field = LowerBoundTypeVariableWithCollectionAttributeClass.class.getDeclaredField("value");
Field field = LowerBoundTypeVariableWithCollectionAttributeClass.class.getDeclaredField("value");

Type genericType = field.getGenericType();
Type genericType = field.getGenericType();

List<AnotherGenericTestClass<Integer, Shape>> fromJson = jsonb.fromJson(toJson, genericType);
List<AnotherGenericTestClass<Integer, Shape>> fromJson = defaultJsonb.fromJson(toJson, genericType);

assertEquals(5, fromJson.get(0).field2.getArea());
assertEquals(6, fromJson.get(0).field1);
}
assertEquals(5, fromJson.get(0).field2.getArea());
assertEquals(6, fromJson.get(0).field1);
}

public interface FunctionalInterface<T> {
Expand Down
66 changes: 26 additions & 40 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +541,16 @@ public void testSerializeMapWithNullsForceArraySerializer() throws Exception {
* Map shall be stored as a single JsonObject with keys as object properties names.
*/
@Test
public void testSerializeMapToJsonObject() throws Exception {
public void testSerializeMapToJsonObject() {
Map<String, Object> map = new HashMap<>();
try (Jsonb jsonb = JsonbBuilder.create(new JsonbConfig())) {
map.put("name", "John SMith");
map.put("age", 35);
map.put("married", true);
String json = jsonb.toJson(map);
JsonObject jobj = Json.createReader(new StringReader(json)).read().asJsonObject();
assertEquals("John SMith", jobj.getString("name"));
assertEquals(35, jobj.getInt("age"));
assertTrue(jobj.getBoolean("married"));
}
map.put("name", "John SMith");
map.put("age", 35);
map.put("married", true);
String json = defaultJsonb.toJson(map);
JsonObject jobj = Json.createReader(new StringReader(json)).read().asJsonObject();
assertEquals("John SMith", jobj.getString("name"));
assertEquals(35, jobj.getInt("age"));
assertTrue(jobj.getBoolean("married"));
}

@Test
Expand Down Expand Up @@ -876,52 +874,40 @@ public void testBoxToArrayWithLambda() {
}

@Test
public void testCustomSerializersInContainer() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {

Container expected = new Container(List.of(new Containee("k", "v")));
public void testCustomSerializersInContainer() {
Container expected = new Container(List.of(new Containee("k", "v")));

String expectedJson = jsonb.toJson(expected);
System.out.println(expectedJson);
String expectedJson = defaultJsonb.toJson(expected);
System.out.println(expectedJson);

assertEquals(expected, jsonb.fromJson(expectedJson, Container.class));
}
assertEquals(expected, defaultJsonb.fromJson(expectedJson, Container.class));
}

@Test
public void testCustomSerializersAndDeserializersInEnum() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
public void testCustomSerializersAndDeserializersInEnum() {
Colors expected = Colors.GREEN;

Colors expected = Colors.GREEN;
String expectedJson = defaultJsonb.toJson(expected);

String expectedJson = jsonb.toJson(expected);

assertEquals(expected, jsonb.fromJson(expectedJson, Colors.class));
}
assertEquals(expected, defaultJsonb.fromJson(expectedJson, Colors.class));
}

@Test
public void testJsonbPropertyInEnum() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
public void testJsonbPropertyInEnum() {
Cars expected = Cars.FORD;

Cars expected = Cars.FORD;
String expectedJson = defaultJsonb.toJson(expected);

String expectedJson = jsonb.toJson(expected);

assertEquals(expected, jsonb.fromJson(expectedJson, Cars.class));
}
assertEquals(expected, defaultJsonb.fromJson(expectedJson, Cars.class));
}

@Test
public void testNoJsonbPropertyInEnum() throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
public void testNoJsonbPropertyInEnum() {
Cars expected = Cars.FIAT;

Cars expected = Cars.FIAT;
String expectedJson = defaultJsonb.toJson(expected);

String expectedJson = jsonb.toJson(expected);

assertEquals(expected, jsonb.fromJson(expectedJson, Cars.class));
}
assertEquals(expected, defaultJsonb.fromJson(expectedJson, Cars.class));
}

}

0 comments on commit 062a161

Please sign in to comment.