|
| 1 | +package com.baeldung.jsonb; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.time.LocalDate; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import javax.json.bind.Jsonb; |
| 12 | +import javax.json.bind.JsonbBuilder; |
| 13 | +import javax.json.bind.JsonbConfig; |
| 14 | +import javax.json.bind.config.PropertyNamingStrategy; |
| 15 | +import javax.json.bind.config.PropertyOrderStrategy; |
| 16 | + |
| 17 | +import org.apache.commons.collections4.ListUtils; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +public class JsonbTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() { |
| 24 | + Jsonb jsonb = JsonbBuilder.create(); |
| 25 | + // @formatter:off |
| 26 | + List<Person> personList = Arrays.asList( |
| 27 | + new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), |
| 28 | + new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), |
| 29 | + new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), |
| 30 | + new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500))); |
| 31 | + // @formatter:on |
| 32 | + String jsonArrayPerson = jsonb.toJson(personList); |
| 33 | + // @formatter:off |
| 34 | + String jsonExpected = "[" + |
| 35 | + "{\"email\":\"jhon@test.com\"," + |
| 36 | + "\"id\":1,\"person-name\":\"Jhon\"," + |
| 37 | + "\"registeredDate\":\"09-09-2019\"," + |
| 38 | + "\"salary\":\"1000.0\"}," + |
| 39 | + "{\"email\":\"jhon1@test.com\"," + |
| 40 | + "\"id\":2,\"person-name\":\"Jhon\"," + |
| 41 | + "\"registeredDate\":\"09-09-2019\"," + |
| 42 | + "\"salary\":\"1500.0\"},{\"email\":null," + |
| 43 | + "\"id\":3,\"person-name\":\"Jhon\"," + |
| 44 | + "\"registeredDate\":\"09-09-2019\"," + |
| 45 | + "\"salary\":\"1000.0\"}," + |
| 46 | + "{\"email\":\"tom@test.com\"," + |
| 47 | + "\"id\":4,\"person-name\":\"Tom\"," + |
| 48 | + "\"registeredDate\":\"09-09-2019\"," + |
| 49 | + "\"salary\":\"1500.0\"}" |
| 50 | + + "]"; |
| 51 | + // @formatter:on |
| 52 | + assertTrue(jsonArrayPerson.equals(jsonExpected)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenPersonJsonArray_whenDeserializeWithJsonb_thenGetPersonList() { |
| 57 | + Jsonb jsonb = JsonbBuilder.create(); |
| 58 | + // @formatter:off |
| 59 | + String personJsonArray = |
| 60 | + "[" + |
| 61 | + "{\"email\":\"jhon@test.com\"," + |
| 62 | + "\"id\":1,\"person-name\":\"Jhon\"," + |
| 63 | + "\"registeredDate\":\"09-09-2019\"," + |
| 64 | + "\"salary\":\"1000.0\"}," + |
| 65 | + "{\"email\":\"jhon1@test.com\"," + |
| 66 | + "\"id\":2,\"person-name\":\"Jhon\"," + |
| 67 | + "\"registeredDate\":\"09-09-2019\"," + |
| 68 | + "\"salary\":\"1500.0\"},{\"email\":null," + |
| 69 | + "\"id\":3,\"person-name\":\"Jhon\"," + |
| 70 | + "\"registeredDate\":\"09-09-2019\"," + |
| 71 | + "\"salary\":\"1000.0\"}," + |
| 72 | + "{\"email\":\"tom@test.com\"," + |
| 73 | + "\"id\":4,\"person-name\":\"Tom\"," + |
| 74 | + "\"registeredDate\":\"09-09-2019\"," + |
| 75 | + "\"salary\":\"1500.0\"}" |
| 76 | + + "]"; |
| 77 | + // @formatter:on |
| 78 | + @SuppressWarnings("serial") |
| 79 | + List<Person> personList = jsonb.fromJson(personJsonArray, new ArrayList<Person>() { |
| 80 | + }.getClass() |
| 81 | + .getGenericSuperclass()); |
| 82 | + // @formatter:off |
| 83 | + List<Person> personListExpected = Arrays.asList( |
| 84 | + new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), |
| 85 | + new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), |
| 86 | + new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), |
| 87 | + new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500))); |
| 88 | + // @formatter:on |
| 89 | + assertTrue(ListUtils.isEqualList(personList, personListExpected)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() { |
| 94 | + JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES); |
| 95 | + Jsonb jsonb = JsonbBuilder.create(config); |
| 96 | + Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); |
| 97 | + String jsonPerson = jsonb.toJson(person); |
| 98 | + // @formatter:off |
| 99 | + String jsonExpected = |
| 100 | + "{\"email\":\"jhon@test.com\"," + |
| 101 | + "\"id\":1," + |
| 102 | + "\"person-name\":\"Jhon\"," + |
| 103 | + "\"registered_date\":\"07-09-2019\"," + |
| 104 | + "\"salary\":\"1000.0\"}"; |
| 105 | + // @formatter:on |
| 106 | + assertTrue(jsonExpected.equals(jsonPerson)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson() { |
| 111 | + JsonbConfig config = new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE); |
| 112 | + Jsonb jsonb = JsonbBuilder.create(config); |
| 113 | + Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); |
| 114 | + String jsonPerson = jsonb.toJson(person); |
| 115 | + // @formatter:off |
| 116 | + String jsonExpected = |
| 117 | + "{\"salary\":\"1000.0\","+ |
| 118 | + "\"registeredDate\":\"07-09-2019\"," + |
| 119 | + "\"person-name\":\"Jhon\"," + |
| 120 | + "\"id\":1," + |
| 121 | + "\"email\":\"jhon@test.com\"}"; |
| 122 | + // @formatter:on |
| 123 | + assertTrue(jsonExpected.equals(jsonPerson)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() { |
| 128 | + Jsonb jsonb = JsonbBuilder.create(); |
| 129 | + Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); |
| 130 | + String jsonPerson = jsonb.toJson(person); |
| 131 | + // @formatter:off |
| 132 | + String jsonExpected = |
| 133 | + "{\"email\":\"jhon@test.com\"," + |
| 134 | + "\"id\":1," + |
| 135 | + "\"person-name\":\"Jhon\"," + |
| 136 | + "\"registeredDate\":\"07-09-2019\"," + |
| 137 | + "\"salary\":\"1000.0\"}"; |
| 138 | + // @formatter:on |
| 139 | + assertTrue(jsonExpected.equals(jsonPerson)); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() { |
| 144 | + Jsonb jsonb = JsonbBuilder.create(); |
| 145 | + Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); |
| 146 | + // @formatter:off |
| 147 | + String jsonPerson = |
| 148 | + "{\"email\":\"jhon@test.com\"," + |
| 149 | + "\"id\":1," + |
| 150 | + "\"person-name\":\"Jhon\"," + |
| 151 | + "\"registeredDate\":\"07-09-2019\"," + |
| 152 | + "\"salary\":\"1000.0\"}"; |
| 153 | + // @formatter:on |
| 154 | + assertTrue(jsonb.fromJson(jsonPerson, Person.class) |
| 155 | + .equals(person)); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments