[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / model-impl / appc / src / main / java / org / onap / policy / appc / util / Serialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appc
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.appc.util;
22
23 import java.lang.reflect.Type;
24 import java.time.Instant;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27
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;
37
38 public final class Serialization {
39         
40         public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
41
42         public static class gsonUTCAdapter implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
43
44                 public ZonedDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context)
45                                 throws JsonParseException {
46                         try {
47                                 return ZonedDateTime.parse(element.getAsString(), format);
48                         } catch (Exception e) {
49                                 System.err.println(e);
50                         }
51                         return null;
52                 }
53
54                 public JsonElement serialize(ZonedDateTime datetime, Type type, JsonSerializationContext context) {
55                         return new JsonPrimitive(datetime.format(format));
56                 }       
57         }
58         public static class gsonInstantAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
59
60                 @Override
61                 public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
62                                 throws JsonParseException {
63                         return Instant.ofEpochMilli(json.getAsLong());
64                 }
65
66                 @Override
67                 public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
68                         return new JsonPrimitive(src.toEpochMilli());
69                 }
70                 
71         }
72
73         public static final Gson gsonPretty = new GsonBuilder()
74                         .disableHtmlEscaping()
75                         .setPrettyPrinting()
76                         .registerTypeAdapter(ZonedDateTime.class, new gsonUTCAdapter())
77                         .registerTypeAdapter(Instant.class, new gsonInstantAdapter())
78 //                      .registerTypeAdapter(CommonHeader1607.class, new gsonCommonHeaderInstance())
79 //                      .registerTypeAdapter(ResponseStatus1607.class, new gsonResponseStatus())
80                         .create();
81         
82 }