-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver_src_main_java_com_httpserveremanuel_httpserver_util_Json.java
More file actions
54 lines (40 loc) · 1.84 KB
/
Copy pathhttpserver_src_main_java_com_httpserveremanuel_httpserver_util_Json.java
File metadata and controls
54 lines (40 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.httpserveremanuel.httpserver.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import java.io.IOException;
public class Json {
private static ObjectMapper myObjectMapper = defaultObjectMapper();
private static ObjectMapper defaultObjectMapper(){
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om;
}
//We create to parse an json string into a json node
public static JsonNode parse(String jsonSrc) throws IOException {
return myObjectMapper.readTree(jsonSrc);
}
//Now we need a way this json node into the configuration file
//This A us generic one
public static <A> A fromJson(JsonNode node ,Class<A> clazz) throws JsonProcessingException {
return myObjectMapper.treeToValue(node, clazz);
}
// After this we create the configuration file into the json file
public static JsonNode toJson(Object obj ){
return myObjectMapper.valueToTree(obj);
}
//This is the method that we are called from the main method the one is the true the other is false
public static String stringify(JsonNode node) throws JsonProcessingException {
return generateJson(node, false);
}
//This is the method that we are called from the main method the one is true the other false
public static String stringifyPretty(JsonNode node) throws JsonProcessingException {
return generateJson(node, true);
}
//be able to see a jsnode to see in a string
private static String generateJson(Object o , boolean pretty) throws JsonProcessingException {
ObjectWriter objectWriter = myObjectMapper.writer();
if(pretty)
objectWriter = objectWriter.with(SerializationFeature.INDENT_OUTPUT);
return objectWriter.writeValueAsString(o);
}
}