1 package com.guinetik.rr.json;
2
3 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
4 import com.fasterxml.jackson.annotation.JsonInclude;
5 import com.fasterxml.jackson.annotation.PropertyAccessor;
6 import com.fasterxml.jackson.core.JsonProcessingException;
7 import com.fasterxml.jackson.core.type.TypeReference;
8 import com.fasterxml.jackson.databind.DeserializationFeature;
9 import com.fasterxml.jackson.databind.JsonNode;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.net.HttpURLConnection;
17 import java.util.List;
18 import java.util.Map;
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 public class JsonObjectMapper extends ObjectMapper {
83
84 private static final long serialVersionUID = 1L;
85
86 private static class ObjectMapperInstanceHolder {
87 static final ObjectMapper INSTANCE = getDefault();
88 }
89
90 public JsonObjectMapper() {
91 setVisibilityChecker(
92 getVisibilityChecker().withVisibility(PropertyAccessor.FIELD, Visibility.ANY)
93 .withVisibility(PropertyAccessor.GETTER, Visibility.NONE).withVisibility(
94 PropertyAccessor.SETTER, Visibility.NONE
95 ));
96 enable(SerializationFeature.INDENT_OUTPUT);
97 }
98
99 private static String toJsonString(Object model, boolean ident) {
100 ObjectMapper mapper = JsonObjectMapper.get();
101 try {
102 if (ident)
103 mapper.enable(SerializationFeature.INDENT_OUTPUT);
104
105 return mapper.writeValueAsString(model);
106 } catch (JsonProcessingException e) {
107 e.printStackTrace();
108 return null;
109 }
110 }
111
112 public static JsonNode getJsonNode(String json) {
113 try {
114 return JsonObjectMapper.get().readValue(json, JsonNode.class);
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 return null;
119 }
120
121 public static Map<String, Object> jsonToMap(String json) {
122 return JsonObjectMapper.get().convertValue(json, new TypeReference<Map<String, Object>>() {
123 });
124 }
125
126 public static Map<String, Object> jsonNodeToMap(JsonNode json) {
127 return JsonObjectMapper.get().convertValue(json, new TypeReference<Map<String, Object>>() {
128 });
129 }
130
131 public static <T> T jsonToObject(String json, Class<T> type) {
132
133 try {
134 return JsonObjectMapper.getDefault().readValue(json, type);
135 } catch (Exception e) {
136 e.printStackTrace();
137 }
138 return null;
139 }
140
141 public static List<Map<String, Object>> jsonToListOfMap(String json) {
142 ObjectMapper mapper = JsonObjectMapper.getDefault();
143 try {
144 return mapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
145 });
146 } catch (Exception e) {
147 e.printStackTrace();
148 }
149 return null;
150 }
151
152 public static String toJsonString(Object model) {
153 return toJsonString(model, true);
154 }
155
156 public static String toJsonStringNoIdent(Object model) {
157 return toJsonString(model, false);
158 }
159
160 public static ObjectMapper getDefault() {
161 ObjectMapper mapper = new ObjectMapper();
162 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
163 mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
164 mapper.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
165 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
166
167
168
169 return mapper;
170 }
171
172 public static ObjectMapper get() {
173 return ObjectMapperInstanceHolder.INSTANCE;
174 }
175
176 public static Map<String, Object> parseResponse(HttpURLConnection connection) throws IOException {
177 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
178 StringBuilder response = new StringBuilder();
179 String line;
180 while ((line = reader.readLine()) != null) {
181 response.append(line);
182 }
183 return get().readValue(response.toString(), new TypeReference<Map<String, Object>>() {});
184 }
185 }
186 }