rsanchez
2014-10-22 ffc60638fba7475b4cb6a863aa8f27c7e5a9b059
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -8,207 +8,199 @@
88
99 import org.apache.logging.log4j.LogManager;
1010 import org.apache.logging.log4j.Logger;
11
-import org.codehaus.jackson.JsonParseException;
12
-import org.codehaus.jackson.JsonParser;
13
-import org.codehaus.jackson.JsonProcessingException;
14
-import org.codehaus.jackson.map.ObjectMapper;
15
-import org.codehaus.jackson.map.SerializationConfig;
16
-import org.codehaus.jackson.type.TypeReference;
11
+
12
+import com.fasterxml.jackson.core.JsonParseException;
13
+import com.fasterxml.jackson.core.JsonParser;
14
+import com.fasterxml.jackson.core.type.TypeReference;
15
+import com.fasterxml.jackson.databind.ObjectMapper;
16
+import com.fasterxml.jackson.databind.SerializationFeature;
17
+
18
+//import org.codehaus.jackson.map.ObjectMapper;
1719
1820 /**
1921 * Helper method to perform JSON tasks
2022 *
21
- * @author cproberto
23
+ * @author cproberto, ccalvo
2224 */
2325 public class JsonUtils {
2426
25
- private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
27
+ private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
2628
27
- final private static ObjectMapper MAPPER = new ObjectMapper();
29
+ final private static ObjectMapper MAPPER = new ObjectMapper();
30
+ final private static ObjectMapper MAPPER_PRETTY;
2831
29
- static {
30
- MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
31
- MAPPER.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
32
- MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
33
- MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
34
- MAPPER.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
35
- MAPPER.configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
36
- MAPPER.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
37
- MAPPER.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
32
+ static {
3833
39
- }
34
+ MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
35
+ MAPPER.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
36
+ MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
37
+ MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
38
+ MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
39
+ MAPPER.disable(SerializationFeature.INDENT_OUTPUT);
40
+ MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
41
+ MAPPER.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
42
+ MAPPER_PRETTY = MAPPER.copy();
43
+ MAPPER_PRETTY.enable(SerializationFeature.INDENT_OUTPUT);
44
+ }
4045
41
- /**
42
- * Convert an object in the type pass as parameter, avoiding to use casting in code.
43
- *
44
- * @param value
45
- * @param type
46
- * @return
47
- */
48
- @SuppressWarnings("unchecked")
49
- public static <T> T value(Object value, Class<T> type) {
46
+ /**
47
+ * Convert an object in the type pass as parameter, avoiding to use casting
48
+ * in code.
49
+ *
50
+ * @param value
51
+ * @param type
52
+ * @return
53
+ */
54
+ public static <T> T value(Object value, Class<T> type) {
5055
51
- return (T) value;
52
- }
56
+ return (T) value;
57
+ }
5358
54
- public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
55
- try {
56
- if (json == null){
57
- return null;
58
- }
59
- return MAPPER.readValue(json, type);
60
- } catch (JsonParseException e) {
61
- LOG.error("Error parsing JSON string to obejct: {}", json, e);
62
- if (json.length() > 60)
63
- json = json.substring(0, 50) + "...";
64
- throw new SeCurisException("Error parsing JSON string to object: " + json, e);
65
- } catch (IOException e) {
66
- LOG.error("Error parsing JSON string to object: {}", json, e);
67
- if (json.length() > 60)
68
- json = json.substring(0, 50) + "...";
69
- throw new SeCurisException("Error parsing JSON string to object: " + json, e);
70
- }
71
- }
59
+ public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
60
+ try {
61
+ if (json == null)
62
+ return null;
63
+ return MAPPER.readValue(json, type);
64
+ } catch (JsonParseException e) {
65
+ LOG.error("Error parsing JSON string to obejct: {}", json, e);
66
+ if (json.length() > 60)
67
+ json = json.substring(0, 50) + "...";
68
+ throw new SeCurisException("Error parsing JSON string to object: " + json, e);
69
+ } catch (IOException e) {
70
+ LOG.error("Error parsing JSON string to object: {}", json, e);
71
+ if (json.length() > 60)
72
+ json = json.substring(0, 50) + "...";
73
+ throw new SeCurisException("Error parsing JSON string to object: " + json, e);
74
+ }
75
+ }
7276
73
- /**
74
- * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
75
- * {"f1":2345,"f2":"Test de valor"}
76
- *
77
- * @param obj
78
- * @return JSON string representation from object
79
- */
80
- public static String toJSON(Object obj) throws SeCurisException {
81
- // and could also do other configuration...
82
- try {
83
- if (obj == null) {
84
- return null;
85
- }
86
- return MAPPER.writeValueAsString(obj);
87
- } catch (JsonProcessingException e) {
88
- LOG.error("Error formating JSON from object: {}", obj, e);
89
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
90
- } catch (IOException e) {
91
- LOG.error("Error formating JSON from object: {}", obj, e);
92
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
93
- }
94
- }
77
+ /**
78
+ * Create a JSON string from a object compatible or annotated with Jackson,
79
+ * i.e: <code>
80
+ * {"f1":2345,"f2":"Test de valor"}
81
+ *
82
+ * @param obj
83
+ * @return JSON string representation from object
84
+ */
85
+ public static String toJSON(Object obj) throws SeCurisException {
86
+ // and could also do other configuration...
87
+ try {
88
+ if (obj == null)
89
+ return null;
90
+ return MAPPER.writeValueAsString(obj);
91
+ } catch (IOException e) {
92
+ LOG.error("Error formating JSON from object: {}", obj, e);
93
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
94
+ }
95
+ }
9596
96
- /**
97
- * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
98
- * {"f1":2345,"f2":"Test de valor"}
99
- *
100
- * @param obj
101
- * @return JSON string representation from object
102
- */
103
- public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
104
- // and could also do other configuration...
105
- try {
106
- if (obj == null) {
107
- return null;
108
- }
109
- MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
110
- String json = MAPPER.writeValueAsString(obj);
111
- MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
112
- return json;
113
- } catch (JsonProcessingException e) {
114
- LOG.error("Error formating JSON from object: {}", obj, e);
115
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
116
- } catch (IOException e) {
117
- LOG.error("Error formating JSON from object: {}", obj, e);
118
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
119
- }
120
- }
97
+ /**
98
+ * Create a JSON string from a object compatible or annotated with Jackson,
99
+ * i.e: <code>
100
+ * {"f1":2345,"f2":"Test de valor"}
101
+ *
102
+ * @param obj
103
+ * @return JSON string representation from object
104
+ */
105
+ public static String toPrettyJSON(Object obj) throws SeCurisException {
106
+ // and could also do other configuration...
107
+ try {
108
+ if (obj == null)
109
+ return null;
110
+ return MAPPER_PRETTY.writeValueAsString(obj);
111
+ } catch (IOException e) {
112
+ LOG.error("Error formating JSON from object: {}", obj, e);
113
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
114
+ }
115
+ }
121116
122
- /**
123
- * Create a Map from a json string, i.e: <code>
124
- * {"f1":2345,"f2":"Test de valor"}
125
- * </code>
126
- *
127
- * @param json
128
- * String with json format
129
- * @return
130
- */
131
- @SuppressWarnings("unchecked")
132
- public static Map<String, Object> json2map(String json) throws JsonParseException {
117
+ /**
118
+ * Create a Map from a json string, i.e: <code>
119
+ * {"f1":2345,"f2":"Test de valor"}
120
+ * </code>
121
+ *
122
+ * @param json
123
+ * String with json format
124
+ * @return
125
+ */
126
+ public static Map<String, Object> json2map(String json) throws JsonParseException {
133127
134
- try {
135
- if (json == null) {
136
- return null;
137
- }
138
- return MAPPER.readValue(json, Map.class);
139
- } catch (JsonParseException e) {
140
- LOG.error("Error parsing JSON string to map: {}", json, e);
141
- throw e;
142
- } catch (IOException e) {
143
- LOG.error("Error parsing JSON string to map: {}", json, e);
144
- }
145
- return null;
146
- }
128
+ try {
129
+ if (json == null)
130
+ return null;
131
+ return MAPPER.readValue(json, Map.class);
132
+ } catch (JsonParseException e) {
133
+ LOG.error("Error parsing JSON string to map: {}", json, e);
134
+ throw e;
135
+ } catch (IOException e) {
136
+ LOG.error("Error parsing JSON string to map: {}", json, e);
137
+ }
138
+ return null;
139
+ }
147140
148
- /**
149
- * Create a JSON strin from a Map object, i.e: <code>
150
- * {"f1":2345,"f2":"Test de valor"}
151
- *
152
- * @param map
153
- * @return
154
- */
155
- public static String map2json(Map<String, Object> map) {
156
- // and could also do other configuration...
157
- try {
158
- if (map == null) {
159
- return null;
160
- }
161
- return MAPPER.writeValueAsString(map);
162
- } catch (JsonProcessingException e) {
163
- LOG.error("Error formating JSON from map: {}", map, e);
164
- } catch (IOException e) {
165
- LOG.error("Error formating JSON from map: {}", map, e);
166
- }
141
+ /**
142
+ * Create a JSON strin from a Map object, i.e: <code>
143
+ * {"f1":2345,"f2":"Test de valor"}
144
+ *
145
+ * @param map
146
+ * @return
147
+ */
148
+ public static String map2json(Map<String, Object> map) {
149
+ // and could also do other configuration...
150
+ try {
151
+ if (map == null)
152
+ return null;
153
+ return MAPPER.writeValueAsString(map);
154
+ } catch (IOException e) {
155
+ LOG.error("Error formating JSON from map: {}", map, e);
156
+ }
167157
168
- return null;
169
- }
158
+ return null;
159
+ }
170160
171
- /**
172
- * Create a Map from a json string, i.e: <code>
173
- * [{"f1":2345}, {"f2":"Test de valor"}]
174
- * </code>
175
- *
176
- * @param json
177
- * String with json format
178
- * @return
179
- */
180
- @SuppressWarnings("unchecked")
181
- public static List<Object> json2list(String json) throws SeCurisException {
182
- try {
183
- return MAPPER.readValue(json, List.class);
184
- } catch (JsonParseException e) {
185
- LOG.error("Error converting JSON string to object {}", json, e);
186
- throw new SeCurisException("Error converting JSON to object", e);
187
- } catch (IOException e) {
188
- LOG.error("Error converting JSON string to object {}", json, e);
189
- throw new SeCurisException("Error converting JSON to object", e);
190
- }
191
- }
161
+ /**
162
+ * Create a Map from a json string, i.e: <code>
163
+ * [{"f1":2345}, {"f2":"Test de valor"}]
164
+ * </code>
165
+ *
166
+ * @param json
167
+ * String with json format
168
+ * @return
169
+ */
170
+ public static List<Object> json2list(String json) {
171
+ try {
172
+ return MAPPER.readValue(json, List.class);
173
+ } catch (JsonParseException e) {
174
+ LOG.error("Error converting JSON string to object {}", json, e);
175
+ } catch (IOException e) {
176
+ LOG.error("Error converting JSON string to object {}", json, e);
177
+ }
178
+ return null;
179
+ }
192180
193
- public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
194
- try {
195
- return MAPPER.readValue(json, classObject);
196
- } catch (JsonParseException e) {
197
- throw new SeCurisException("Error converting JSON to object", e);
198
- } catch (IOException e) {
199
- throw new SeCurisException("Error converting JSON to object", e);
200
- }
201
- }
181
+ public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
182
+ try {
183
+ return MAPPER.readValue(json, classObject);
184
+ } catch (JsonParseException e) {
185
+ throw new SeCurisException("Error converting JSON to object", e);
186
+ } catch (IOException e) {
187
+ throw new SeCurisException("Error converting JSON to object", e);
188
+ }
189
+ }
202190
203
- public static <T> T json2object(String json, TypeReference<T> typeReference) throws SeCurisException {
204
- try {
205
- return MAPPER.readValue(json, typeReference);
206
- } catch (JsonParseException e) {
207
- throw new SeCurisException("Error converting JSON to object", e);
208
- } catch (IOException e) {
209
- LOG.error("Error converting JSON to object", e);
210
- throw new SeCurisException("Error converting JSON to object", e);
211
- }
212
- }
191
+ public static <T> T json2object(String json, TypeReference<T> typeReference) throws SeCurisException {
192
+ try {
193
+ return MAPPER.readValue(json, typeReference);
194
+ } catch (JsonParseException e) {
195
+ throw new SeCurisException("Error converting JSON to object", e);
196
+ } catch (IOException e) {
197
+ LOG.error("Unexpected error in JSON to Object process", e);
198
+ throw new SeCurisException("Error converting JSON to object", e);
199
+ }
200
+ }
201
+
202
+ public static ObjectMapper getMapper() {
203
+ return MAPPER;
204
+ }
213205
214206 }