Example usage for com.fasterxml.jackson.core Version Version

List of usage examples for com.fasterxml.jackson.core Version Version

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core Version Version.

Prototype

public Version(int major, int minor, int patchLevel, String snapshotInfo, String groupId, String artifactId) 

Source Link

Usage

From source file:eu.trentorise.opendata.commons.jackson.OdtCommonsModule.java

/**
 * Returns the jackson version for an odt module by reading it from build info at the
 * root of provided class resources./* ww w . j a va 2  s.c  o m*/
 */
public static Version readJacksonVersion(Class clazz) {
    SemVersion semver = SemVersion.of(OdtConfig.of(clazz).getBuildInfo().getVersion());
    return new Version(semver.getMajor(), semver.getMinor(), semver.getPatch(), semver.getPreReleaseVersion(),
            "eu.trentorise.opendata.commons.jackson", "odt-commons-jackson");
}

From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.three.MappingTest3.java

@Test
public void testPolymorphicJacksonSerializationAndDeserialization() {
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer(QueryFilter.class, new QueryFilterDeserializer());

    mapper.registerModule(testModule);//from  w  ww .j  a  v  a 2  s .co  m

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper);

    String testFixture = "/jsonUtils/testdomain/two/queryFilter-realAndLogical2.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter>() {
    });

    // Make sure the hydrated QFilter looks right
    AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter3);
    AssertJUnit.assertEquals(QueryParam.AND, queryFilter.getQueryParam());
    AssertJUnit.assertTrue(queryFilter.isLogical());
    AssertJUnit.assertEquals(3, queryFilter.getFilters().size());
    AssertJUnit.assertNotNull(queryFilter.getFilters().get(QueryParam.OR));

    // Make sure one of the top level RealFilters looks right
    QueryFilter productIdFilter = queryFilter.getFilters().get(QueryParam.PRODUCTID);
    AssertJUnit.assertTrue(productIdFilter.isReal());
    AssertJUnit.assertEquals(QueryParam.PRODUCTID, productIdFilter.getQueryParam());
    AssertJUnit.assertEquals("Acme-1234", productIdFilter.getValue());

    // Make sure the nested OR looks right
    QueryFilter orFilter = queryFilter.getFilters().get(QueryParam.OR);
    AssertJUnit.assertTrue(orFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.OR, orFilter.getQueryParam());
    AssertJUnit.assertEquals(2, orFilter.getFilters().size());

    // Make sure nested AND looks right
    QueryFilter nestedAndFilter = orFilter.getFilters().get(QueryParam.AND);
    AssertJUnit.assertTrue(nestedAndFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.AND, nestedAndFilter.getQueryParam());
    AssertJUnit.assertEquals(2, nestedAndFilter.getFilters().size());

    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString(queryFilter);

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString);
    Map<String, Object> expected = JsonUtils.classpathToMap(testFixture);

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff(expected, actual);
    if (!result.isEmpty()) {
        AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected)
                + "\n  actual: " + JsonUtils.toJsonString(result.actual));
    }
}

From source file:org.mongojack.TestCustomObjectMapper.java

private ObjectMapper createObjectMapper() {
    SimpleModule module = new SimpleModule("MySimpleModule", new Version(1, 0, 0, null, "", ""));
    module.addDeserializer(Custom.class, new JsonDeserializer<Custom>() {
        @Override/* ww  w .  j  av  a  2  s.  c o m*/
        public Custom deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            JsonNode node = jp.readValueAsTree();
            return new Custom(node.get("v1").asText(), node.get("v2").asText());
        }
    });
    module.addSerializer(Custom.class, new JsonSerializer<Custom>() {
        @Override
        public void serialize(Custom value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException {
            jgen.writeStartObject();
            jgen.writeFieldName("v1");
            jgen.writeString(value.value1);
            jgen.writeFieldName("v2");
            jgen.writeString(value.value2);
            jgen.writeEndObject();
        }
    });

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(module);
    MongoJackModule.configure(objectMapper);
    return objectMapper;
}

From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.one.MappingTest1.java

