Example usage for com.fasterxml.jackson.databind ObjectMapper registerModule

List of usage examples for com.fasterxml.jackson.databind ObjectMapper registerModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper registerModule.

Prototype

public ObjectMapper registerModule(Module module) 

Source Link

Document

Method for registering a module that can extend functionality provided by this mapper; for example, by adding providers for custom serializers and deserializers.

Usage

From source file:com.arpnetworking.test.junitbenchmarks.GCShapshotSerializerTest.java

@Test
public void testSerialization() {
    final SimpleModule module = new SimpleModule();
    module.addSerializer(GCSnapshot.class, new GCSnapshotSerializer());
    final ObjectMapper mapper = ObjectMapperFactory.createInstance();
    mapper.registerModule(module);

    final GCSnapshot testSnapshot = DataCreator.createGCSnapshot();

    final JsonNode jsonNode = mapper.valueToTree(testSnapshot);
    Assert.assertTrue(jsonNode.isObject());
    Assert.assertThat(jsonNode.get("accumulatedInvocations").asLong(), Matchers.greaterThanOrEqualTo(0L));
    Assert.assertThat(jsonNode.get("accumulatedTime").asLong(), Matchers.greaterThanOrEqualTo(0L));
}

From source file:io.getlime.push.configuration.WebApplicationConfig.java

/**
 * Custom object mapper to make sure that dates and other values serialize
 * correctly./*w  ww  . j  a  va 2 s  . com*/
 *
 * @return A new object mapper.
 */
private ObjectMapper objectMapper() {
    Jackson2ObjectMapperFactoryBean bean = new Jackson2ObjectMapperFactoryBean();
    bean.setIndentOutput(true);
    bean.setDateFormat(new ISO8601DateFormat());
    bean.afterPropertiesSet();
    ObjectMapper objectMapper = bean.getObject();
    objectMapper.registerModule(new JodaModule());
    return objectMapper;
}

From source file:Sandbox.java

@Test
public void playAround() {
    String json = null;//  w ww  .  jav a  2  s . c  o m
    ObjectifyJacksonModule ojm = new ObjectifyJacksonModule();
    ObjectMapper mapper = new ObjectMapper();

    mapper.registerModule(ojm);

    Key key = Key.create(Artist.class, "the-beatles");

    logger.info("Web Safe String = " + key.getString());
    logger.info("toString() = " + key.toString());

    try {
        json = mapper.writeValueAsString(key);
    } catch (JsonProcessingException jpe) {
        jpe.printStackTrace();
    }

    logger.info("Serializing '" + json + "'.");
}

From source file:org.lable.rfc3881.auditlogger.serialization.CodeReferenceDeserializerTest.java

@Test
public void nullFieldTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new CodeReferenceDeserializerModule());

    String json = "{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"csn\":null," + "\"dn\":\"One\"" + "}";

    CodeReference expected = new CodeReference("CS", null, "001", "One", null);
    CodeReference output = objectMapper.readValue(json, CodeReference.class);

    assertThat(output, is(expected));/*from   w  w  w. jav a 2s  . co  m*/
    assertThat(output.getCodeSystemName(), is(expected.getCodeSystemName()));
    assertThat(output.getOriginalText(), is(expected.getOriginalText()));
    assertThat(output.getDisplayName(), is(expected.getDisplayName()));
}

From source file:org.lable.rfc3881.auditlogger.serialization.CodeReferenceDeserializerTest.java

@Test
public void basicTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new CodeReferenceDeserializerModule());

    String json = "{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"csn\":\"CodeSystem\"," + "\"dn\":\"One\","
            + "\"ot\":\"ONE\"" + "}";

    CodeReference expected = new CodeReference("CS", "CodeSystem", "001", "One", "ONE");
    CodeReference output = objectMapper.readValue(json, CodeReference.class);

    assertThat(output, is(expected));/*w  w w  .  j  av a  2 s  .c o  m*/
    assertThat(output.getCodeSystemName(), is(expected.getCodeSystemName()));
    assertThat(output.getOriginalText(), is(expected.getOriginalText()));
    assertThat(output.getDisplayName(), is(expected.getDisplayName()));
}

From source file:com.arpnetworking.jackson.EnumerationDeserializerTest.java

