c3eefcd3cd5fc063e804910b80d4dcda85889af5
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.core.domain;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.List;
28
29 import org.json.JSONException;
30 import org.json.JSONObject;
31
32 import com.fasterxml.jackson.annotation.JsonInclude;
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.core.JsonGenerationException;
35 import com.fasterxml.jackson.databind.JsonMappingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.ObjectWriter;
38 import com.fasterxml.jackson.databind.SerializationFeature;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 /**
45  * Wrapper encapsulates needed JSON functionality 
46  * to be extended by MSO service decomposition objects
47  * providing ways to convert to and from JSON
48  *
49  */
50 @JsonInclude(Include.NON_NULL)
51 public abstract class JsonWrapper implements Serializable  {
52
53         private static final Logger logger = LoggerFactory.getLogger(JsonWrapper.class);
54         @JsonInclude(Include.NON_NULL)
55         public String toJsonString(){
56
57                 String jsonString = "";
58                 //convert with Jackson
59                 ObjectMapper mapper = new ObjectMapper();
60                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
61
62                 mapper.setSerializationInclusion(Include.NON_NULL);
63
64                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
65                 try {
66                         jsonString = ow.writeValueAsString(this);
67                 } catch (Exception e){
68
69                         logger.debug("Exception :",e);
70                 }
71                 return jsonString;
72         }
73
74         @JsonInclude(Include.NON_NULL)
75         public JSONObject toJsonObject(){
76
77                 ObjectMapper mapper = new ObjectMapper();   
78                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);    
79                 JSONObject json = new JSONObject();
80                 try {
81                         json = new JSONObject(mapper.writeValueAsString(this));
82                 } catch (JsonGenerationException e) {
83                         logger.debug("Exception :",e);
84                 } catch (JsonMappingException e) {
85                         logger.debug("Exception :",e);
86                 } catch (JSONException e) {
87                         logger.debug("Exception :",e);
88                 } catch (IOException e) {
89                         logger.debug("Exception :",e);
90                 }
91                 return json; 
92         }
93
94         public String listToJson(List list) {
95                 ObjectMapper mapper = new ObjectMapper();
96                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
97
98                 String jsonString = "";
99                 try {
100                         jsonString = mapper.writeValueAsString(list);
101                 } catch (JsonGenerationException e) {
102                         logger.debug("Exception :",e);
103                 } catch (JsonMappingException e) {
104                         logger.debug("Exception :",e);
105                 } catch (IOException e) {
106                         logger.debug("Exception :",e);
107                 }
108                 return jsonString;
109         }
110
111         @JsonInclude(Include.NON_NULL)
112         public String toJsonStringNoRootName(){
113
114                 String jsonString = "";
115                 //convert with Jackson
116                 ObjectMapper mapper = new ObjectMapper();
117                 mapper.setSerializationInclusion(Include.NON_NULL);
118
119                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
120                 try {
121                         jsonString = ow.writeValueAsString(this);
122                 } catch (Exception e){
123
124                         logger.debug("Exception :",e);
125                 }
126                 return jsonString;
127         }
128
129         /**
130          * Returns a string representation of this object.
131          */
132         public String toString() {
133                 return this.toJsonString();
134         }
135 }