X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-service-api%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fcommon%2Futility%2Fgson%2FGsonUtils.java;fp=otf-service-api%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fcommon%2Futility%2Fgson%2FGsonUtils.java;h=1b224fc812fb1d689b9a471de8a5e43f0237a03c;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b 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 index 0000000..1b224fc --- /dev/null +++ b/otf-service-api/src/main/java/org/oran/otf/common/utility/gson/GsonUtils.java @@ -0,0 +1,69 @@ +/* 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.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.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; +import org.bson.types.ObjectId; + +public class GsonUtils { + 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(); + } + + private static final Gson gson = getGson(); + private static final Type TT_mapStringString = new TypeToken>(){}.getType(); + + public static Map jsonToMapStringString(String json) { + Map ret = new HashMap(); + if (json == null || json.isEmpty()) + return ret; + return gson.fromJson(json, TT_mapStringString); + } + public static String mapStringObjectToJson(Map map) { + if (map == null) + map = new HashMap(); + return gson.toJson(map); + } +} \ No newline at end of file