1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File JacksonJsonTools.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
17
4
1
70
54
8
0.47
4.25
4
2

Classes

Class Line # Actions
JacksonJsonTools 18 17 0% 8 2
0.913043591.3%
 

Contributing tests

This file is covered by 9 tests. .

Source view

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    * Implementation of JavaScript Object Notation (JSON) processing.
14    *
15    * @param <T>
16    */
17    @Repository
 
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    * Constructor.
25    */
 
26  1 toggle public JacksonJsonTools() {
27  1 LOGGER.log("instance class", LogLevel.INFO);
28    }
29   
 
30  3 toggle @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   
 
45  3 toggle @Override
46    @SuppressWarnings("unchecked")
47    //TODO: TP-CORE Release 4.0 (API Change) try to get rid of the object Parameter.
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   
 
59  3 toggle @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    }