-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#156 ServiceProvider enabling Jackson ObjectMapper configuration - ex…
…ample
- Loading branch information
1 parent
0e4ca59
commit 8dc7c88
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...-config/src/test/java/net/javacrumbs/jsonunit/test/jackson2config/Jackson2ConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../src/test/java/net/javacrumbs/jsonunit/test/jackson2config/Java8ObjectMapperProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/services/net.javacrumbs.jsonunit.providers.Jackson2ObjectMapperProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
net.javacrumbs.jsonunit.test.jackson2config.Java8ObjectMapperProvider |