1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
17 package org.oran.otf.common.utility.gson;
\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
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
39 public class Convert {
\r
41 private static final GsonBuilder gsonBuilder =
\r
43 .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
\r
44 .registerTypeAdapter(
\r
46 new JsonSerializer<ObjectId>() {
\r
48 public JsonElement serialize(
\r
49 ObjectId src, Type typeOfSrc, JsonSerializationContext context) {
\r
50 return new JsonPrimitive(src.toHexString());
\r
53 .registerTypeAdapter(
\r
55 new JsonDeserializer<ObjectId>() {
\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
64 public static Gson getGson() {
\r
65 return gsonBuilder.create();
\r
68 public static String mapToJson(Map map) {
\r
69 if (map.isEmpty()) {
\r
72 return getGson().toJson(map);
\r
75 public static Map<String, Object> jsonToMap(String json) {
\r
76 Type type = new TypeToken<HashMap<String, Object>>() {
\r
78 return getGson().fromJson(json, type);
\r
81 public static String objectToJson(Object obj) {
\r
82 return getGson().toJson(obj);
\r
85 public static<T> T mapToObject(Map map, TypeReference<T> typeReference) throws IOException {
\r
86 return jsonToObject(mapToJson(map), typeReference);
\r
89 public static <T> T jsonToObject(String json, TypeReference<T> typeReference) throws IOException {
\r
90 ObjectMapper objectMapper = new ObjectMapper();
\r
92 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
\r
93 return objectMapper.readValue(json, typeReference);
\r