Fix serialization of the "key" property
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / event / envelope / ChampEventEnvelope.java
index f4a20a6..0dbe1d9 100644 (file)
 package org.onap.aai.champcore.event.envelope;
 
 import org.onap.aai.champcore.event.ChampEvent;
-import org.onap.aai.champcore.event.envelope.util.GsonUtil;
-import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
-import com.google.gson.Gson;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class ChampEventEnvelope {
 
     private ChampEventHeader header;
     private ChampEvent body;
 
-    /**
-     * Serializer/deserializer for converting to/from JSON.
-     */
-    private static final Gson gson = GsonUtil.createGson();
-
     public ChampEventEnvelope(ChampEvent event) {
         this.header = new ChampEventHeader.Builder(ChampEventHeader.EventType.UPDATE_NOTIFICATION)
                 .requestId(event.getTransactionId()).build();
@@ -69,24 +64,13 @@ public class ChampEventEnvelope {
      * @return - A JSON format string representation of this Vertex.
      */
     public String toJson() {
-        return gson.toJson(this);
-    }
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.setSerializationInclusion(Include.NON_NULL);
 
-    /**
-     * Deserializes the provided JSON string into a Event Envelope object.
-     *
-     * @param json the JSON string to produce the Event Envelope from.
-     * @return an Event Envelope object.
-     * @throws ChampUnmarshallingException
-     */
-    public static ChampEventEnvelope fromJson(String json) throws ChampUnmarshallingException {
         try {
-            if (json == null || json.isEmpty()) {
-                throw new ChampUnmarshallingException("Empty or null JSON string.");
-            }
-            return gson.fromJson(json, ChampEventEnvelope.class);
-        } catch (Exception ex) {
-            throw new ChampUnmarshallingException("Unable to parse JSON string: ");
+            return mapper.writeValueAsString(this);
+        } catch (JsonProcessingException e) {
+            return "Unmarshallable: " + e.getMessage();
         }
     }