Skip to content

Commit

Permalink
#156 ServiceProvider enabling Jackson ObjectMapper configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jan 13, 2019
1 parent 3f1c773 commit 0e4ca59
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import net.javacrumbs.jsonunit.providers.Jackson2ObjectMapperProvider;

import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

import static net.javacrumbs.jsonunit.core.internal.Utils.closeQuietly;

/**
* Deserializes node using Jackson 2
*/
class Jackson2NodeFactory extends AbstractNodeFactory {
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper lenientMapper = new ObjectMapper();

static {
lenientMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
lenientMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
lenientMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}

private final ServiceLoader<Jackson2ObjectMapperProvider> serviceLoader = ServiceLoader.load(Jackson2ObjectMapperProvider.class);

@Override
protected Node doConvertValue(Object source) {
return newNode(mapper.convertValue(source, JsonNode.class));
return newNode(getMapper(false).convertValue(source, JsonNode.class));
}

@Override
Expand All @@ -63,7 +57,16 @@ protected Node readValue(Reader value, String label, boolean lenient) {
}

private ObjectMapper getMapper(boolean lenient) {
return lenient ? lenientMapper : mapper;
return getMapperProvider().getObjectMapper(lenient);
}

private Jackson2ObjectMapperProvider getMapperProvider() {
Iterator<Jackson2ObjectMapperProvider> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return DefaultObjectMapperProvider.INSTANCE;
}
}

private static Node newNode(JsonNode jsonNode) {
Expand Down Expand Up @@ -177,4 +180,21 @@ public String toString() {
return jsonNode.toString();
}
}

private static class DefaultObjectMapperProvider implements Jackson2ObjectMapperProvider {
static final Jackson2ObjectMapperProvider INSTANCE = new DefaultObjectMapperProvider();

private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper lenientMapper = new ObjectMapper();

static {
lenientMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
lenientMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
lenientMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
@Override
public ObjectMapper getObjectMapper(boolean lenient) {
return lenient ? lenientMapper : mapper;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright 2009-2017 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.jsonunit.providers;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Interface for customizing Jackson 2 ObjectMapper. {@see https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html}
*/
public interface Jackson2ObjectMapperProvider {
/**
* Provides ObjectMapper
* @param lenient Lenient parsing is used for parsing the expected JSON value
* @return customized ObjectMapper.
*/
ObjectMapper getObjectMapper(boolean lenient);
}
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<modules>
<module>test-base</module>
<module>test-jackson2</module>
<module>test-jackson2-config</module>
<module>test-gson</module>
<module>test-jsonorg</module>
<module>test-moshi</module>
Expand Down
36 changes: 36 additions & 0 deletions tests/test-jackson2-config/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tests</artifactId>
<groupId>net.javacrumbs.json-unit</groupId>
<version>2.2.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-jackson2-config</artifactId>
<version>2.2.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>test-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>
</project>

0 comments on commit 0e4ca59

Please sign in to comment.