package net.curisit.securis.utils; import java.io.IOException; import java.util.List; import java.util.Map; import net.curisit.securis.SeCurisException; 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; /** * Helper method to perform JSON tasks * * @author cproberto */ public class JsonUtils { private static final Logger log = LogManager.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 */ @SuppressWarnings("unchecked") public static T value(Object value, Class type) { return (T) value; } public static T parseJSON(String json, Class 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: * {"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: * {"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: * {"f1":2345,"f2":"Test de valor"} * * * @param json * String with json format * @return */ @SuppressWarnings("unchecked") public static Map 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: * {"f1":2345,"f2":"Test de valor"} * * @param map * @return */ public static String map2json(Map 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: * [{"f1":2345}, {"f2":"Test de valor"}] * * * @param json * String with json format * @return */ @SuppressWarnings("unchecked") public static List 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); } } public static T json2object(String json, Class 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 json2object(String json, TypeReference 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); } } }