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.JsonPrimitive;
39 import com.google.gson.JsonSerializationContext;
40 import com.google.gson.JsonSerializer;
42 public final class Serialization {
43 private Serialization() {
46 public static class NotificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
48 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
49 JsonSerializationContext context) {
50 return new JsonPrimitive(src.toString());
54 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
55 JsonDeserializationContext context) {
56 return ControlLoopNotificationType.toType(json.getAsString());
60 public static class TargetTypeAdapter implements JsonSerializer<ControlLoopTargetType>, JsonDeserializer<ControlLoopTargetType> {
62 public JsonElement serialize(ControlLoopTargetType src, Type typeOfSrc,
63 JsonSerializationContext context) {
64 return new JsonPrimitive(src.toString());
68 public ControlLoopTargetType deserialize(JsonElement json, Type typeOfT,
69 JsonDeserializationContext context) {
70 return ControlLoopTargetType.toType(json.getAsString());
74 public static class GSONUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
75 private static final Logger logger = LoggerFactory.getLogger(GSONUTCAdapter.class);
76 public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
79 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
81 return ZonedDateTime.parse(element.getAsString(), format);
82 } catch (Exception e) {
83 logger.error(e.getMessage(), e);
89 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
90 return new JsonPrimitive(datetime.format(format));
94 public static class GSONInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
97 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
98 return Instant.ofEpochMilli(json.getAsLong());
102 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
103 return new JsonPrimitive(src.toEpochMilli());
108 public static final Gson gson = new GsonBuilder().disableHtmlEscaping()
109 .registerTypeAdapter(ZonedDateTime.class, new GSONUTCAdapter())
110 .registerTypeAdapter(Instant.class, new GSONInstantAdapter())
111 .registerTypeAdapter(ControlLoopNotificationType.class, new NotificationTypeAdapter())
112 .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter())
116 public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
118 .registerTypeAdapter(ZonedDateTime.class, new GSONUTCAdapter())
119 .registerTypeAdapter(Instant.class, new GSONInstantAdapter())
120 .registerTypeAdapter(ControlLoopNotificationType.class, new NotificationTypeAdapter())
121 .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter())
124 public static final Gson gsonJunit = new GsonBuilder().disableHtmlEscaping()
126 .registerTypeAdapter(ZonedDateTime.class, new GSONUTCAdapter())
127 .registerTypeAdapter(Instant.class, new GSONInstantAdapter())
128 .registerTypeAdapter(ControlLoopTargetType.class, new TargetTypeAdapter())