added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / common / utility / gson / GsonUtils.java
diff --git a/otf-service-api/src/main/java/org/oran/otf/common/utility/gson/GsonUtils.java b/otf-service-api/src/main/java/org/oran/otf/common/utility/gson/GsonUtils.java
new file mode 100644 (file)
index 0000000..1b224fc
--- /dev/null
@@ -0,0 +1,69 @@
+/*  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.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
+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 GsonUtils {\r
+    private static final GsonBuilder gsonBuilder =\r
+            new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")\r
+                    .registerTypeAdapter(ObjectId.class, new JsonSerializer<ObjectId>() {\r
+                        @Override\r
+                        public JsonElement serialize(ObjectId src, Type typeOfSrc,\r
+                                                     JsonSerializationContext context) {\r
+                            return new JsonPrimitive(src.toHexString());\r
+                        }\r
+                    }).registerTypeAdapter(ObjectId.class, new JsonDeserializer<ObjectId>() {\r
+                @Override\r
+                public ObjectId deserialize(JsonElement json, Type typeOfT,\r
+                                            JsonDeserializationContext context) throws JsonParseException {\r
+                    return new ObjectId(json.getAsString());\r
+                }\r
+            });\r
+\r
+    public static Gson getGson() {\r
+        return gsonBuilder.create();\r
+    }\r
+\r
+    private static final Gson gson = getGson();\r
+    private static final Type TT_mapStringString = new TypeToken<Map<String,String>>(){}.getType();\r
+\r
+    public static Map<String, String> jsonToMapStringString(String json) {\r
+        Map<String, String> ret = new HashMap<String, String>();\r
+        if (json == null || json.isEmpty())\r
+            return ret;\r
+        return gson.fromJson(json, TT_mapStringString);\r
+    }\r
+    public static String mapStringObjectToJson(Map<String, Object> map) {\r
+        if (map == null)\r
+            map = new HashMap<String, Object>();\r
+        return gson.toJson(map);\r
+    }\r
+}
\ No newline at end of file