Skip to content

Commit 8dc7c88

Browse files
committed
#156 ServiceProvider enabling Jackson ObjectMapper configuration - example
1 parent 0e4ca59 commit 8dc7c88

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2009-2017 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.javacrumbs.jsonunit.test.jackson2config;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.time.Instant;
21+
22+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
23+
24+
class Jackson2ConfigTest {
25+
26+
@Test
27+
void testSerializeTime() {
28+
assertThatJson(new Bean()).isEqualTo("{time:'2019-01-11T18:12:00Z'}");
29+
}
30+
31+
@Test
32+
void testSerializeTime2() {
33+
assertThatJson(new Bean()).isEqualTo(new Bean());
34+
}
35+
36+
public static class Bean {
37+
private final Instant time = Instant.parse("2019-01-11T18:12:00Z");
38+
39+
public Instant getTime() {
40+
return time;
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2009-2017 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.javacrumbs.jsonunit.test.jackson2config;
17+
18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.SerializationFeature;
21+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
22+
import net.javacrumbs.jsonunit.providers.Jackson2ObjectMapperProvider;
23+
24+
public class Java8ObjectMapperProvider implements Jackson2ObjectMapperProvider {
25+
private final ObjectMapper mapper;
26+
27+
private final ObjectMapper lenientMapper;
28+
29+
30+
public Java8ObjectMapperProvider() {
31+
mapper = new ObjectMapper().registerModule(new JavaTimeModule());
32+
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
33+
34+
lenientMapper = new ObjectMapper().registerModule(new JavaTimeModule());
35+
lenientMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
36+
lenientMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
37+
lenientMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
38+
}
39+
40+
@Override
41+
public ObjectMapper getObjectMapper(boolean lenient) {
42+
return lenient ? lenientMapper : mapper;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
net.javacrumbs.jsonunit.test.jackson2config.Java8ObjectMapperProvider

0 commit comments

Comments
 (0)