1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import com.fasterxml.jackson.core.type.TypeReference; |
4 |
|
import org.europa.together.exceptions.JsonProcessingException; |
5 |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
6 |
|
import java.util.List; |
7 |
|
import org.europa.together.business.JsonTools; |
8 |
|
import org.europa.together.business.Logger; |
9 |
|
import org.europa.together.domain.LogLevel; |
10 |
|
import org.springframework.stereotype.Repository; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
@param |
16 |
|
|
17 |
|
@Repository |
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 8 |
Complexity Density: 0.47 |
|
18 |
|
public class JacksonJsonTools<T> implements JsonTools<T> { |
19 |
|
|
20 |
|
private static final long serialVersionUID = 15L; |
21 |
|
private static final Logger LOGGER = new LogbackLogger(JacksonJsonTools.class); |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
26 |
1 |
public JacksonJsonTools() {... |
27 |
1 |
LOGGER.log("instance class", LogLevel.INFO); |
28 |
|
} |
29 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
30 |
3 |
@Override... |
31 |
|
public String serializeAsJsonObject(final T object) |
32 |
|
throws JsonProcessingException { |
33 |
3 |
try { |
34 |
3 |
if (object == null) { |
35 |
1 |
throw new JsonProcessingException("Object is null."); |
36 |
|
} |
37 |
2 |
ObjectMapper mapper = new ObjectMapper(); |
38 |
2 |
return mapper.writeValueAsString(object); |
39 |
|
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) { |
40 |
0 |
LOGGER.catchException(ex); |
41 |
0 |
throw new JsonProcessingException(ex.getOriginalMessage()); |
42 |
|
} |
43 |
|
} |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
45 |
3 |
@Override... |
46 |
|
@SuppressWarnings("unchecked") |
47 |
|
|
48 |
|
public T deserializeJsonAsObject(final String json, final Class<T> object) |
49 |
|
throws JsonProcessingException, ClassNotFoundException { |
50 |
3 |
try { |
51 |
3 |
Class<?> clazz = Class.forName(object.getCanonicalName()); |
52 |
3 |
ObjectMapper mapper = new ObjectMapper(); |
53 |
3 |
return (T) mapper.readValue(json, clazz); |
54 |
|
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) { |
55 |
1 |
throw new JsonProcessingException(ex.getOriginalMessage()); |
56 |
|
} |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
59 |
3 |
@Override... |
60 |
|
public List<T> deserializeJsonAsList(final String json) |
61 |
|
throws JsonProcessingException, ClassNotFoundException { |
62 |
3 |
try { |
63 |
3 |
ObjectMapper mapper = new ObjectMapper(); |
64 |
3 |
return mapper.readValue(json, new TypeReference<List<T>>() { |
65 |
|
}); |
66 |
|
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) { |
67 |
1 |
throw new JsonProcessingException(ex.getOriginalMessage()); |
68 |
|
} |
69 |
|
} |
70 |
|
} |