@Test
public void testPolymorphicJacksonSerializationAndDeserialization() {
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer(QueryFilter.class, new QueryFilter1Deserializer());

    mapper.registerModule(testModule);// www .j  a v a 2 s  .  c  om

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper);

    String testFixture = "/jsonUtils/testdomain/one/queryFilter-realAndLogical.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter>() {
    });

    // Make sure the hydrated queryFilter looks right
    AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter1);
    AssertJUnit.assertEquals(QueryParam.AND, queryFilter.getQueryParam());
    AssertJUnit.assertTrue(queryFilter.isLogical());
    AssertJUnit.assertEquals(3, queryFilter.getFilters().size());
    AssertJUnit.assertNotNull(queryFilter.getFilters().get(QueryParam.OR));

    // Make sure one of the top level RealFilters looks right
    QueryFilter productIdFilter = queryFilter.getFilters().get(QueryParam.PRODUCTID);
    AssertJUnit.assertTrue(productIdFilter.isReal());
    AssertJUnit.assertEquals(QueryParam.PRODUCTID, productIdFilter.getQueryParam());
    AssertJUnit.assertEquals("Acme-1234", productIdFilter.getValue());

    // Make sure the nested OR looks right
    QueryFilter orFilter = queryFilter.getFilters().get(QueryParam.OR);
    AssertJUnit.assertTrue(orFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.OR, orFilter.getQueryParam());
    AssertJUnit.assertEquals(2, orFilter.getFilters().size());

    // Make sure nested AND looks right
    QueryFilter nestedAndFilter = orFilter.getFilters().get(QueryParam.AND);
    AssertJUnit.assertTrue(nestedAndFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.AND, nestedAndFilter.getQueryParam());
    AssertJUnit.assertEquals(2, nestedAndFilter.getFilters().size());

    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString(queryFilter);

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString);
    Map<String, Object> expected = JsonUtils.classpathToMap(testFixture);

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff(expected, actual);
    if (!result.isEmpty()) {
        AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected)
                + "\n  actual: " + JsonUtils.toJsonString(result.actual));
    }
}

From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.four.MappingTest4.java

@Test
public void testPolymorphicJacksonSerializationAndDeserialization() {
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer(QueryFilter4.class, new QueryFilter4Deserializer());

    mapper.registerModule(testModule);/*from w  w  w. j  av  a  2 s .  c om*/

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper);

    String testFixture = "/jsonUtils/testdomain/four/queryFilter-realAndLogical4.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter4 queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter4>() {
    });

    // Make sure the hydrated QFilter looks right
    AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter4);
    AssertJUnit.assertEquals(QueryParam.AND, queryFilter.getQueryParam());
    AssertJUnit.assertTrue(queryFilter.isLogical());
    AssertJUnit.assertEquals(3, queryFilter.getFilters().size());
    AssertJUnit.assertNotNull(queryFilter.getFilters().get(QueryParam.OR));

    // Make sure one of the top level RealFilters looks right
    QueryFilter4 productIdFilter = queryFilter.getFilters().get(QueryParam.PRODUCTID);
    AssertJUnit.assertTrue(productIdFilter.isReal());
    AssertJUnit.assertTrue(productIdFilter instanceof StringRealFilter4);
    StringRealFilter4 stringRealProductIdFilter = (StringRealFilter4) productIdFilter;
    AssertJUnit.assertEquals(QueryParam.PRODUCTID, stringRealProductIdFilter.getQueryParam());
    AssertJUnit.assertEquals("Acme-1234", stringRealProductIdFilter.getValue());

    // Make sure the nested OR looks right
    QueryFilter4 orFilter = queryFilter.getFilters().get(QueryParam.OR);
    AssertJUnit.assertTrue(orFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.OR, orFilter.getQueryParam());
    AssertJUnit.assertEquals(2, orFilter.getFilters().size());

    // Make sure nested AND looks right
    QueryFilter4 nestedAndFilter = orFilter.getFilters().get(QueryParam.AND);
    AssertJUnit.assertTrue(nestedAndFilter.isLogical());
    AssertJUnit.assertEquals(QueryParam.AND, nestedAndFilter.getQueryParam());
    AssertJUnit.assertEquals(2, nestedAndFilter.getFilters().size());

    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString(queryFilter);

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString);
    Map<String, Object> expected = JsonUtils.classpathToMap(testFixture);

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff(expected, actual);
    if (!result.isEmpty()) {
        AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected)
                + "\n  actual: " + JsonUtils.toJsonString(result.actual));
    }
}

From source file:com.ning.billing.recurly.model.TestXmlMapper.java

@Test(groups = "fast", description = "See https://github.com/FasterXML/jackson-dataformat-xml/issues/76")
public void testCollection() throws Exception {
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setSerializerProvider(new RecurlyXmlSerializerProvider());
    final SimpleModule m = new SimpleModule("module", new Version(1, 0, 0, null, null, null));
    m.addSerializer(Values.class, new ValuesSerializer());
    xmlMapper.registerModule(m);/*from   ww  w  .java2 s. co  m*/

    final Values values = xmlMapper
            .readValue(
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "  <values type=\"array\">\n"
                            + "    <value>Hi!</value>" + "    <value>Salut!</value>" + "  </values>",
                    Values.class);
    Assert.assertEquals(values.size(), 2, values.toString());
    Assert.assertEquals(values.get(0), "Hi!");
    Assert.assertEquals(values.get(1), "Salut!");

    // Test we can re-serialize
    final String valueAsString = xmlMapper.writeValueAsString(values);
    final Values values2 = xmlMapper.readValue(valueAsString, Values.class);
    Assert.assertEquals(values2, values, valueAsString);
}

From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.five.MappingTest5.java

