added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / common / utility / gson / Convert.java
diff --git a/otf-service-api/src/main/java/org/oran/otf/common/utility/gson/Convert.java b/otf-service-api/src/main/java/org/oran/otf/common/utility/gson/Convert.java
new file mode 100644 (file)
index 0000000..bc1d0af
--- /dev/null
@@ -0,0 +1,95 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.common.utility.gson;\r
+\r
+import com.fasterxml.jackson.core.type.TypeReference;\r
+import com.fasterxml.jackson.databind.DeserializationFeature;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.google.gson.Gson;\r
+import com.google.gson.GsonBuilder;\r
+import com.google.gson.JsonDeserializationContext;\r
+import com.google.gson.JsonDeserializer;\r
+import com.google.gson.JsonElement;\r
+import com.google.gson.JsonParseException;\r
+import com.google.gson.JsonPrimitive;\r
+import com.google.gson.JsonSerializationContext;\r
+import com.google.gson.JsonSerializer;\r
+import com.google.gson.reflect.TypeToken;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.Type;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import org.bson.types.ObjectId;\r
+\r
+public class Convert {\r
+\r
+  private static final GsonBuilder gsonBuilder =\r
+      new GsonBuilder()\r
+          .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")\r
+          .registerTypeAdapter(\r
+              ObjectId.class,\r
+              new JsonSerializer<ObjectId>() {\r
+                @Override\r
+                public JsonElement serialize(\r
+                    ObjectId src, Type typeOfSrc, JsonSerializationContext context) {\r
+                  return new JsonPrimitive(src.toHexString());\r
+                }\r
+              })\r
+          .registerTypeAdapter(\r
+              ObjectId.class,\r
+              new JsonDeserializer<ObjectId>() {\r
+                @Override\r
+                public ObjectId deserialize(\r
+                    JsonElement json, Type typeOfT, JsonDeserializationContext context)\r
+                    throws JsonParseException {\r
+                  return new ObjectId(json.getAsString());\r
+                }\r
+              });\r
+\r
+  public static Gson getGson() {\r
+    return gsonBuilder.create();\r
+  }\r
+\r
+  public static String mapToJson(Map map) {\r
+    if (map.isEmpty()) {\r
+      return "{}";\r
+    }\r
+    return getGson().toJson(map);\r
+  }\r
+\r
+  public static Map<String, Object> jsonToMap(String json) {\r
+    Type type = new TypeToken<HashMap<String, Object>>() {\r
+    }.getType();\r
+    return getGson().fromJson(json, type);\r
+  }\r
+\r
+  public static String objectToJson(Object obj) {\r
+    return getGson().toJson(obj);\r
+  }\r
+\r
+  public static<T> T mapToObject(Map map, TypeReference<T> typeReference) throws IOException {\r
+    return jsonToObject(mapToJson(map), typeReference);\r
+  }\r
+\r
+  public static <T> T jsonToObject(String json, TypeReference<T> typeReference) throws IOException {\r
+    ObjectMapper objectMapper = new ObjectMapper();\r
+    objectMapper\r
+        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);\r
+    return objectMapper.readValue(json, typeReference);\r
+  }\r
+}\r