Roberto Sánchez
2014-02-21 8cc95fdfbe8146af8d5d4bcac7f7b9e3e2eb95c6
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package net.curisit.securis.utils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import net.curisit.securis.SeCurisException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Helper method to perform JSON tasks
 * 
 * @author cproberto
 */
public class JsonUtils {
   private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
   final private static ObjectMapper MAPPER = new ObjectMapper();
   static {
       MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
       MAPPER.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
       MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
       MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
       MAPPER.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
       MAPPER.configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
       MAPPER.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
       MAPPER.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
   }
   /**
    * Convert an object in the type pass as parameter, avoiding to use casting in code.
    * 
    * @param value
    * @param type
    * @return
    */
   public static <T> T value(Object value, Class<T> type) {
       return (T) value;
   }
   public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
       try {
           if (json == null)
               return null;
           return MAPPER.readValue(json, type);
       } catch (JsonParseException e) {
           log.error("Error parsing JSON string to obejct: {}", json, e);
           if (json.length() > 60)
               json = json.substring(0, 50) + "...";
           throw new SeCurisException("Error parsing JSON string to object: " + json, e);
       } catch (IOException e) {
           log.error("Error parsing JSON string to object: {}", json, e);
           if (json.length() > 60)
               json = json.substring(0, 50) + "...";
           throw new SeCurisException("Error parsing JSON string to object: " + json, e);
       }
   }
   /**
    * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
    * {"f1":2345,"f2":"Test de valor"}
    * 
    * @param obj
    * @return JSON string representation from object
    */
   public static String toJSON(Object obj) throws SeCurisException {
       // and could also do other configuration...
       try {
           if (obj == null)
               return null;
           return MAPPER.writeValueAsString(obj);
       } catch (JsonProcessingException e) {
           log.error("Error formating JSON from object: {}", obj, e);
           throw new SeCurisException("Error formating JSON from object: " + obj, e);
       } catch (IOException e) {
           log.error("Error formating JSON from object: {}", obj, e);
           throw new SeCurisException("Error formating JSON from object: " + obj, e);
       }
   }
   /**
    * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
    * {"f1":2345,"f2":"Test de valor"}
    * 
    * @param obj
    * @return JSON string representation from object
    */
   public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
       // and could also do other configuration...
       try {
           if (obj == null)
               return null;
           MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
           String json = MAPPER.writeValueAsString(obj);
           MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
           return json;
       } catch (JsonProcessingException e) {
           log.error("Error formating JSON from object: {}", obj, e);
           throw new SeCurisException("Error formating JSON from object: " + obj, e);
       } catch (IOException e) {
           log.error("Error formating JSON from object: {}", obj, e);
           throw new SeCurisException("Error formating JSON from object: " + obj, e);
       }
   }
   /**
    * Create a Map from a json string, i.e: <code>
    * {"f1":2345,"f2":"Test de valor"}
    * </code>
    * 
    * @param json
    *            String with json format
    * @return
    */
   public static Map<String, Object> json2map(String json) throws JsonParseException {
       try {
           if (json == null)
               return null;
           return MAPPER.readValue(json, Map.class);
       } catch (JsonParseException e) {
           log.error("Error parsing JSON string to map: {}", json, e);
           throw e;
       } catch (IOException e) {
           log.error("Error parsing JSON string to map: {}", json, e);
       }
       return null;
   }
   /**
    * Create a JSON strin from a Map object, i.e: <code>
    * {"f1":2345,"f2":"Test de valor"}
    * 
    * @param map
    * @return
    */
   public static String map2json(Map<String, Object> map) {
       // and could also do other configuration...
       try {
           if (map == null)
               return null;
           return MAPPER.writeValueAsString(map);
       } catch (JsonProcessingException e) {
           log.error("Error formating JSON from map: {}", map, e);
       } catch (IOException e) {
           log.error("Error formating JSON from map: {}", map, e);
       }
       return null;
   }
   /**
    * Create a Map from a json string, i.e: <code>
    * [{"f1":2345}, {"f2":"Test de valor"}]
    * </code>
    * 
    * @param json
    *            String with json format
    * @return
    */
   public static List<Object> json2list(String json) {
       try {
           return MAPPER.readValue(json, List.class);
       } catch (JsonParseException e) {
           log.error("Error converting JSON string to object {}", json, e);
       } catch (IOException e) {
           log.error("Error converting JSON string to object {}", json, e);
       }
       return null;
   }
   public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
       try {
           return MAPPER.readValue(json, classObject);
       } catch (JsonParseException e) {
           throw new SeCurisException("Error converting JSON to object", e);
       } catch (IOException e) {
           throw new SeCurisException("Error converting JSON to object", e);
       }
   }
   public static <T> T json2object(String json, TypeReference<T> typeReference) throws SeCurisException {
       try {
           return MAPPER.readValue(json, typeReference);
       } catch (JsonParseException e) {
           throw new SeCurisException("Error converting JSON to object", e);
       } catch (IOException e) {
           e.printStackTrace();
           throw new SeCurisException("Error converting JSON to object", e);
       }
   }
}