| 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 | @SuppressWarnings("unchecked") | |
| 19 | public class JacksonJsonTools<T> implements JsonTools<T> { | |
| 20 | ||
| 21 | private static final long serialVersionUID = 15L; | |
| 22 | private static final Logger LOGGER = new LogbackLogger(JacksonJsonTools.class); | |
| 23 | ||
| 24 | /** | |
| 25 | * Constructor. | |
| 26 | */ | |
| 27 | public JacksonJsonTools() { | |
| 28 | LOGGER.log("instance class", LogLevel.INFO); | |
| 29 | } | |
| 30 | ||
| 31 | @Override | |
| 32 | public String serializeAsJsonObject(final T object) | |
| 33 | throws JsonProcessingException { | |
| 34 | try { | |
| 35 |
1
1. serializeAsJsonObject : negated conditional → KILLED |
if (object == null) { |
| 36 | throw new JsonProcessingException("Object is null."); | |
| 37 | } | |
| 38 | ObjectMapper mapper = new ObjectMapper(); | |
| 39 |
1
1. serializeAsJsonObject : replaced return value with "" for org/europa/together/application/JacksonJsonTools::serializeAsJsonObject → KILLED |
return mapper.writeValueAsString(object); |
| 40 | } catch (com.fasterxml.jackson.core.JsonProcessingException ex) { | |
| 41 | LOGGER.catchException(ex); | |
| 42 | throw new JsonProcessingException(ex.getOriginalMessage()); | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | @Override | |
| 47 | public T deserializeJsonAsObject(final String json, final Class<T> object) | |
| 48 | throws JsonProcessingException, ClassNotFoundException { | |
| 49 | try { | |
| 50 | Class<?> clazz = Class.forName(object.getCanonicalName()); | |
| 51 | ObjectMapper mapper = new ObjectMapper(); | |
| 52 |
1
1. deserializeJsonAsObject : replaced return value with null for org/europa/together/application/JacksonJsonTools::deserializeJsonAsObject → KILLED |
return (T) mapper.readValue(json, clazz); |
| 53 | } catch (com.fasterxml.jackson.core.JsonProcessingException ex) { | |
| 54 | throw new JsonProcessingException(ex.getOriginalMessage()); | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | public List<T> deserializeJsonAsList(final String json) | |
| 60 | throws JsonProcessingException, ClassNotFoundException { | |
| 61 | try { | |
| 62 | ObjectMapper mapper = new ObjectMapper(); | |
| 63 |
1
1. deserializeJsonAsList : replaced return value with Collections.emptyList for org/europa/together/application/JacksonJsonTools::deserializeJsonAsList → KILLED |
return mapper.readValue(json, new TypeReference<List<T>>() { |
| 64 | }); | |
| 65 | } catch (com.fasterxml.jackson.core.JsonProcessingException ex) { | |
| 66 | throw new JsonProcessingException(ex.getOriginalMessage()); | |
| 67 | } | |
| 68 | } | |
| 69 | } | |
Mutations | ||
| 35 |
1.1 |
|
| 39 |
1.1 |
|
| 52 |
1.1 |
|
| 63 |
1.1 |