@SuppressWarnings("unchecked")
@Test/*from  ww  w  . j  a va2s.co  m*/
public void testDeserializer() throws Exception {
    final EnumerationDeserializerStrategy<TestEnum> strategy = Mockito
            .mock(EnumerationDeserializerStrategy.class);
    Mockito.doReturn(TestEnum.FOO).when(strategy).toEnum(Mockito.any(Class.class), Mockito.eq("bar"));

    final SimpleModule module = new SimpleModule();
    module.addDeserializer(TestEnum.class, EnumerationDeserializer.newInstance(TestEnum.class, strategy));
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(module);

    final TestClass c = objectMapper.readValue("{\"enum\":\"bar\"}", TestClass.class);
    Mockito.verify(strategy).toEnum(Mockito.any(Class.class), Mockito.eq("bar"));
    Assert.assertEquals(TestEnum.FOO, c.getEnum());
}

From source file:com.chiralbehaviors.CoRE.workspace.WorkspaceSnapshotTest.java

@Test
public void testSerializeWorkspaceSnapshot() throws Exception {
    Agency pseudoScientist = new Agency("Behold the Pseudo Scientist!");
    pseudoScientist.setUpdatedBy(pseudoScientist);
    Product definingProduct = new Product("zee product", pseudoScientist);
    WorkspaceAuthorization auth = new WorkspaceAuthorization(pseudoScientist, definingProduct, pseudoScientist);
    WorkspaceSnapshot snapshot = new WorkspaceSnapshot(Arrays.asList(auth));
    snapshot.retarget(em);//from  w  w  w  .  j  a va 2  s .c  o m
    em.getTransaction().commit();
    WorkspaceSnapshot retrieved = new WorkspaceSnapshot(definingProduct, em);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new CoREModule());
    mapper.writeValue(os, retrieved);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    WorkspaceSnapshot deserialized = mapper.readValue(is, WorkspaceSnapshot.class);
    assertEquals(1, deserialized.getAuths().size());
    assertEquals(auth, deserialized.getAuths().get(0));

}

From source file:com.siemens.sw360.datahandler.couchdb.MapperFactory.java

@Override
public ObjectMapper createObjectMapper(CouchDbConnector connector) {
    ObjectMapper mapper = createObjectMapper();
    mapper.registerModule(new EktorpJacksonModule(connector, mapper));
    return mapper;
}

From source file:com.nestorledon.employeedirectory.config.ApplicationConfig.java

/**
 * Needed for Jackson2 to read HATEOAS/HAL JSON.
 * /*ww w. jav a 2  s . c  o m*/
 * Example 1:
 * String resultBody = restTemplate.getForObject(link.getHref(), String.class);
 * Resource<EmployeeDTO> resource = objectMapper.readValue(resultBody, EmployeeDTO.class);
 * 
 * Example 2:
 * MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
 * converter.setObjectMapper(objectMapper);
 * restTemplate.setMessageConverters(Collections.<HttpMessageConverter<?>> singletonList(converter));
 * Resource<EmployeeDTO> resource = restTemplate.getForObject(link.getHref(), EmployeeDTO.class);
 * 
 * @see http://stackoverflow.com/questions/25812776/proper-way-to-convert-spring-hateoas-link-to-object/25922483#25922483
 * 
 * @return
 */
@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}

From source file:org.apache.nifi.att.m2x.TestM2XStreamValues.java

@Test
public void testNumericValues() throws JsonParseException, IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());

    final String json = "{\"start\":\"2014-09-01T00:00:00.000Z\",\"end\":\"2014-09-30T23:59:59.000Z\",\"limit\":100,\"values\":[{\"timestamp\":\"2014-09-09T19:15:00.563Z\",\"value\":32},{\"timestamp\":\"2014-09-09T20:15:00.874Z\",\"value\":29},{\"timestamp\":\"2014-09-09T21:15:00.325Z\",\"value\":30}]}";
    final M2XStreamValues valuesObj = mapper.readValue(json, M2XStreamValues.class);
    final List<M2XStreamValue> valuesList = valuesObj.getValues();

    assertEquals((Integer) 100, valuesObj.getLimit());
    assertEquals(3, valuesList.size());//w w  w  .  j a  va  2s .  co  m

    assertEquals(32, valuesList.get(0).getValue());
    assertEquals(29, valuesList.get(1).getValue());
    assertEquals(30, valuesList.get(2).getValue());
}