Skip to content

Commit

Permalink
First part of #301 implementation, for Smile
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 19, 2021
1 parent b0586a7 commit df07ca2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.fasterxml.jackson.dataformat.smile.databind;

import com.fasterxml.jackson.core.Version;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;

import com.fasterxml.jackson.dataformat.smile.PackageVersion;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileParser;

/**
* Specialized {@link ObjectMapper} to use with CBOR format backend.
Expand All @@ -17,14 +21,89 @@ public class SmileMapper extends ObjectMapper

/**
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
* CBOR backend.
* Smile backend.
*
* @since 2.10
*/
public static class Builder extends MapperBuilder<SmileMapper, Builder>
{
protected final SmileFactory _streamFactory; // since 2.14

public Builder(SmileMapper m) {
super(m);
_streamFactory = m.getFactory();
}

/*
/******************************************************************
/* Format features
/******************************************************************
*/

/**
* @since 2.14
*/
public Builder enable(SmileParser.Feature... features) {
for (SmileParser.Feature f : features) {
_streamFactory.enable(f);
}
return this;
}

/**
* @since 2.14
*/
public Builder disable(SmileParser.Feature... features) {
for (SmileParser.Feature f : features) {
_streamFactory.disable(f);
}
return this;
}

/**
* @since 2.14
*/
public Builder configure(SmileParser.Feature f, boolean state)
{
if (state) {
_streamFactory.enable(f);
} else {
_streamFactory.disable(f);
}
return this;
}

/**
* @since 2.14
*/
public Builder enable(SmileGenerator.Feature... features) {
for (SmileGenerator.Feature f : features) {
_streamFactory.enable(f);
}
return this;
}

/**
* @since 2.14
*/
public Builder disable(SmileGenerator.Feature... features) {
for (SmileGenerator.Feature f : features) {
_streamFactory.disable(f);
}
return this;
}

/**
* @since 2.14
*/
public Builder configure(SmileGenerator.Feature f, boolean state)
{
if (state) {
_streamFactory.enable(f);
} else {
_streamFactory.disable(f);
}
return this;
}
}

Expand All @@ -46,7 +125,6 @@ protected SmileMapper(SmileMapper src) {
super(src);
}

@SuppressWarnings("unchecked")
public static SmileMapper.Builder builder() {
return new Builder(new SmileMapper());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileParser;
import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;

public class SmileMapperTest extends BaseTestForSmile
{
Expand Down Expand Up @@ -168,4 +172,42 @@ private byte[] _generateHugeDoc(SmileFactory f) throws IOException
g.close();
return b.toByteArray();
}

/*
/**********************************************************
/* 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");
}
}
}

0 comments on commit df07ca2

Please sign in to comment.