/* 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; } }