Created
November 17, 2013 12:26
-
-
Save jonasurbano/7512763 to your computer and use it in GitHub Desktop.
JsonMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.pfc.utils; | |
import java.io.ByteArrayInputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.log4j.Logger; | |
import org.codehaus.jackson.JsonFactory; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.type.TypeReference; | |
public class JsonMap { | |
private static Logger logger = Logger.getLogger(JsonMap.class); | |
/** | |
* Convierte un String que contiene un JSON a Map | |
* @return Map sin valores si algún error ocurrió | |
*/ | |
public static Map<String, String> toMap(String json) { | |
Map<String, String> mapa = new HashMap<String,String>(); | |
if (json == null) return mapa; | |
JsonFactory factory = new JsonFactory(); | |
ObjectMapper mapper = new ObjectMapper(factory); | |
TypeReference<HashMap<String, String>> typeRef = | |
new TypeReference<HashMap<String, String>>() {}; | |
try { | |
mapa = mapper.readValue( | |
new ByteArrayInputStream(json.getBytes("UTF-8")), typeRef); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
logger.error("No se pudo deserializar correctamente",e); | |
} | |
return mapa; | |
} | |
/** | |
* Añade "clave":"valor" a {@code json} | |
* @param json String con un JSON al que añadir el nuevo "clave":"valor" | |
* @param clave si existe, se sobreescribe valor | |
* @return {@code json} si algún error ocurrió | |
*/ | |
public static String toJson(String clave,String valor,String json) { | |
if (clave == null || valor == null) return json; | |
Map<String,String> mapa = toMap(json); | |
mapa.put(clave, valor); | |
JsonFactory factory = new JsonFactory(); | |
ObjectMapper mapper = new ObjectMapper(factory); | |
try { | |
return mapper.writeValueAsString(mapa); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
logger.error("No se pudo serializar correctamente",e); | |
} | |
return json; | |
} | |
/** | |
* Añade "clave":"valor" a {@code mapa} y lo convierte a un JSON | |
* @param clave si existe, se sobreescribe valor | |
* @return {@code null} si algún error ocurrió | |
*/ | |
public static String toJson(String clave,String valor,Map<String,String> mapa) { | |
if (mapa == null) mapa = new HashMap<String,String>(); | |
if (clave != null && valor != null) { | |
mapa.put(clave, valor); | |
} | |
JsonFactory factory = new JsonFactory(); | |
ObjectMapper mapper = new ObjectMapper(factory); | |
try { | |
return mapper.writeValueAsString(mapa); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
logger.error("No se pudo serializar correctamente",e); | |
} | |
return null; | |
} | |
/** | |
* Crea un JSON con {@code "clave":"valor"} | |
* @return {@code null} si algún error ocurrió | |
*/ | |
public static String toJson(String clave,String valor) { | |
return toJson(clave, valor, (String)null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment