Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add ObjectMapperProducer method returns ObjectMapper with JmsFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Apr 13, 2018
1 parent d9f32a3 commit 389a84e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

### Added
- ObjectMapperProducer method for creating and object mapper with a JmsFactory

## [1.12.1] - 2018-03-06

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
</modules>

<properties>
<common-bom.version>1.22.0</common-bom.version>
<test-utils.version>1.15.0</test-utils.version>

<cpp.repo.name>utilities</cpp.repo.name>

<common-bom.version>1.24.0</common-bom.version>
<test-utils.version>1.15.0</test-utils.version>
</properties>

<scm>
Expand Down
17 changes: 11 additions & 6 deletions utilities-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down Expand Up @@ -95,12 +103,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package uk.gov.justice.services.common.converter.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import uk.gov.justice.services.common.converter.jackson.additionalproperties.AdditionalPropertiesModule;
import uk.gov.justice.services.common.converter.jackson.jsr353.InclusionAwareJSR353Module;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import java.time.ZonedDateTime;

import static com.fasterxml.jackson.annotation.JsonCreator.Mode.PROPERTIES;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_ABSENT;
import static com.fasterxml.jackson.databind.DeserializationFeature.READ_ENUMS_USING_TO_STRING;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_WITH_ZONE_ID;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_NULL_MAP_VALUES;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_NULL_MAP_VALUES;
import static java.time.ZoneOffset.UTC;
import static java.time.format.DateTimeFormatter.ofPattern;
import static java.util.TimeZone.getTimeZone;
import static uk.gov.justice.services.common.converter.ZonedDateTimes.ISO_8601;

import uk.gov.justice.services.common.converter.jackson.additionalproperties.AdditionalPropertiesModule;
import uk.gov.justice.services.common.converter.jackson.jsr353.InclusionAwareJSR353Module;

import java.time.ZonedDateTime;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

/**
* Produces the configured {@link ObjectMapper}.
*/
Expand All @@ -32,7 +35,15 @@ public class ObjectMapperProducer {

@Produces
public ObjectMapper objectMapper() {
return new ObjectMapper()
return configureObjectMapper(new ObjectMapper());
}

public ObjectMapper objectMapperWith(final JsonFactory jsonFactory) {
return configureObjectMapper(new ObjectMapper(jsonFactory));
}

private ObjectMapper configureObjectMapper(final ObjectMapper objectMapper) {
return objectMapper
.registerModule(javaTimeModuleWithFormattedDateTime())
.registerModule(new Jdk8Module())
.registerModule(new ParameterNamesModule(PROPERTIES))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import javax.json.JsonValue;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -29,6 +31,9 @@ public class ObjectMapperProducerTest {
private static final String JSON_OBJECT_STRING = "{\n" +
" \"id\": \"861c9430-7bc6-4bf0-b549-6534394b8d65\"\n" +
"}";
private static final String YAML_AS_STRING = "---\n" +
"subscription_descriptor:\n" +
" spec_version: 1.0.0\n";

private ObjectMapper mapper;

Expand Down Expand Up @@ -208,6 +213,16 @@ public void shouldWriteAdditionalPropertiesWithAllPropertyTypes() throws Excepti
;
}

@Test
public void shouldConvertYamlToJsonObject() throws Exception {
final ObjectMapper yamlObjectMapper = new ObjectMapperProducer().objectMapperWith(new YAMLFactory());

final Object yamlObject = yamlObjectMapper.readValue(YAML_AS_STRING, Object.class);
final JSONObject yamlAsJsonObject = new JSONObject(mapper.writeValueAsString(yamlObject));

assertThat(yamlAsJsonObject.getJSONObject("subscription_descriptor").get("spec_version"), is("1.0.0"));
}

public static class DummyBeanWithSingleArgConstructor {
private final String name;

Expand All @@ -233,7 +248,7 @@ public int hashCode() {
}
}

public static enum Colour {
public enum Colour {
RED("Red"),
GREEN("Green"),
BLUE("Blue");
Expand Down

0 comments on commit 389a84e

Please sign in to comment.