Containerization feature of SO
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / onap / so / bpmn / core / domain / JsonWrapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.so.bpmn.core.domain;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.List;
26
27 import org.json.JSONException;
28 import org.json.JSONObject;
29
30 import com.fasterxml.jackson.annotation.JsonInclude;
31 import com.fasterxml.jackson.annotation.JsonInclude.Include;
32 import com.fasterxml.jackson.core.JsonGenerationException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.ObjectWriter;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
38
39 import org.onap.so.logger.MsoLogger;
40 //import com.fasterxml.jackson.map.SerializationFeature;
41
42
43 /**
44  * Wrapper encapsulates needed JSON functionality 
45  * to be extended by MSO service decomposition objects
46  * providing ways to convert to and from JSON
47  *
48  */
49 @JsonInclude(Include.NON_NULL)
50 public abstract class JsonWrapper implements Serializable  {
51
52         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, JsonWrapper.class);
53         @JsonInclude(Include.NON_NULL)
54         public String toJsonString(){
55
56                 String jsonString = "";
57                 //convert with Jackson
58                 ObjectMapper mapper = new ObjectMapper();
59                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
60
61                 mapper.setSerializationInclusion(Include.NON_NULL);
62
63                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
64                 try {
65                         jsonString = ow.writeValueAsString(this);
66                 } catch (Exception e){
67
68                         LOGGER.debug("Exception :",e);
69                 }
70                 return jsonString;
71         }
72
73         @JsonInclude(Include.NON_NULL)
74         public JSONObject toJsonObject(){
75
76                 ObjectMapper mapper = new ObjectMapper();   
77                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);    
78                 JSONObject json = new JSONObject();
79                 try {
80                         json = new JSONObject(mapper.writeValueAsString(this));
81                 } catch (JsonGenerationException e) {
82                         LOGGER.debug("Exception :",e);
83                 } catch (JsonMappingException e) {
84                         LOGGER.debug("Exception :",e);
85                 } catch (JSONException e) {
86                         LOGGER.debug("Exception :",e);
87                 } catch (IOException e) {
88                         LOGGER.debug("Exception :",e);
89                 }
90                 return json; 
91         }
92
93         public String listToJson(List list) {
94                 ObjectMapper mapper = new ObjectMapper();
95                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
96
97                 String jsonString = "";
98                 try {
99                         jsonString = mapper.writeValueAsString(list);
100                 } catch (JsonGenerationException e) {
101                         LOGGER.debug("Exception :",e);
102                 } catch (JsonMappingException e) {
103                         LOGGER.debug("Exception :",e);
104                 } catch (IOException e) {
105                         LOGGER.debug("Exception :",e);
106                 }
107                 return jsonString;
108         }
109
110         @JsonInclude(Include.NON_NULL)
111         public String toJsonStringNoRootName(){
112
113                 String jsonString = "";
114                 //convert with Jackson
115                 ObjectMapper mapper = new ObjectMapper();
116                 mapper.setSerializationInclusion(Include.NON_NULL);
117
118                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
119                 try {
120                         jsonString = ow.writeValueAsString(this);
121                 } catch (Exception e){
122
123                         LOGGER.debug("Exception :",e);
124                 }
125                 return jsonString;
126         }
127
128         /**
129          * Returns a string representation of this object.
130          */
131         public String toString() {
132                 return this.toJsonString();
133         }
134 }