From ffc60638fba7475b4cb6a863aa8f27c7e5a9b059 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Wed, 22 Oct 2014 17:07:40 +0000
Subject: [PATCH] #2021 feature - Updated JSON library to the latest version

---
 src/main/java/net/curisit/securis/utils/JsonUtils.java |  352 ++++++++++++++++++++++++++++------------------------------
 1 files changed, 172 insertions(+), 180 deletions(-)

diff --git a/src/main/java/net/curisit/securis/utils/JsonUtils.java b/src/main/java/net/curisit/securis/utils/JsonUtils.java
index 3ac49c3..1492de4 100644
--- a/src/main/java/net/curisit/securis/utils/JsonUtils.java
+++ b/src/main/java/net/curisit/securis/utils/JsonUtils.java
@@ -8,207 +8,199 @@
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
-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 com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+//import org.codehaus.jackson.map.ObjectMapper;
 
 /**
  * Helper method to perform JSON tasks
  * 
- * @author cproberto
+ * @author cproberto, ccalvo
  */
 public class JsonUtils {
 
-	private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
+    private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
 
-	final private static ObjectMapper MAPPER = new ObjectMapper();
+    final private static ObjectMapper MAPPER = new ObjectMapper();
+    final private static ObjectMapper MAPPER_PRETTY;
 
-	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);
+    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.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+        MAPPER.disable(SerializationFeature.INDENT_OUTPUT);
+        MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+        MAPPER.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
+        MAPPER_PRETTY = MAPPER.copy();
+        MAPPER_PRETTY.enable(SerializationFeature.INDENT_OUTPUT);
+    }
 
-	/**
-	 * Convert an object in the type pass as parameter, avoiding to use casting in code.
-	 * 
-	 * @param value
-	 * @param type
-	 * @return
-	 */
-	@SuppressWarnings("unchecked")
-	public static <T> T value(Object value, Class<T> type) {
+    /**
+     * 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;
-	}
+        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);
-		}
-	}
+    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) throws SeCurisException {
+        // and could also do other configuration...
+        try {
+            if (obj == null)
+                return null;
+            return MAPPER.writeValueAsString(obj);
+        } 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 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 toPrettyJSON(Object obj) throws SeCurisException {
+        // and could also do other configuration...
+        try {
+            if (obj == null)
+                return null;
+            return MAPPER_PRETTY.writeValueAsString(obj);
+        } 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
-	 */
-	@SuppressWarnings("unchecked")
-	public static Map<String, Object> json2map(String json) throws JsonParseException {
+    /**
+     * 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;
-	}
+        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);
-		}
+    /**
+     * 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 (IOException e) {
+            LOG.error("Error formating JSON from map: {}", map, e);
+        }
 
-		return null;
-	}
+        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
-	 */
-	@SuppressWarnings("unchecked")
-	public static List<Object> json2list(String json) throws SeCurisException {
-		try {
-			return MAPPER.readValue(json, List.class);
-		} catch (JsonParseException e) {
-			LOG.error("Error converting JSON string to object {}", json, e);
-			throw new SeCurisException("Error converting JSON to object", e);
-		} catch (IOException e) {
-			LOG.error("Error converting JSON string to object {}", json, e);
-			throw new SeCurisException("Error converting JSON to object", 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 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, 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) {
-			LOG.error("Error converting JSON to object", 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) {
+            LOG.error("Unexpected error in JSON to Object process", e);
+            throw new SeCurisException("Error converting JSON to object", e);
+        }
+    }
+
+    public static ObjectMapper getMapper() {
+        return MAPPER;
+    }
 
 }

--
Gitblit v1.3.2