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/LicenseValidator.java | 4
src/main/java/net/curisit/securis/beans/LicenseBean.java | 8
src/main/java/net/curisit/securis/beans/SignedLicenseBean.java | 5
src/main/java/net/curisit/securis/ConnectionManager.java | 107 +++++------
src/main/java/net/curisit/securis/utils/JsonUtils.java | 352 +++++++++++++++++++--------------------
src/main/java/net/curisit/securis/LicenseManager.java | 2
src/main/java/net/curisit/securis/beans/StatusBean.java | 2
src/main/java/net/curisit/securis/ReqGenerator.java | 2
src/main/java/net/curisit/securis/beans/RequestBean.java | 7
pom.xml | 10
10 files changed, 244 insertions(+), 255 deletions(-)
diff --git a/pom.xml b/pom.xml
index 225d7c5..914f385 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,11 +47,6 @@
<version>1.2</version>
</dependency>
<dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
@@ -61,6 +56,11 @@
<artifactId>httpclient</artifactId>
<version>4.4-beta1</version>
</dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.4.3</version>
+ </dependency>
</dependencies>
<distributionManagement>
diff --git a/src/main/java/net/curisit/securis/ConnectionManager.java b/src/main/java/net/curisit/securis/ConnectionManager.java
index dde3e6f..56c7ce9 100644
--- a/src/main/java/net/curisit/securis/ConnectionManager.java
+++ b/src/main/java/net/curisit/securis/ConnectionManager.java
@@ -1,6 +1,5 @@
package net.curisit.securis;
-import java.awt.PageAttributes.MediaType;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
@@ -9,8 +8,6 @@
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
-
-import javax.activation.MimeType;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.utils.JsonUtils;
@@ -36,57 +33,56 @@
*/
public class ConnectionManager {
- private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
- private static final int HTTP_STATUS_APP_ERRROR = 418;
- private static final String JSON_MEDIA_TYPE= "application/json";
+ private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
+ private static final int HTTP_STATUS_APP_ERRROR = 418;
+ private static final String JSON_MEDIA_TYPE = "application/json";
private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
- private static ConnectionManager singleton;
+ private static ConnectionManager singleton;
- private final String serverUrl;
- private final CloseableHttpClient httpClient;
-
- private ConnectionManager() throws SeCurisException {
- String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
- if (aux.endsWith("/")) {
- serverUrl = aux.substring(0, aux.length()-2);
- } else {
- serverUrl = aux;
- }
- httpClient = createHttpClient();
- }
-
- private CloseableHttpClient createHttpClient() throws SeCurisException {
- SSLContextBuilder builder = new SSLContextBuilder();
- SSLConnectionSocketFactory sslsf = null;
- try {
- builder.loadTrustMaterial((KeyStore)null, new TrustStrategy() {
- @Override
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- return true;
- }
- });
- sslsf = new SSLConnectionSocketFactory(builder.build());
- } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
- LOG.error(e1);
- throw new SeCurisException("Error creating SSL socket factory");
- }
- return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
- }
+ private final String serverUrl;
+ private final CloseableHttpClient httpClient;
- public synchronized static ConnectionManager getInstance() throws SeCurisException {
- if (singleton == null) {
- singleton = new ConnectionManager();
- }
- return singleton;
- }
+ private ConnectionManager() throws SeCurisException {
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
+ if (aux.endsWith("/")) {
+ serverUrl = aux.substring(0, aux.length() - 2);
+ } else {
+ serverUrl = aux;
+ }
+ httpClient = createHttpClient();
+ }
+ private CloseableHttpClient createHttpClient() throws SeCurisException {
+ SSLContextBuilder builder = new SSLContextBuilder();
+ SSLConnectionSocketFactory sslsf = null;
+ try {
+ builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
+ @Override
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+ return true;
+ }
+ });
+ sslsf = new SSLConnectionSocketFactory(builder.build());
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
+ LOG.error(e1);
+ throw new SeCurisException("Error creating SSL socket factory");
+ }
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
+ }
- public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
+ if (singleton == null) {
+ singleton = new ConnectionManager();
+ }
+ return singleton;
+ }
+
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
postRequest.addHeader("accept", JSON_MEDIA_TYPE);
-
+
postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
try {
postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
@@ -96,23 +92,23 @@
HttpResponse response;
try {
response = httpClient.execute(postRequest);
-
+
checkErrors(command, response);
-
+
String jsonLic = IOUtils.toString(response.getEntity().getContent());
LOG.debug("Response content read OK: {}", jsonLic);
T responseBean = JsonUtils.json2object(jsonLic, returnType);
LOG.debug("Response bean read OK: {}", responseBean);
-
+
return responseBean;
} catch (IOException e) {
LOG.error("Error accessing SeCuris server", e);
throw new SeCurisException("Error accessing SeCuris server");
}
- }
+ }
- private void checkErrors(String command, HttpResponse response) throws SeCurisException {
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
if (response.getStatusLine().getStatusCode() != 200) {
if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
@@ -123,7 +119,7 @@
throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
}
- }
+ }
public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
@@ -140,19 +136,18 @@
T responseBean = JsonUtils.json2object(jsonLic, returnType);
LOG.debug("Response bean read OK: {}", responseBean);
-
+
return responseBean;
} catch (IOException e) {
LOG.error("Error acessing SeCuris server", e);
throw new SeCurisException("Error accessing SeCuris server");
}
}
-
- public static class Command {
+
+ public static class Command {
public static final String TEST = "ping";
public static final String CREATE_LIC = "request";
public static final String RENEW_LIC = "renew";
- }
-
+ }
}
diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index 1298d81..e6bf728 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -147,7 +147,7 @@
public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
byte[] json;
try {
- json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (UnsupportedEncodingException e) {
LOG.error("Error creating json doc from license: " + signedLic, e);
diff --git a/src/main/java/net/curisit/securis/LicenseValidator.java b/src/main/java/net/curisit/securis/LicenseValidator.java
index ad4bd29..3022aea 100644
--- a/src/main/java/net/curisit/securis/LicenseValidator.java
+++ b/src/main/java/net/curisit/securis/LicenseValidator.java
@@ -61,8 +61,8 @@
public void validateHW(RequestBean reqBean, String appCode, String customerCode, String packCode) throws SeCurisException {
RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode, packCode);
if (!currentHW.match(reqBean)) {
- throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: " + JsonUtils.toJSON(reqBean, true)
- + "\n Expected: " + JsonUtils.toJSON(currentHW, true));
+ throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n" + JsonUtils.toPrettyJSON(reqBean)
+ + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
}
}
diff --git a/src/main/java/net/curisit/securis/ReqGenerator.java b/src/main/java/net/curisit/securis/ReqGenerator.java
index d486a63..4614a3b 100644
--- a/src/main/java/net/curisit/securis/ReqGenerator.java
+++ b/src/main/java/net/curisit/securis/ReqGenerator.java
@@ -65,7 +65,7 @@
public void save(RequestBean req, File file) throws SeCurisException {
byte[] json;
try {
- json = JsonUtils.toJSON(req, true).getBytes("utf-8");
+ json = JsonUtils.toPrettyJSON(req).getBytes("utf-8");
Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (UnsupportedEncodingException e) {
LOG.error("Error creating json doc from request: " + req, e);
diff --git a/src/main/java/net/curisit/securis/beans/LicenseBean.java b/src/main/java/net/curisit/securis/beans/LicenseBean.java
index 6fad59c..6a4a1e0 100644
--- a/src/main/java/net/curisit/securis/beans/LicenseBean.java
+++ b/src/main/java/net/curisit/securis/beans/LicenseBean.java
@@ -4,10 +4,10 @@
import java.util.Map;
import java.util.TreeMap;
-import org.codehaus.jackson.annotate.JsonAutoDetect;
-import org.codehaus.jackson.annotate.JsonIgnore;
-import org.codehaus.jackson.annotate.JsonProperty;
-import org.codehaus.jackson.annotate.JsonPropertyOrder;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonAutoDetect
@JsonPropertyOrder({
diff --git a/src/main/java/net/curisit/securis/beans/RequestBean.java b/src/main/java/net/curisit/securis/beans/RequestBean.java
index 2a79911..3f79375 100644
--- a/src/main/java/net/curisit/securis/beans/RequestBean.java
+++ b/src/main/java/net/curisit/securis/beans/RequestBean.java
@@ -2,11 +2,12 @@
import java.util.List;
-import org.codehaus.jackson.annotate.JsonAutoDetect;
-import org.codehaus.jackson.map.annotate.JsonSerialize;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonAutoDetect
-@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+@JsonInclude(Include.NON_NULL)
public class RequestBean {
private String customerCode;
private String crcLogo;
diff --git a/src/main/java/net/curisit/securis/beans/SignedLicenseBean.java b/src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
index 94e127e..5545c61 100644
--- a/src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
+++ b/src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
@@ -1,7 +1,8 @@
package net.curisit.securis.beans;
-import org.codehaus.jackson.annotate.JsonAutoDetect;
-import org.codehaus.jackson.annotate.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
@JsonAutoDetect
public class SignedLicenseBean extends LicenseBean {
diff --git a/src/main/java/net/curisit/securis/beans/StatusBean.java b/src/main/java/net/curisit/securis/beans/StatusBean.java
index a008b7f..c28b08f 100644
--- a/src/main/java/net/curisit/securis/beans/StatusBean.java
+++ b/src/main/java/net/curisit/securis/beans/StatusBean.java
@@ -2,7 +2,7 @@
import java.util.Date;
-import org.codehaus.jackson.annotate.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
@JsonAutoDetect
public class StatusBean extends RequestBean {
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