|
10 | 10 |
|
11 | 11 | import java.time.LocalDate;
|
12 | 12 | import java.time.LocalDateTime;
|
| 13 | +import java.util.Objects; |
13 | 14 |
|
14 | 15 | import static org.junit.Assert.assertEquals;
|
15 | 16 |
|
@@ -51,20 +52,29 @@ private static final class Container {
|
51 | 52 | @B2Json.ignored
|
52 | 53 | public int c;
|
53 | 54 |
|
54 |
| - @B2Json.constructor(params = "a, b") |
55 |
| - public Container(int a, String b) { |
| 55 | + @B2Json.optionalWithDefault(defaultValue = "0") |
| 56 | + public int d; |
| 57 | + |
| 58 | + @B2Json.constructor(params = "a, b, d") |
| 59 | + public Container(int a, String b, int d) { |
56 | 60 | this.a = a;
|
57 | 61 | this.b = b;
|
58 | 62 | this.c = 5;
|
| 63 | + this.d = d; |
59 | 64 | }
|
60 | 65 |
|
61 | 66 | @Override
|
62 | 67 | public boolean equals(Object o) {
|
63 |
| - if (!(o instanceof Container)) { |
64 |
| - return false; |
65 |
| - } |
66 |
| - Container other = (Container) o; |
67 |
| - return a == other.a && (b == null ? other.b == null : b.equals(other.b)); |
| 68 | + System.out.println("Entered overridden equals() method to check now..."); |
| 69 | + if (this == o) return true; |
| 70 | + if (o == null || getClass() != o.getClass()) return false; |
| 71 | + Container container = (Container) o; |
| 72 | + return a == container.a && d == container.d && Objects.equals(b, container.b); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int hashCode() { |
| 77 | + return Objects.hash(a, b, c, d); |
68 | 78 | }
|
69 | 79 | }
|
70 | 80 |
|
@@ -169,18 +179,50 @@ public TestResponse(String str, String message, String reason, boolean succeeded
|
169 | 179 |
|
170 | 180 | @Test
|
171 | 181 | public void testObject() throws B2JsonException {
|
172 |
| - String json = |
173 |
| - "{\n" + |
174 |
| - " \"a\": 41,\n" + |
175 |
| - " \"b\": \"hello\"\n" + |
176 |
| - "}"; |
177 |
| - Container obj = new Container(41, "hello"); |
| 182 | + String json = "{\n" + |
| 183 | + " \"a\": 41,\n" + |
| 184 | + " \"b\": \"hello\",\n" + |
| 185 | + " \"d\": 15\n" + |
| 186 | + "}"; |
| 187 | + Container obj = new Container(41, "hello", 15); |
178 | 188 | assertEquals(json, b2Json.toJson(obj));
|
179 | 189 | System.out.println("obj is: " + obj);
|
180 | 190 | System.out.println("json is: " + json);
|
181 | 191 | assertEquals(obj, b2Json.fromJson(json, Container.class));
|
182 | 192 | }
|
183 | 193 |
|
| 194 | + @Test |
| 195 | + public void testObjectUsingDefaultValue() throws B2JsonException { |
| 196 | + //in this json string, there's no field d which is an optional field with a default value |
| 197 | + String json = "{\n" + |
| 198 | + " \"a\": 2023,\n" + |
| 199 | + " \"b\": \"hello\"\n" + |
| 200 | + "}"; |
| 201 | + System.out.println("json is: " + json); |
| 202 | + Container fromB2Json = b2Json.fromJson(json, Container.class); |
| 203 | + System.out.println("b2Json.fromJson(json, Container.class) is: " + fromB2Json); |
| 204 | + Container fromGson = gson.fromJson(json, Container.class); |
| 205 | + System.out.println("gson.fromJson(json) is: " + fromGson); |
| 206 | + System.out.println("about to check the equality between Gson and B2Json results.."); |
| 207 | + assertEquals(fromGson, fromB2Json); |
| 208 | + String fromB2JsonString = b2Json.toJson(fromB2Json); |
| 209 | + String fromGsonString = gson.toJson(fromB2Json); |
| 210 | + String expectedFromGson = "{\n" + |
| 211 | + " \"a\": 2023,\n" + |
| 212 | + " \"b\": \"hello\",\n" + |
| 213 | + " \"c\": 5,\n" + |
| 214 | + " \"d\": 0\n" + |
| 215 | + "}"; |
| 216 | + assertEquals(expectedFromGson, fromGsonString); |
| 217 | + //there's no field c as it's annotated by B2Json.ignored |
| 218 | + String expectedFromB2Json = "{\n" + |
| 219 | + " \"a\": 2023,\n" + |
| 220 | + " \"b\": \"hello\",\n" + |
| 221 | + " \"d\": 0\n" + |
| 222 | + "}"; |
| 223 | + assertEquals(expectedFromB2Json, fromB2JsonString); |
| 224 | + } |
| 225 | + |
184 | 226 | @Test
|
185 | 227 | public void testRequestUsingB2Json() throws B2JsonException, JSONException {
|
186 | 228 | //B2Json always reorders the fields in the object in alphabetical order
|
|
0 commit comments