X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fcommon%2Futility%2Fgson%2FConvert.java;fp=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fcommon%2Futility%2Fgson%2FConvert.java;h=fb1e55fb4eb43ea7edd11e01b9809df5dec24c7f;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-camunda/src/main/java/org/oran/otf/common/utility/gson/Convert.java b/otf-camunda/src/main/java/org/oran/otf/common/utility/gson/Convert.java new file mode 100644 index 0000000..fb1e55f --- /dev/null +++ b/otf-camunda/src/main/java/org/oran/otf/common/utility/gson/Convert.java @@ -0,0 +1,121 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +package org.oran.otf.common.utility.gson; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.reflect.TypeToken; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.bson.types.ObjectId; + +public class Convert { + + private static final GsonBuilder gsonBuilder = + new GsonBuilder() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .registerTypeAdapter( + ObjectId.class, + new JsonSerializer() { + @Override + public JsonElement serialize( + ObjectId src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toHexString()); + } + }) + .registerTypeAdapter( + ObjectId.class, + new JsonDeserializer() { + @Override + public ObjectId deserialize( + JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return new ObjectId(json.getAsString()); + } + }); + + public static Gson getGson() { + return gsonBuilder.create(); + } + + public static String mapToJson(Map map) { + if (map.isEmpty()) { + return "{}"; + } + return getGson().toJson(map); + } + + public static String listToJson(List list) { + if (list.isEmpty()) { + return "[]"; + } + return getGson().toJson(list); + } + + public static Map jsonToMap(String json) { + Type type = new TypeToken>() { + }.getType(); + return getGson().fromJson(json, type); + } + + public static String objectToJson(Object obj) { + return getGson().toJson(obj); + } + + public static T jsonToObject(String json, TypeReference typeReference) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); + return objectMapper.readValue(json, typeReference); + } + + public static T mapToObject(Map map, TypeReference typeReference) throws IOException { + return jsonToObject(mapToJson(map), typeReference); + } + + public static T listToObjectList(List list, TypeReference typeReference) throws IOException { + return jsonToObject(listToJson(list), typeReference); + } + + public static List convertObjectToList(Object obj) { + List list = new ArrayList<>(); + if (obj.getClass().isArray()) { + list = Arrays.asList((Object[])obj); + } else if (obj instanceof Collection) { + list = new ArrayList<>((Collection)obj); + } + return list; + } + +}