|
1 | 1 | package com.fasterxml.jackson.core.json;
|
2 | 2 |
|
3 | 3 | import java.io.*;
|
| 4 | +import java.nio.charset.StandardCharsets; |
4 | 5 | import java.util.Iterator;
|
5 | 6 |
|
6 | 7 | import com.fasterxml.jackson.core.*;
|
| 8 | +import com.fasterxml.jackson.core.json.async.NonBlockingJsonParser; |
7 | 9 | import com.fasterxml.jackson.core.type.ResolvedType;
|
8 | 10 | import com.fasterxml.jackson.core.type.TypeReference;
|
9 | 11 |
|
@@ -288,4 +290,65 @@ public void testRootValues() throws Exception
|
288 | 290 | g.close();
|
289 | 291 | assertEquals("1/2/3", w.toString());
|
290 | 292 | }
|
| 293 | + |
| 294 | + public void testCanonicalizationEnabled() throws Exception { |
| 295 | + doCanonicalizationTest(false); |
| 296 | + } |
| 297 | + |
| 298 | + public void testCanonicalizationDisabled() throws Exception { |
| 299 | + doCanonicalizationTest(false); |
| 300 | + } |
| 301 | + |
| 302 | + // Configure the JsonFactory as expected, and verify across common shapes of input |
| 303 | + // to cover common JsonParser implementations. |
| 304 | + private void doCanonicalizationTest(boolean canonicalize) throws Exception { |
| 305 | + String contents = "{\"a\":true,\"a\":true}"; |
| 306 | + JsonFactory factory = JsonFactory.builder() |
| 307 | + .configure(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES, canonicalize) |
| 308 | + .build(); |
| 309 | + try (JsonParser parser = factory.createParser(contents)) { |
| 310 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 311 | + } |
| 312 | + try (JsonParser parser = factory.createParser(contents.getBytes(StandardCharsets.UTF_8))) { |
| 313 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 314 | + } |
| 315 | + try (JsonParser parser = factory.createParser( |
| 316 | + new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)))) { |
| 317 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 318 | + } |
| 319 | + try (JsonParser parser = factory.createParser(new StringReader(contents))) { |
| 320 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 321 | + } |
| 322 | + try (JsonParser parser = factory.createParser((DataInput) new DataInputStream( |
| 323 | + new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8))))) { |
| 324 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 325 | + } |
| 326 | + try (NonBlockingJsonParser parser = (NonBlockingJsonParser) factory.createNonBlockingByteArrayParser()) { |
| 327 | + byte[] data = contents.getBytes(StandardCharsets.UTF_8); |
| 328 | + parser.feedInput(data, 0, data.length); |
| 329 | + parser.endOfInput(); |
| 330 | + verifyCanonicalizationTestResult(parser, canonicalize); |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + private void verifyCanonicalizationTestResult(JsonParser parser, boolean canonicalize) throws Exception { |
| 335 | + assertToken(JsonToken.START_OBJECT, parser.nextToken()); |
| 336 | + String field1 = parser.nextFieldName(); |
| 337 | + assertEquals("a", field1); |
| 338 | + assertToken(JsonToken.VALUE_TRUE, parser.nextToken()); |
| 339 | + String field2 = parser.nextFieldName(); |
| 340 | + assertEquals("a", field2); |
| 341 | + if (canonicalize) { |
| 342 | + assertSame(field1, field2); |
| 343 | + } else { |
| 344 | + // n.b. It's possible that this may flake if a garbage collector with string deduplication |
| 345 | + // enabled is used. Such a failure is unlikely because younger GC generations are typically |
| 346 | + // not considered for deduplication due to high churn, but under heavy memory pressure it |
| 347 | + // may be possible. I've left this comment in an attempt to simplify investigation in the |
| 348 | + // off-chance that such flakes eventually occur. |
| 349 | + assertNotSame(field1, field2); |
| 350 | + } |
| 351 | + assertToken(JsonToken.VALUE_TRUE, parser.nextToken()); |
| 352 | + assertToken(JsonToken.END_OBJECT, parser.nextToken()); |
| 353 | + } |
291 | 354 | }
|
0 commit comments