Replaced all tabs with spaces in java and pom.xml
[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 import org.json.JSONException;
29 import org.json.JSONObject;
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 org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 /**
42  * Wrapper encapsulates needed JSON functionality to be extended by MSO service decomposition objects providing ways to
43  * convert to and from JSON
44  *
45  */
46 @JsonInclude(Include.NON_NULL)
47 public abstract class JsonWrapper implements Serializable {
48
49     private static final Logger logger = LoggerFactory.getLogger(JsonWrapper.class);
50
51     @JsonInclude(Include.NON_NULL)
52     public String toJsonString() {
53
54         String jsonString = "";
55         // convert with Jackson
56         ObjectMapper mapper = new ObjectMapper();
57         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
58
59         mapper.setSerializationInclusion(Include.NON_NULL);
60
61         ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
62         try {
63             jsonString = ow.writeValueAsString(this);
64         } catch (Exception e) {
65
66             logger.debug("Exception :", e);
67         }
68         return jsonString;
69     }
70
71     @JsonInclude(Include.NON_NULL)
72     public JSONObject toJsonObject() {
73
74         ObjectMapper mapper = new ObjectMapper();
75         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
76         JSONObject json = new JSONObject();
77         try {
78             json = new JSONObject(mapper.writeValueAsString(this));
79         } catch (JsonGenerationException e) {
80             logger.debug("Exception :", e);
81         } catch (JsonMappingException e) {
82             logger.debug("Exception :", e);
83         } catch (JSONException e) {
84             logger.debug("Exception :", e);
85         } catch (IOException e) {
86             logger.debug("Exception :", e);
87         }
88         return json;
89     }
90
91     public String listToJson(List list) {
92         ObjectMapper mapper = new ObjectMapper();
93         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
94
95         String jsonString = "";
96         try {
97             jsonString = mapper.writeValueAsString(list);
98         } catch (JsonGenerationException e) {
99             logger.debug("Exception :", e);
100         } catch (JsonMappingException e) {
101             logger.debug("Exception :", e);
102         } catch (IOException e) {
103             logger.debug("Exception :", e);
104         }
105         return jsonString;
106     }
107
108     @JsonInclude(Include.NON_NULL)
109     public String toJsonStringNoRootName() {
110
111         String jsonString = "";
112         // convert with Jackson
113         ObjectMapper mapper = new ObjectMapper();
114         mapper.setSerializationInclusion(Include.NON_NULL);
115
116         ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
117         try {
118             jsonString = ow.writeValueAsString(this);
119         } catch (Exception e) {
120
121             logger.debug("Exception :", e);
122         }
123         return jsonString;
124     }
125
126     /**
127      * Returns a string representation of this object.
128      */
129     public String toString() {
130         return this.toJsonString();
131     }
132 }