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 11, 2019
1 parent 3f1c773 commit bb56559
Show file tree
Hide file tree
Showing 3 changed files with 57 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.core.jackson2.ObjectMapperProvider;

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<ObjectMapperProvider> serviceLoader = ServiceLoader.load(ObjectMapperProvider.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 ObjectMapperProvider getMapperProvider() {
Iterator<ObjectMapperProvider> 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 ObjectMapperProvider {
static final ObjectMapperProvider 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,25 @@
/**
* 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.core.jackson2;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Interface for customizing ObjectMapper. {@see https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html}
*/
public interface ObjectMapperProvider {
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

0 comments on commit bb56559

Please sign in to comment.