Skip to content

Commit

Permalink
#156 ServiceProvider enabling Jackson ObjectMapper configuration - ex…
Browse files Browse the repository at this point in the history
…ample
  • Loading branch information
lukas-krecan committed Jan 13, 2019
1 parent 0e4ca59 commit 8dc7c88
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.test.jackson2config;

import org.junit.jupiter.api.Test;

import java.time.Instant;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

class Jackson2ConfigTest {

@Test
void testSerializeTime() {
assertThatJson(new Bean()).isEqualTo("{time:'2019-01-11T18:12:00Z'}");
}

@Test
void testSerializeTime2() {
assertThatJson(new Bean()).isEqualTo(new Bean());
}

public static class Bean {
private final Instant time = Instant.parse("2019-01-11T18:12:00Z");

public Instant getTime() {
return time;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.test.jackson2config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import net.javacrumbs.jsonunit.providers.Jackson2ObjectMapperProvider;

public class Java8ObjectMapperProvider implements Jackson2ObjectMapperProvider {
private final ObjectMapper mapper;

private final ObjectMapper lenientMapper;


public Java8ObjectMapperProvider() {
mapper = new ObjectMapper().registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

lenientMapper = new ObjectMapper().registerModule(new JavaTimeModule());
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 @@
net.javacrumbs.jsonunit.test.jackson2config.Java8ObjectMapperProvider

0 comments on commit 8dc7c88

Please sign in to comment.