use of com.fasterxml.jackson.dataformat.smile.databind.SmileMapper in project jackson-dataformats-binary by FasterXML.
the class StreamingDecoratorsTest method testOutputDecorators.
public void testOutputDecorators() throws Exception {
final byte[] DOC = _smileDoc(" 137");
// important! Do not add document header for this test
final SmileFactory streamF = smileFactoryBuilder(false, false, false).outputDecorator(new PrefixOutputDecorator(DOC)).build();
final SmileMapper mapper = SmileMapper.builder(streamF).build();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator g = mapper.createGenerator(bytes);
g.writeString("foo");
g.close();
JsonParser p = mapper.createParser(bytes.toByteArray());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(137, p.getIntValue());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("foo", p.getText());
assertNull(p.nextToken());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.databind.SmileMapper in project jackson-dataformats-binary by FasterXML.
the class StreamingDecoratorsTest method testInputDecorators.
public void testInputDecorators() throws Exception {
final byte[] DOC = _smileDoc("42 37");
final SmileFactory streamF = smileFactoryBuilder(false, true, false).inputDecorator(new PrefixInputDecorator(DOC)).build();
final SmileMapper mapper = SmileMapper.builder(streamF).build();
JsonParser p = mapper.createParser(new byte[0], 0, 0);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(42, p.getIntValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(37, p.getIntValue());
assertNull(p.nextToken());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.databind.SmileMapper in project jackson-dataformats-binary by FasterXML.
the class JDKSerializabilityTest method testApacheMapperWithModule.
public void testApacheMapperWithModule() throws Exception {
// very simple validation: should still work wrt serialization
SmileMapper unfrozenMapper = serializeAndDeserialize(new SmileMapper());
// and then simple verification that write+read still works
Object input = _simpleData();
byte[] encoded = unfrozenMapper.writeValueAsBytes(input);
final Object result = unfrozenMapper.readerFor(Object.class).readValue(encoded);
assertEquals(result, input);
// and also verify `rebuild()` works:
SmileMapper copy = unfrozenMapper.rebuild().build();
assertNotSame(unfrozenMapper, copy);
// with 3.x, factories are immutable so they need not be unshared:
assertSame(unfrozenMapper.tokenStreamFactory(), copy.tokenStreamFactory());
final Object result2 = copy.readerFor(Object.class).readValue(encoded);
assertEquals(input, result2);
}
use of com.fasterxml.jackson.dataformat.smile.databind.SmileMapper in project jackson-dataformats-binary by FasterXML.
the class MapperSimpleReadTest method testStreamingFeaturesViaMapper.
/*
/**********************************************************
/* Tests for [dataformats-binary#301]
/**********************************************************
*/
public void testStreamingFeaturesViaMapper() throws Exception {
SmileMapper mapperWithHeaders = SmileMapper.builder().enable(SmileGenerator.Feature.WRITE_HEADER).enable(SmileParser.Feature.REQUIRE_HEADER).build();
byte[] encodedWithHeader = mapperWithHeaders.writeValueAsBytes("foo");
assertEquals(8, encodedWithHeader.length);
SmileMapper mapperNoHeaders = SmileMapper.builder().disable(SmileGenerator.Feature.WRITE_HEADER).disable(SmileParser.Feature.REQUIRE_HEADER).build();
byte[] encodedNoHeader = mapperNoHeaders.writeValueAsBytes("foo");
assertEquals(4, encodedNoHeader.length);
// And then see that we can parse; with header always
assertEquals("foo", mapperWithHeaders.readValue(encodedWithHeader, Object.class));
assertEquals("foo", mapperNoHeaders.readValue(encodedWithHeader, Object.class));
// without if not required
assertEquals("foo", mapperNoHeaders.readValue(encodedNoHeader, Object.class));
// But the reverse will fail
try {
mapperWithHeaders.readValue(encodedNoHeader, Object.class);
fail("Should not pass");
} catch (StreamReadException e) {
verifyException(e, "Input does not start with Smile format header");
}
}
use of com.fasterxml.jackson.dataformat.smile.databind.SmileMapper in project jackson-dataformats-binary by FasterXML.
the class SymbolTableTest method testSimpleDefault.
public void testSimpleDefault() throws Exception {
final SmileMapper vanillaMapper = smileMapper();
final byte[] doc = _smileDoc("{\"a\":1,\"b\":2}");
// First: should have empty symbol table
try (JsonParser p = vanillaMapper.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
// added in 2.13
assertTrue(syms.isCanonicalizing());
assertEquals(0, syms.size());
assertEquals(0, _findParent(syms).size());
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
assertEquals("a", p.currentName());
assertEquals(1, syms.size());
// not yet synced to parent
assertEquals(0, _findParent(syms).size());
while (p.nextToken() != null) {
;
}
assertEquals(2, syms.size());
// but after closing, should sync
assertEquals(2, _findParent(syms).size());
}
// by default, should canonicalize etc:
try (JsonParser p = vanillaMapper.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertEquals(2, syms.size());
// also check that parent (root) has it all?
assertEquals(2, _findParent(syms).size());
// but no additions second time around
while (p.nextToken() != null) {
;
}
assertEquals(2, syms.size());
}
// yet may get more added
final byte[] doc2 = _smileDoc("{\"a\":1,\"foo\":2}");
try (JsonParser p = vanillaMapper.createParser(doc2)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertEquals(2, syms.size());
vanillaMapper.readValue(p, Object.class);
syms = _findSymbols(p);
assertEquals(3, syms.size());
}
// and verify it gets reflected too
try (JsonParser p = vanillaMapper.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertEquals(3, syms.size());
}
}
Aggregations