| .. | .. |
|---|
| 83 | 83 | public static String toJSON(Object obj) throws SeCurisException { |
|---|
| 84 | 84 | // and could also do other configuration... |
|---|
| 85 | 85 | try { |
|---|
| 86 | | - if (obj == null) |
|---|
| 86 | + if (obj == null) { |
|---|
| 87 | 87 | return null; |
|---|
| 88 | + } |
|---|
| 88 | 89 | return MAPPER.writeValueAsString(obj); |
|---|
| 89 | 90 | } catch (IOException e) { |
|---|
| 90 | 91 | LOG.error("Error formating JSON from object: {}", obj, e); |
|---|
| .. | .. |
|---|
| 100 | 101 | * @param obj |
|---|
| 101 | 102 | * @return JSON string representation from object |
|---|
| 102 | 103 | */ |
|---|
| 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 | + */ |
|---|
| 103 | 125 | public static String toPrettyJSON(Object obj) throws SeCurisException { |
|---|
| 104 | 126 | // and could also do other configuration... |
|---|
| 105 | 127 | try { |
|---|