2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.util;
23 import java.lang.reflect.Type;
24 import java.time.Instant;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
28 import org.onap.policy.controlloop.ControlLoopNotificationType;
29 import org.onap.policy.controlloop.ControlLoopTargetType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonDeserializationContext;
36 import com.google.gson.JsonDeserializer;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParseException;
39 import com.google.gson.JsonPrimitive;
40 import com.google.gson.JsonSerializationContext;
41 import com.google.gson.JsonSerializer;
44 public final class Serialization {
46 public static class notificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
50 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
51 JsonSerializationContext context) {
52 return new JsonPrimitive(src.toString());
56 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
57 JsonDeserializationContext context) throws JsonParseException {
58 return ControlLoopNotificationType.toType(json.getAsString());
63 public static class targetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
66 public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc,
67 JsonSerializationContext context) {
68 return new JsonPrimitive(src.toString());
72 public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT,
73 JsonDeserializationContext context) throws JsonParseException {
74 return ControlLoopTargetType.toType(json.getAsString());
79 public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
80 private static final Logger logger = LoggerFactory.getLogger(gsonUTCAdapter.class);
81 public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
83 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
84 throws JsonParseException {
86 return ZonedDateTime.parse(element.getAsString(), format);
87 } catch (Exception e) {
88 logger.error(e.getMessage(), e);
93 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
94 return new JsonPrimitive(datetime.format(format));
98 public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
101 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
102 throws JsonParseException {
103 return Instant.ofEpochMilli(json.getAsLong());
107 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
108 return new JsonPrimitive(src.toEpochMilli());
113 final static public Gson gson = new GsonBuilder().disableHtmlEscaping()
114 .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
115 .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
116 .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
117 .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())
121 final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
123 .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
124 .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
125 .registerTypeAdapter(ControlLoopNotificationType.class, new notificationTypeAdapter())
126 .registerTypeAdapter(ControlLoopTargetType.class, new targetTypeAdapter())