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.openecomp.policy.appc.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 com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonDeserializationContext;
31 import com.google.gson.JsonDeserializer;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonParseException;
34 import com.google.gson.JsonPrimitive;
35 import com.google.gson.JsonSerializationContext;
36 import com.google.gson.JsonSerializer;
38 public final class Serialization {
40 public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
42 public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
44 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
45 throws JsonParseException {
47 return ZonedDateTime.parse(element.getAsString(), format);
48 } catch (Exception e) {
49 System.err.println(e);
54 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
55 return new JsonPrimitive(datetime.format(format));
58 public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
61 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
62 throws JsonParseException {
63 return Instant.ofEpochMilli(json.getAsLong());
67 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
68 return new JsonPrimitive(src.toEpochMilli());
73 public static final Gson gsonPretty = new GsonBuilder()
74 .disableHtmlEscaping()
76 .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
77 .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
78 // .registerTypeAdapter(CommonHeader1607.class, new gsonCommonHeaderInstance())
79 // .registerTypeAdapter(ResponseStatus1607.class, new gsonResponseStatus())