11package fr .maif .json ;
22
33import com .fasterxml .jackson .annotation .JsonInclude ;
4- import com . fasterxml .jackson .core .json .JsonWriteFeature ;
5- import com . fasterxml .jackson .core .type .TypeReference ;
6- import com . fasterxml .jackson .databind .DeserializationFeature ;
7- import com . fasterxml .jackson .databind .JsonNode ;
8- import com . fasterxml .jackson .databind .ObjectMapper ;
9- import com . fasterxml .jackson .databind .ObjectWriter ;
10- import com . fasterxml .jackson .databind .SerializationFeature ;
11- import com . fasterxml . jackson .databind .node . ArrayNode ;
12- import com . fasterxml . jackson .databind .node . BooleanNode ;
13- import com . fasterxml . jackson .databind .node .IntNode ;
14- import com . fasterxml . jackson .databind .node .LongNode ;
15- import com . fasterxml . jackson .databind .node .ObjectNode ;
16- import com . fasterxml . jackson .databind .node .TextNode ;
17- import com . fasterxml . jackson .datatype . jdk8 . Jdk8Module ;
18- import com . fasterxml . jackson .datatype . jsr310 . JavaTimeModule ;
4+ import tools .jackson .core .json .JsonWriteFeature ;
5+ import tools .jackson .core .type .TypeReference ;
6+ import tools .jackson .databind .DeserializationFeature ;
7+ import tools .jackson .databind .JsonNode ;
8+ import tools .jackson .databind .ObjectMapper ;
9+ import tools .jackson .databind .ObjectWriter ;
10+ import tools .jackson .databind .SerializationFeature ;
11+ import tools . jackson .databind .cfg . DateTimeFeature ;
12+ import tools . jackson .databind .json . JsonMapper ;
13+ import tools . jackson .databind .node .ArrayNode ;
14+ import tools . jackson .databind .node .BooleanNode ;
15+ import tools . jackson .databind .node .IntNode ;
16+ import tools . jackson .databind .node .LongNode ;
17+ import tools . jackson .databind . node . ObjectNode ;
18+ import tools . jackson .databind . node . StringNode ;
1919import io .vavr .collection .List ;
2020import io .vavr .collection .Traversable ;
2121import io .vavr .control .Option ;
@@ -30,15 +30,13 @@ public class Json {
3030 private static ObjectMapper defaultObjectMapper = newDefaultMapper ();
3131
3232 public static ObjectMapper newDefaultMapper () {
33- ObjectMapper mapper = new ObjectMapper ();
34- mapper .registerModule (new VavrModule ());
35- mapper .registerModule (new Jdk8Module ());
36- mapper .registerModule (new JavaTimeModule ());
37- mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
38- mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
39- mapper .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false );
40- mapper .setSerializationInclusion (JsonInclude .Include .NON_ABSENT );
41- return mapper ;
33+ return JsonMapper .builder ()
34+ .addModule (new VavrModule ())
35+ .configure (DateTimeFeature .WRITE_DATES_AS_TIMESTAMPS , false )
36+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
37+ .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false )
38+ .changeDefaultPropertyInclusion (incl -> incl .withValueInclusion (JsonInclude .Include .NON_ABSENT ))
39+ .build ();
4240 }
4341
4442 /**
@@ -63,18 +61,14 @@ public static ObjectMapper mapper() {
6361
6462
6563 private static String generateJson (Object o , boolean prettyPrint , boolean escapeNonASCII ) {
66- try {
67- ObjectWriter writer = mapper ().writer ();
68- if (prettyPrint ) {
69- writer = writer .with (SerializationFeature .INDENT_OUTPUT );
70- }
71- if (escapeNonASCII ) {
72- writer = writer .with (JsonWriteFeature .ESCAPE_NON_ASCII );
73- }
74- return writer .writeValueAsString (o );
75- } catch (IOException e ) {
76- throw new RuntimeException (e );
64+ ObjectWriter writer = mapper ().writer ();
65+ if (prettyPrint ) {
66+ writer = writer .with (SerializationFeature .INDENT_OUTPUT );
67+ }
68+ if (escapeNonASCII ) {
69+ writer = writer .with (JsonWriteFeature .ESCAPE_NON_ASCII );
7770 }
71+ return writer .writeValueAsString (o );
7872 }
7973
8074 /**
@@ -201,7 +195,7 @@ public static ObjectNode obj(ObjectNode obj, JsPair ...pairs) {
201195 }
202196
203197 public static ObjectNode merge (ObjectNode obj , ObjectNode obj2 ) {
204- obj .fields ().forEachRemaining (e ->
198+ obj .properties ().forEach (e ->
205199 obj2 .set (e .getKey (), e .getValue ())
206200 );
207201 return obj2 ;
@@ -261,7 +255,7 @@ public static ArrayNode arr(Traversable<JsonNode> nodes) {
261255 */
262256 public static ArrayNode arr (String ...nodes ) {
263257 ArrayNode obj = newArray ();
264- List .of (nodes ).map (TextNode ::new ).forEach (obj ::add );
258+ List .of (nodes ).map (StringNode ::new ).forEach (obj ::add );
265259 return obj ;
266260 }
267261
@@ -309,7 +303,7 @@ public static ArrayNode arr(String ...nodes) {
309303 * @return the field name / value pair
310304 */
311305 public static JsPair $ (String field , Option <String > value ) {
312- return new JsPair (field , value .map (TextNode ::new ));
306+ return new JsPair (field , value .map (StringNode ::new ));
313307 }
314308 /**
315309 * An object field for a string option.
0 commit comments