-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonUtils.java
155 lines (127 loc) · 4.94 KB
/
JsonUtils.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.peeyush.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Created by Peeyush Chandel.
*/
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
private static ObjectMapper objectMapperMessagePack = new ObjectMapper(new MessagePackFactory());
static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapperMessagePack.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapperMessagePack.setSerializationInclusion(Include.NON_NULL);
}
private JsonUtils() {
}
public static String getJsonStringFromObject(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
StringWriter writer = new StringWriter();
objectMapper.writeValue(writer, obj);
return writer.toString();
}
public static <T> T getObjectFromJsonString(Class<T> clazz, String requestJson,
String callingMethod) throws IOException {
T t = null;
InputStream is = new ByteArrayInputStream(requestJson.getBytes("UTF-8"));
t = (T) objectMapper.readValue(is, clazz);
return t;
}
public static <T> T getObjectFromJsonString(String json, Class<T> className)
throws JsonParseException, JsonMappingException, IOException {
return getObjectFromJsonString(className, json, "null");
}
public static <T> Object getObjectFromJsonString(String json, TypeReference<T> typeRef)
throws IOException {
InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
return objectMapper.readValue(is, typeRef);
}
public static JsonNode convertObjectToJsonNode(Object object) {
JsonNode jsonAdObject = objectMapper.valueToTree(object);
return jsonAdObject;
}
public static <T> Object getObjectFromFile(String fileName, Class<T> className)
throws JsonParseException, JsonMappingException, IOException {
T obj = null;
File file = new File(fileName);
obj = objectMapper.readValue(file, className);
return obj;
}
public static JsonNode getJsonNodeFromQueryParamsMap(Map<String, String[]> queryParams) {
if (queryParams == null) {
return null;
}
Map<String, String> queryParamsMap = new HashMap<String, String>();
for (Map.Entry<String, String[]> entry : queryParams.entrySet()) {
queryParamsMap.put(entry.getKey(), entry.getValue()[0]);
}
JsonNode jsonObject = objectMapper.valueToTree(queryParamsMap);
return jsonObject;
}
public static JsonNode getFirstParamNodeFromQueryParams(Map<String, String[]> queryParams) {
if (queryParams == null) {
return null;
}
ObjectNode node = objectMapper.createObjectNode();
for (String key : queryParams.keySet()) {
node.put(key, queryParams.get(key)[0]);
}
return node;
}
public static <T> T getObjectFromJsonNode(JsonNode node, Class<T> className)
throws JsonProcessingException {
return objectMapper.treeToValue(node, className);
}
public static JsonNode getJsonNodeFromObject(Object object) {
return objectMapper.valueToTree(object);
}
public static JsonNode getJsonNodeFromJsonString(String jsonString) throws JsonParseException,
JsonMappingException, IOException {
JsonNode retval = null;
if (StringUtils.isNotEmpty(jsonString)) {
retval = objectMapper.readValue(jsonString, JsonNode.class);
}
return retval;
}
public static ArrayNode createArrayNode() {
return objectMapper.createArrayNode();
}
public static ObjectNode createObjectNode() {
return objectMapper.createObjectNode();
}
public static byte[] messagePackSerialize(Object obj) throws JsonProcessingException {
return objectMapperMessagePack.writeValueAsBytes(obj);
}
public static <T> Object messagePackDeserialize(byte[] b, Class<T> className)
throws JsonParseException, JsonMappingException, IOException {
return objectMapperMessagePack.readValue(b, className);
}
public static List<String> getSortedArrayNode(ArrayNode arrayNode) throws IOException {
List<String> list = new ArrayList<String>();
Iterator<JsonNode> it = arrayNode.iterator();
while (it.hasNext()) {
list.add(it.next().asText());
}
Collections.sort(list);
return list;
}
}