rsanchez
2014-12-02 cd3cac266bd33f09a328e4ebfd0dfd5b2c3e408e
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -83,8 +83,9 @@
8383 public static String toJSON(Object obj) throws SeCurisException {
8484 // and could also do other configuration...
8585 try {
86
- if (obj == null)
86
+ if (obj == null) {
8787 return null;
88
+ }
8889 return MAPPER.writeValueAsString(obj);
8990 } catch (IOException e) {
9091 LOG.error("Error formating JSON from object: {}", obj, e);
....@@ -100,6 +101,27 @@
100101 * @param obj
101102 * @return JSON string representation from object
102103 */
104
+ public static String toJSON(Object obj, Class<?> klass) throws SeCurisException {
105
+ // and could also do other configuration...
106
+ try {
107
+ if (obj == null) {
108
+ return null;
109
+ }
110
+ return MAPPER.writerWithType(klass).writeValueAsString(obj);
111
+ } catch (IOException e) {
112
+ LOG.error("Error formating JSON from object: {}", obj, e);
113
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Create a JSON string from a object compatible or annotated with Jackson,
119
+ * i.e: <code>
120
+ * {"f1":2345,"f2":"Test de valor"}
121
+ *
122
+ * @param obj
123
+ * @return JSON string representation from object
124
+ */
103125 public static String toPrettyJSON(Object obj) throws SeCurisException {
104126 // and could also do other configuration...
105127 try {