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;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonDeserializationContext;
34 import com.google.gson.JsonDeserializer;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonParseException;
37 import com.google.gson.JsonPrimitive;
38 import com.google.gson.JsonSerializationContext;
39 import com.google.gson.JsonSerializer;
41 public final class Serialization {
43 public static class notificationTypeAdapter implements JsonSerializer<ControlLoopNotificationType>, JsonDeserializer<ControlLoopNotificationType> {
46 public JsonElement serialize(ControlLoopNotificationType src, Type typeOfSrc,
47 JsonSerializationContext context) {
48 return new JsonPrimitive(src.toString());
52 public ControlLoopNotificationType deserialize(JsonElement json, Type typeOfT,
53 JsonDeserializationContext context) throws JsonParseException {
54 return ControlLoopNotificationType.toType(json.getAsString());
59 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) throws JsonParseException {
70 return ControlLoopTargetType.toType(json.getAsString());
75 public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
76 public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
78 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
79 throws JsonParseException {
81 return ZonedDateTime.parse(element.getAsString(), format);
82 } catch (Exception e) {
83 System.err.println(e);
88 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
89 return new JsonPrimitive(datetime.format(format));
93 public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
96 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
97 throws JsonParseException {
98 return Instant.ofEpochMilli(json.getAsLong());
102 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
103 return new JsonPrimitive(src.toEpochMilli());
108 final static public 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 final static public 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())