2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.appclcm.util;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonPrimitive;
30 import com.google.gson.JsonSerializationContext;
31 import com.google.gson.JsonSerializer;
33 import java.lang.reflect.Type;
34 import java.time.Instant;
36 import org.onap.policy.appclcm.LcmRequest;
37 import org.onap.policy.appclcm.LcmResponse;
39 public final class Serialization {
40 public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
41 .registerTypeAdapter(Instant.class, new InstantAdapter()).create();
43 public static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
44 .registerTypeAdapter(LcmRequest.class, new RequestAdapter())
45 .registerTypeAdapter(LcmResponse.class, new ResponseAdapter()).create();
47 public static final Gson gsonJunit = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
48 .registerTypeAdapter(Instant.class, new InstantJunitAdapter()).create();
50 private Serialization() {}
52 public static class RequestAdapter implements JsonSerializer<LcmRequest>, JsonDeserializer<LcmRequest> {
55 public JsonElement serialize(LcmRequest src, Type typeOfSrc, JsonSerializationContext context) {
56 JsonElement requestJson = gsonPretty.toJsonTree(src, LcmRequest.class);
57 JsonObject input = new JsonObject();
58 input.add("input", requestJson);
64 public LcmRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
65 return gsonPretty.fromJson(json.getAsJsonObject().get("input"), LcmRequest.class);
69 public static class ResponseAdapter implements JsonSerializer<LcmResponse>, JsonDeserializer<LcmResponse> {
72 public JsonElement serialize(LcmResponse src, Type typeOfSrc, JsonSerializationContext context) {
73 JsonElement responseJson = gsonPretty.toJsonTree(src, LcmResponse.class);
74 JsonObject output = new JsonObject();
75 output.add("output", responseJson);
80 public LcmResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
81 return gsonPretty.fromJson(json.getAsJsonObject().get("output"), LcmResponse.class);
85 public static class InstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
88 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
89 return Instant.parse(json.getAsString());
93 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
94 return new JsonPrimitive(src.toString());
99 public static class InstantJunitAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
102 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
103 return Instant.ofEpochMilli(json.getAsLong());
107 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
108 return new JsonPrimitive(src.toEpochMilli());