1 package org.openecomp.sdc.securityutil;
3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.databind.DeserializationFeature;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.fasterxml.jackson.databind.SerializationFeature;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
10 import java.io.IOException;
12 public class RepresentationUtils {
14 private static final Logger log = LoggerFactory.getLogger(RepresentationUtils.class.getName());
17 * Build JSON Representation of given Object
19 * @param elementToRepresent
23 public static <T> String toRepresentation(T elementToRepresent) throws IOException {
25 ObjectMapper mapper = new ObjectMapper();
26 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
27 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
28 return mapper.writeValueAsString(elementToRepresent);
32 * Convert JSON representation to given class
39 public static <T> T fromRepresentation(String json, Class<T> clazz) {
40 ObjectMapper mapper = new ObjectMapper();
42 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
43 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
44 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
46 object = mapper.readValue(json, clazz);
47 } catch (Exception e) {
48 log.error("Error when parsing JSON of object of type {}", clazz.getSimpleName(), e);
49 } // return null in case of exception