|
| 1 | +package io.quarkus.bootstrap.json; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +class JsonDeserializerTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void testSimpleObject() { |
| 16 | + JsonObject obj = JsonReader.of("{\"name\":\"John\",\"age\":30}").read(); |
| 17 | + assertNotNull(obj); |
| 18 | + assertEquals("John", ((JsonString) obj.get("name")).value()); |
| 19 | + assertEquals(30L, ((JsonInteger) obj.get("age")).longValue()); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void testSimpleArray() { |
| 24 | + JsonArray arr = JsonReader.of("[\"apple\",\"banana\",\"cherry\"]").read(); |
| 25 | + assertNotNull(arr); |
| 26 | + assertEquals(3, arr.size()); |
| 27 | + assertEquals("apple", ((JsonString) arr.value().get(0)).value()); |
| 28 | + assertEquals("banana", ((JsonString) arr.value().get(1)).value()); |
| 29 | + assertEquals("cherry", ((JsonString) arr.value().get(2)).value()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testNestedObjects() { |
| 34 | + JsonObject obj = JsonReader. of( "{\"user\":{\"name\":\"Alice\",\"email\":\"[email protected]\"},\"active\":true}") |
| 35 | + .read(); |
| 36 | + assertNotNull(obj); |
| 37 | + JsonObject user = (JsonObject) obj.get("user"); |
| 38 | + assertEquals("Alice", ((JsonString) user.get("name")).value()); |
| 39 | + assertEquals( "[email protected]", (( JsonString) user. get( "email")). value()); |
| 40 | + assertTrue(((JsonBoolean) obj.get("active")).value()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testNestedArrays() { |
| 45 | + JsonArray arr = JsonReader.of("[[1,2],[3,4]]").read(); |
| 46 | + assertNotNull(arr); |
| 47 | + assertEquals(2, arr.size()); |
| 48 | + JsonArray first = (JsonArray) arr.value().get(0); |
| 49 | + JsonArray second = (JsonArray) arr.value().get(1); |
| 50 | + assertEquals(1L, ((JsonInteger) first.value().get(0)).longValue()); |
| 51 | + assertEquals(2L, ((JsonInteger) first.value().get(1)).longValue()); |
| 52 | + assertEquals(3L, ((JsonInteger) second.value().get(0)).longValue()); |
| 53 | + assertEquals(4L, ((JsonInteger) second.value().get(1)).longValue()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testMixedTypes() { |
| 58 | + JsonObject obj = JsonReader |
| 59 | + .of("{\"string\":\"value\",\"integer\":42,\"long\":9876543210,\"boolean\":false}").read(); |
| 60 | + assertNotNull(obj); |
| 61 | + assertEquals("value", ((JsonString) obj.get("string")).value()); |
| 62 | + assertEquals(42L, ((JsonInteger) obj.get("integer")).longValue()); |
| 63 | + assertEquals(9876543210L, ((JsonInteger) obj.get("long")).longValue()); |
| 64 | + assertFalse(((JsonBoolean) obj.get("boolean")).value()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testEmptyObject() { |
| 69 | + JsonObject obj = JsonReader.of("{}").read(); |
| 70 | + assertNotNull(obj); |
| 71 | + assertEquals(0, obj.members().size()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testEmptyArray() { |
| 76 | + JsonArray arr = JsonReader.of("[]").read(); |
| 77 | + assertNotNull(arr); |
| 78 | + assertEquals(0, arr.size()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testStringEscapingQuotes() { |
| 83 | + JsonObject obj = JsonReader.of("{\"message\":\"He said \\\"Hello\\\"\"}").read(); |
| 84 | + assertEquals("He said \"Hello\"", ((JsonString) obj.get("message")).value()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testStringEscapingBackslash() { |
| 89 | + JsonObject obj = JsonReader.of("{\"path\":\"C:\\\\Users\\\\John\"}").read(); |
| 90 | + assertEquals("C:\\Users\\John", ((JsonString) obj.get("path")).value()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testStringEscapingControlCharacters() { |
| 95 | + JsonObject obj = JsonReader.of("{\"text\":\"Line1\\nLine2\\tTabbed\\rReturn\"}").read(); |
| 96 | + assertEquals("Line1\nLine2\tTabbed\rReturn", ((JsonString) obj.get("text")).value()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testStringEscapingUnicodeSequences() { |
| 101 | + JsonObject obj = JsonReader.of("{\"text\":\"\\u0048\\u0065\\u006c\\u006c\\u006f\"}").read(); |
| 102 | + assertEquals("Hello", ((JsonString) obj.get("text")).value()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testStringEscapingAllControlCharacters() { |
| 107 | + String json = "{\"controls\":\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" + |
| 108 | + "\\u0008\\u0009\\u000a\\u000b\\u000c\\u000d\\u000e\\u000f" + |
| 109 | + "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" + |
| 110 | + "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\"}"; |
| 111 | + JsonObject obj = JsonReader.of(json).read(); |
| 112 | + String value = ((JsonString) obj.get("controls")).value(); |
| 113 | + assertEquals(32, value.length()); |
| 114 | + for (int i = 0; i <= 0x1f; i++) { |
| 115 | + assertEquals((char) i, value.charAt(i)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testStringWithSpecialCharacters() { |
| 121 | + JsonObject obj = JsonReader.of("{\"special\":\"äöü ñ €\"}").read(); |
| 122 | + assertEquals("äöü ñ €", ((JsonString) obj.get("special")).value()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testEmptyString() { |
| 127 | + JsonObject obj = JsonReader.of("{\"empty\":\"\"}").read(); |
| 128 | + assertEquals("", ((JsonString) obj.get("empty")).value()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testArrayWithMixedTypes() { |
| 133 | + JsonArray arr = JsonReader.of("[\"text\",123,true,{\"key\":\"value\"}]").read(); |
| 134 | + assertEquals(4, arr.size()); |
| 135 | + assertEquals("text", ((JsonString) arr.value().get(0)).value()); |
| 136 | + assertEquals(123L, ((JsonInteger) arr.value().get(1)).longValue()); |
| 137 | + assertTrue(((JsonBoolean) arr.value().get(2)).value()); |
| 138 | + JsonObject obj = (JsonObject) arr.value().get(3); |
| 139 | + assertEquals("value", ((JsonString) obj.get("key")).value()); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void testComplexNestedStructure() { |
| 144 | + JsonObject obj = JsonReader |
| 145 | + .of("{\"users\":[{\"id\":1,\"name\":\"Alice\"},{\"id\":2,\"name\":\"Bob\"}],\"count\":2}").read(); |
| 146 | + JsonArray users = (JsonArray) obj.get("users"); |
| 147 | + assertEquals(2, users.size()); |
| 148 | + JsonObject alice = (JsonObject) users.value().get(0); |
| 149 | + assertEquals(1L, ((JsonInteger) alice.get("id")).longValue()); |
| 150 | + assertEquals("Alice", ((JsonString) alice.get("name")).value()); |
| 151 | + JsonObject bob = (JsonObject) users.value().get(1); |
| 152 | + assertEquals(2L, ((JsonInteger) bob.get("id")).longValue()); |
| 153 | + assertEquals("Bob", ((JsonString) bob.get("name")).value()); |
| 154 | + assertEquals(2L, ((JsonInteger) obj.get("count")).longValue()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testWhitespaceHandling() { |
| 159 | + JsonObject obj = JsonReader.of(" { \"name\" : \"John\" , \"age\" : 30 } ").read(); |
| 160 | + assertEquals("John", ((JsonString) obj.get("name")).value()); |
| 161 | + assertEquals(30L, ((JsonInteger) obj.get("age")).longValue()); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testNullValue() { |
| 166 | + JsonObject obj = JsonReader.of("{\"value\":null}").read(); |
| 167 | + assertInstanceOf(JsonNull.class, obj.get("value")); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testBooleanValues() { |
| 172 | + JsonObject obj = JsonReader.of("{\"trueVal\":true,\"falseVal\":false}").read(); |
| 173 | + assertTrue(((JsonBoolean) obj.get("trueVal")).value()); |
| 174 | + assertFalse(((JsonBoolean) obj.get("falseVal")).value()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void testNumberFormats() { |
| 179 | + JsonObject obj = JsonReader.of("{\"int\":42,\"negative\":-17,\"zero\":0,\"decimal\":3.14,\"exp\":1.0e10}") |
| 180 | + .read(); |
| 181 | + assertEquals(42L, ((JsonInteger) obj.get("int")).longValue()); |
| 182 | + assertEquals(-17L, ((JsonInteger) obj.get("negative")).longValue()); |
| 183 | + assertEquals(0L, ((JsonInteger) obj.get("zero")).longValue()); |
| 184 | + assertEquals(3.14, ((JsonDouble) obj.get("decimal")).value(), 0.001); |
| 185 | + assertEquals(1.0e10, ((JsonDouble) obj.get("exp")).value(), 0.001); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void testLargeNumbers() { |
| 190 | + JsonObject obj = JsonReader |
| 191 | + .of("{\"maxInt\":2147483647,\"minInt\":-2147483648,\"maxLong\":9223372036854775807,\"minLong\":-9223372036854775808}") |
| 192 | + .read(); |
| 193 | + assertEquals(Integer.MAX_VALUE, ((JsonInteger) obj.get("maxInt")).longValue()); |
| 194 | + assertEquals(Integer.MIN_VALUE, ((JsonInteger) obj.get("minInt")).longValue()); |
| 195 | + assertEquals(Long.MAX_VALUE, ((JsonInteger) obj.get("maxLong")).longValue()); |
| 196 | + assertEquals(Long.MIN_VALUE, ((JsonInteger) obj.get("minLong")).longValue()); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + void testStringWithQuotesAndBackslashes() { |
| 201 | + JsonObject obj = JsonReader.of("{\"complex\":\"\\\"\\\\\\\"\\\\\\\\\\\"\"}").read(); |
| 202 | + assertEquals("\"\\\"\\\\\"", ((JsonString) obj.get("complex")).value()); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + void testInvalidJsonMissingClosingBrace() { |
| 207 | + assertThrows(IllegalArgumentException.class, () -> JsonReader.of("{\"name\":\"John\"").read()); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + void testInvalidJsonMissingClosingBracket() { |
| 212 | + assertThrows(IllegalArgumentException.class, () -> JsonReader.of("[1,2,3").read()); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + void testInvalidJsonMissingColon() { |
| 217 | + assertThrows(IllegalArgumentException.class, () -> JsonReader.of("{\"name\" \"John\"}").read()); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + void testInvalidJsonUnquotedString() { |
| 222 | + assertThrows(IllegalArgumentException.class, () -> JsonReader.of("{\"name\":John}").read()); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void testInvalidJsonControlCharacterInString() { |
| 227 | + assertThrows(IllegalArgumentException.class, () -> JsonReader.of("{\"text\":\"Line1\nLine2\"}").read()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + void testStringWithForwardSlashEscape() { |
| 232 | + JsonObject obj = JsonReader.of("{\"url\":\"https:\\/\\/example.com\"}").read(); |
| 233 | + assertEquals("https://example.com", ((JsonString) obj.get("url")).value()); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void testStringWithBackspaceAndFormFeed() { |
| 238 | + JsonObject obj = JsonReader.of("{\"text\":\"Hello\\b\\f\"}").read(); |
| 239 | + assertEquals("Hello\b\f", ((JsonString) obj.get("text")).value()); |
| 240 | + } |
| 241 | +} |
0 commit comments