Roberto Sánchez
2014-09-18 ca4f38f1305667758c2c2e90b45cd19bdb532721
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -45,6 +45,7 @@
4545 * @param type
4646 * @return
4747 */
48
+ @SuppressWarnings("unchecked")
4849 public static <T> T value(Object value, Class<T> type) {
4950
5051 return (T) value;
....@@ -52,8 +53,9 @@
5253
5354 public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
5455 try {
55
- if (json == null)
56
+ if (json == null){
5657 return null;
58
+ }
5759 return MAPPER.readValue(json, type);
5860 } catch (JsonParseException e) {
5961 log.error("Error parsing JSON string to obejct: {}", json, e);
....@@ -78,8 +80,9 @@
7880 public static String toJSON(Object obj) throws SeCurisException {
7981 // and could also do other configuration...
8082 try {
81
- if (obj == null)
83
+ if (obj == null) {
8284 return null;
85
+ }
8386 return MAPPER.writeValueAsString(obj);
8487 } catch (JsonProcessingException e) {
8588 log.error("Error formating JSON from object: {}", obj, e);
....@@ -100,8 +103,9 @@
100103 public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
101104 // and could also do other configuration...
102105 try {
103
- if (obj == null)
106
+ if (obj == null) {
104107 return null;
108
+ }
105109 MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
106110 String json = MAPPER.writeValueAsString(obj);
107111 MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
....@@ -124,11 +128,13 @@
124128 * String with json format
125129 * @return
126130 */
131
+ @SuppressWarnings("unchecked")
127132 public static Map<String, Object> json2map(String json) throws JsonParseException {
128133
129134 try {
130
- if (json == null)
135
+ if (json == null) {
131136 return null;
137
+ }
132138 return MAPPER.readValue(json, Map.class);
133139 } catch (JsonParseException e) {
134140 log.error("Error parsing JSON string to map: {}", json, e);
....@@ -149,8 +155,9 @@
149155 public static String map2json(Map<String, Object> map) {
150156 // and could also do other configuration...
151157 try {
152
- if (map == null)
158
+ if (map == null) {
153159 return null;
160
+ }
154161 return MAPPER.writeValueAsString(map);
155162 } catch (JsonProcessingException e) {
156163 log.error("Error formating JSON from map: {}", map, e);
....@@ -170,15 +177,17 @@
170177 * String with json format
171178 * @return
172179 */
173
- public static List<Object> json2list(String json) {
180
+ @SuppressWarnings("unchecked")
181
+ public static List<Object> json2list(String json) throws SeCurisException {
174182 try {
175183 return MAPPER.readValue(json, List.class);
176184 } catch (JsonParseException e) {
177185 log.error("Error converting JSON string to object {}", json, e);
186
+ throw new SeCurisException("Error converting JSON to object", e);
178187 } catch (IOException e) {
179188 log.error("Error converting JSON string to object {}", json, e);
189
+ throw new SeCurisException("Error converting JSON to object", e);
180190 }
181
- return null;
182191 }
183192
184193 public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {