added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / common / utility / gson / Convert.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.common.utility.gson;\r
18 \r
19 import com.fasterxml.jackson.core.type.TypeReference;\r
20 import com.fasterxml.jackson.databind.DeserializationFeature;\r
21 import com.fasterxml.jackson.databind.ObjectMapper;\r
22 import com.google.gson.Gson;\r
23 import com.google.gson.GsonBuilder;\r
24 import com.google.gson.JsonDeserializationContext;\r
25 import com.google.gson.JsonDeserializer;\r
26 import com.google.gson.JsonElement;\r
27 import com.google.gson.JsonParseException;\r
28 import com.google.gson.JsonPrimitive;\r
29 import com.google.gson.JsonSerializationContext;\r
30 import com.google.gson.JsonSerializer;\r
31 import com.google.gson.reflect.TypeToken;\r
32 \r
33 import java.io.IOException;\r
34 import java.lang.reflect.Type;\r
35 import java.util.HashMap;\r
36 import java.util.Map;\r
37 import org.bson.types.ObjectId;\r
38 \r
39 public class Convert {\r
40 \r
41   private static final GsonBuilder gsonBuilder =\r
42       new GsonBuilder()\r
43           .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")\r
44           .registerTypeAdapter(\r
45               ObjectId.class,\r
46               new JsonSerializer<ObjectId>() {\r
47                 @Override\r
48                 public JsonElement serialize(\r
49                     ObjectId src, Type typeOfSrc, JsonSerializationContext context) {\r
50                   return new JsonPrimitive(src.toHexString());\r
51                 }\r
52               })\r
53           .registerTypeAdapter(\r
54               ObjectId.class,\r
55               new JsonDeserializer<ObjectId>() {\r
56                 @Override\r
57                 public ObjectId deserialize(\r
58                     JsonElement json, Type typeOfT, JsonDeserializationContext context)\r
59                     throws JsonParseException {\r
60                   return new ObjectId(json.getAsString());\r
61                 }\r
62               });\r
63 \r
64   public static Gson getGson() {\r
65     return gsonBuilder.create();\r
66   }\r
67 \r
68   public static String mapToJson(Map map) {\r
69     if (map.isEmpty()) {\r
70       return "{}";\r
71     }\r
72     return getGson().toJson(map);\r
73   }\r
74 \r
75   public static Map<String, Object> jsonToMap(String json) {\r
76     Type type = new TypeToken<HashMap<String, Object>>() {\r
77     }.getType();\r
78     return getGson().fromJson(json, type);\r
79   }\r
80 \r
81   public static String objectToJson(Object obj) {\r
82     return getGson().toJson(obj);\r
83   }\r
84 \r
85   public static<T> T mapToObject(Map map, TypeReference<T> typeReference) throws IOException {\r
86     return jsonToObject(mapToJson(map), typeReference);\r
87   }\r
88 \r
89   public static <T> T jsonToObject(String json, TypeReference<T> typeReference) throws IOException {\r
90     ObjectMapper objectMapper = new ObjectMapper();\r
91     objectMapper\r
92         .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);\r
93     return objectMapper.readValue(json, typeReference);\r
94   }\r
95 }\r