@Test
public void testPolymorphicJacksonSerializationAndDeserialization() {
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer(QueryFilter5.class, new QueryFilter5Deserializer());

    mapper.registerModule(testModule);//w  ww .  ja v a 2  s.c  om

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper);

    String testFixture = "/jsonUtils/testdomain/five/queryFilter-realAndLogical5.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter5 queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter5>() {
    });

    // Make sure the hydrated QFilter looks right
    AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter5);
    LogicalFilter5 andFilter = (LogicalFilter5) queryFilter;
    AssertJUnit.assertEquals(Operator.AND, andFilter.getOperator());
    AssertJUnit.assertNotNull(andFilter.getValues());
    AssertJUnit.assertEquals(3, andFilter.getValues().size());

    // Make sure one of the top level RealFilters looks right
    QueryFilter5 productIdFilter = andFilter.getValues().get(1);
    AssertJUnit.assertTrue(productIdFilter instanceof StringRealFilter5);
    StringRealFilter5 stringRealProductIdFilter = (StringRealFilter5) productIdFilter;
    AssertJUnit.assertEquals(Field.PRODUCTID, stringRealProductIdFilter.getField());
    AssertJUnit.assertEquals(Operator.EQ, stringRealProductIdFilter.getOperator());
    AssertJUnit.assertEquals("Acme-1234", stringRealProductIdFilter.getValues().get(0));

    // Make sure the nested OR looks right
    QueryFilter5 orFilter = andFilter.getValues().get(2);
    AssertJUnit.assertTrue(orFilter instanceof LogicalFilter5);
    LogicalFilter5 realOrFilter = (LogicalFilter5) orFilter;
    AssertJUnit.assertEquals(Operator.OR, realOrFilter.getOperator());
    AssertJUnit.assertEquals(2, realOrFilter.getValues().size());

    // Make sure nested AND looks right
    QueryFilter5 nestedAndFilter = realOrFilter.getValues().get(1);
    AssertJUnit.assertTrue(nestedAndFilter instanceof LogicalFilter5);
    AssertJUnit.assertEquals(Operator.AND, nestedAndFilter.getOperator());
    AssertJUnit.assertEquals(3, nestedAndFilter.getValues().size());

    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString(queryFilter);

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString);
    Map<String, Object> expected = JsonUtils.classpathToMap(testFixture);

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff(expected, actual);
    if (!result.isEmpty()) {
        AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected)
                + "\n  actual: " + JsonUtils.toJsonString(result.actual));
    }
}

From source file:org.brutusin.json.impl.JacksonCodec.java

public JacksonCodec(ObjectMapper mapper, JacksonFactoryWrapper schemaFactory) {
    if (mapper == null) {
        mapper = new ObjectMapper();

        mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));

        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        SimpleModule testModule = new SimpleModule("json-provider-module",
                new Version(1, 0, 0, null, "org.brutusin", "json-provider"));
        testModule.addSerializer(new JsonNodeSerializer());
        testModule.addDeserializer(JsonNode.class, new JsonNodeDeserializer());
        testModule.addSerializer(new InputStreamSerializer());
        testModule.addDeserializer(InputStream.class, new InputStreamDeserializer());
        testModule.addDeserializer(MetaDataInputStream.class, new InputStreamDeserializer());
        mapper.registerModule(testModule);
    }//from   www .  ja  v a  2  s.  c  o m
    if (schemaFactory == null) {
        schemaFactory = new JacksonFactoryWrapper(new HashMap<Class, String>(DEFAULT_FORMAT_MAP));
    }
    this.mapper = mapper;
    this.schemaFactory = schemaFactory;
}

From source file:org.jongo.marshall.jackson.JacksonMapperTest.java

@Test
public void canAddModule() throws Exception {

    ObjectId oid = new ObjectId("504482e5e4b0d1b2c47fff66");
    Friend friend = new Friend(oid, "Robert");
    Mapper mapper = jacksonMapper().registerModule(new Module() {
        @Override//  w ww.ja v a2  s .c  om
        public String getModuleName() {
            return "test-module";
        }

        @Override
        public Version version() {
            return new Version(2, 0, 0, "", "org.jongo", "testmodule");
        }

        @Override
        public void setupModule(SetupContext setupContext) {
            SimpleDeserializers deserializers = new SimpleDeserializers();
            deserializers.addDeserializer(String.class, new DoeJsonDeserializer());
            setupContext.addDeserializers(deserializers);
        }
    }).build();

    BsonDocument document = mapper.getMarshaller().marshall(friend);

    assertThat(document.toString()).contains("\"_id\" : { \"$oid\" : \"504482e5e4b0d1b2c47fff66\"}");
}

From source file:eu.trentorise.opendata.commons.jackson.TodCommonsModule.java

/**
 * Returns the jackson version for an tod module by reading it from build
 * info at the root of provided class resources.
 *//*  w ww. jav  a2  s.c  om*/
public static Version readJacksonVersion(Class clazz) {
    SemVersion semver = SemVersion.of(TodConfig.of(clazz).getBuildInfo().getVersion());
    return new Version(semver.getMajor(), semver.getMinor(), semver.getPatch(), semver.getPreReleaseVersion(),
            "eu.trentorise.opendata.commons.jackson", "tod-commons-jackson");
}