|
| 1 | +package com.fasterxml.jackson.dataformat.smile.parse; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonFactory; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.core.JsonToken; |
| 8 | +import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile; |
| 11 | +import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 12 | +import com.fasterxml.jackson.dataformat.smile.SmileParserBase; |
| 13 | +import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper; |
| 14 | + |
| 15 | +public class SymbolTableTest extends BaseTestForSmile |
| 16 | +{ |
| 17 | + public void testSimpleDefault() throws Exception |
| 18 | + { |
| 19 | + final SmileMapper vanillaMapper = smileMapper(); |
| 20 | + final byte[] doc = _smileDoc("{\"a\":1,\"b\":2}"); |
| 21 | + |
| 22 | + // First: should have empty symbol table |
| 23 | + try (JsonParser p = vanillaMapper.createParser(doc)) { |
| 24 | + ByteQuadsCanonicalizer syms = _findSymbols(p); |
| 25 | + assertEquals(0, syms.size()); |
| 26 | + assertEquals(0, _findParent(syms).size()); |
| 27 | + |
| 28 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 29 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 30 | + assertEquals("a", p.currentName()); |
| 31 | + assertEquals(1, syms.size()); |
| 32 | + // not yet synced to parent |
| 33 | + assertEquals(0, _findParent(syms).size()); |
| 34 | + |
| 35 | + while (p.nextToken() != null) { ; } |
| 36 | + assertEquals(2, syms.size()); |
| 37 | + // but after closing, should sync |
| 38 | + assertEquals(2, _findParent(syms).size()); |
| 39 | + } |
| 40 | + |
| 41 | + // by default, should canonicalize etc: |
| 42 | + try (JsonParser p = vanillaMapper.createParser(doc)) { |
| 43 | + ByteQuadsCanonicalizer syms = _findSymbols(p); |
| 44 | + assertEquals(2, syms.size()); |
| 45 | + // also check that parent (root) has it all? |
| 46 | + assertEquals(2, _findParent(syms).size()); |
| 47 | + |
| 48 | + // but no additions second time around |
| 49 | + while (p.nextToken() != null) { ; } |
| 50 | + assertEquals(2, syms.size()); |
| 51 | + } |
| 52 | + |
| 53 | + // yet may get more added |
| 54 | + final byte[] doc2 = _smileDoc("{\"a\":1,\"foo\":2}"); |
| 55 | + try (JsonParser p = vanillaMapper.createParser(doc2)) { |
| 56 | + ByteQuadsCanonicalizer syms = _findSymbols(p); |
| 57 | + assertEquals(2, syms.size()); |
| 58 | + vanillaMapper.readValue(p, Object.class); |
| 59 | + syms = _findSymbols(p); |
| 60 | + assertEquals(3, syms.size()); |
| 61 | + } |
| 62 | + |
| 63 | + // and verify it gets reflected too |
| 64 | + try (JsonParser p = vanillaMapper.createParser(doc)) { |
| 65 | + ByteQuadsCanonicalizer syms = _findSymbols(p); |
| 66 | + assertEquals(3, syms.size()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // !!! TODO: |
| 71 | + // [dataformats-binary#252]: should be able to prevent canonicalization |
| 72 | + public void testSimpleNoCanonicalize() throws Exception |
| 73 | + { |
| 74 | + final byte[] doc = _smileDoc("{\"a\":1,\"b\":2}"); |
| 75 | + final SmileMapper mapper = SmileMapper.builder(SmileFactory.builder() |
| 76 | + .disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES) |
| 77 | + .build()) |
| 78 | + .build(); |
| 79 | + |
| 80 | + try (JsonParser p = mapper.createParser(doc)) { |
| 81 | + ByteQuadsCanonicalizer syms = _findSymbols(p); |
| 82 | + assertEquals(0, syms.size()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Helper method to dig up symbol table reference for tests, without |
| 87 | + // exposing it to user code |
| 88 | + private ByteQuadsCanonicalizer _findSymbols(JsonParser p) throws Exception |
| 89 | + { |
| 90 | + Field f = SmileParserBase.class.getDeclaredField("_symbols"); |
| 91 | + f.setAccessible(true); |
| 92 | + return (ByteQuadsCanonicalizer) f.get(p); |
| 93 | + } |
| 94 | + |
| 95 | + private ByteQuadsCanonicalizer _findParent(ByteQuadsCanonicalizer sym) throws Exception |
| 96 | + { |
| 97 | + Field f = ByteQuadsCanonicalizer.class.getDeclaredField("_parent"); |
| 98 | + f.setAccessible(true); |
| 99 | + return (ByteQuadsCanonicalizer) f.get(sym); |
| 100 | + } |
| 101 | +} |
0 commit comments