NCMP : Handle non-existing and non-ready cm handles
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / JsonObjectMapper.java
index 2459b51..60a6e16 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.cps.utils;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -87,4 +88,36 @@ public class JsonObjectMapper {
                     + "JSON content to specific class type.", e.getMessage());
         }
     }
+
+    /**
+     * Serializing generic json object to bytes using Jackson.
+     *
+     * @param jsonObject any json object value
+     * @return the generated JSON as a byte array.
+     */
+    public byte[] asJsonBytes(final Object jsonObject) {
+        try {
+            return objectMapper.writeValueAsBytes(jsonObject);
+        } catch (final JsonProcessingException jsonProcessingException) {
+            log.error("Parsing error occurred while converting JSON object to bytes.");
+            throw new DataValidationException("Parsing error occurred while converting given JSON object to bytes.",
+                    jsonProcessingException.getMessage());
+        }
+    }
+
+    /**
+     * Deserialize JSON content from given JSON content String to JsonNode.
+     *
+     * @param jsonContent   JSON content
+     * @return a json node
+     */
+    public JsonNode convertToJsonNode(final String jsonContent) {
+        try {
+            return objectMapper.readTree(jsonContent);
+        } catch (final JsonProcessingException e) {
+            log.error("Parsing error occurred while converting JSON content to Json Node.");
+            throw new DataValidationException("Parsing error occurred while converting "
+                    + "JSON content to Json Node.", e.getMessage());
+        }
+    }
 }