Merge "Sonar:Critical"
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / domain / JsonWrapper.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.core.domain;\r
22 \r
23 import java.io.IOException;\r
24 import java.io.Serializable;\r
25 import java.util.List;\r
26 \r
27 import org.json.JSONException;\r
28 import org.json.JSONObject;\r
29 \r
30 import com.fasterxml.jackson.annotation.JsonInclude;\r
31 import com.fasterxml.jackson.annotation.JsonInclude.Include;\r
32 import com.fasterxml.jackson.core.JsonGenerationException;\r
33 import com.fasterxml.jackson.databind.JsonMappingException;\r
34 import com.fasterxml.jackson.databind.ObjectMapper;\r
35 import com.fasterxml.jackson.databind.ObjectWriter;\r
36 import com.fasterxml.jackson.databind.SerializationFeature;\r
37 import com.fasterxml.jackson.databind.annotation.JsonSerialize;\r
38 \r
39 import org.openecomp.mso.logger.MsoLogger;\r
40 //import org.codehaus.jackson.map.SerializationConfig.Feature;\r
41 \r
42 \r
43 /**\r
44  * Wrapper encapsulates needed JSON functionality \r
45  * to be extended by MSO service decomposition objects\r
46  * providing ways to convert to and from JSON\r
47  *\r
48  */\r
49 @JsonInclude(Include.NON_NULL)\r
50 public abstract class JsonWrapper implements Serializable  {\r
51         \r
52         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
53         @JsonInclude(Include.NON_NULL)\r
54         public String toJsonString(){\r
55                 \r
56                 String jsonString = "";\r
57                 //convert with Jackson\r
58                 ObjectMapper mapper = new ObjectMapper();\r
59                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
60                 \r
61                 mapper.setSerializationInclusion(Include.NON_NULL);\r
62                 \r
63                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();\r
64                 try {\r
65                         jsonString = ow.writeValueAsString(this);\r
66 //              } catch (JsonGenerationException e) {\r
67 //                      // TODO Auto-generated catch block\r
68 //                      e.printStackTrace();\r
69 //              } catch (JsonMappingException e) {\r
70 //                      // TODO Auto-generated catch block\r
71 //                      e.printStackTrace();\r
72 //              } catch (IOException e) {\r
73 //                      // TODO Auto-generated catch block\r
74 //                      e.printStackTrace();\r
75 //              }\r
76                 } catch (Exception e){\r
77 \r
78                         LOGGER.debug("Exception :",e);\r
79                 }\r
80                 return jsonString;\r
81         }\r
82         \r
83         @JsonInclude(Include.NON_NULL)\r
84         public JSONObject toJsonObject(){\r
85 \r
86         ObjectMapper mapper = new ObjectMapper();\r
87        // mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);\r
88         //mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);\r
89 \r
90         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
91        // mapper.enable(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);\r
92         JSONObject json = new JSONObject();\r
93          try {\r
94                         json = new JSONObject(mapper.writeValueAsString(this));\r
95                 } catch (JSONException e) {\r
96                         LOGGER.debug("Exception :",e);\r
97                 } catch (IOException e) {\r
98                         LOGGER.debug("Exception :",e);\r
99                 }\r
100          return json; \r
101         }\r
102         \r
103         public String listToJson(List list) {\r
104         ObjectMapper mapper = new ObjectMapper();\r
105         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
106         \r
107                 String jsonString = "";\r
108                 try {\r
109                         jsonString = mapper.writeValueAsString(list);\r
110                 } catch (JsonGenerationException e) {\r
111                         LOGGER.debug("Exception :",e);\r
112                 } catch (IOException e) {\r
113                         LOGGER.debug("Exception :",e);\r
114                 }\r
115                 return jsonString;\r
116         }\r
117         \r
118         @JsonInclude(Include.NON_NULL)\r
119         public String toJsonStringNoRootName(){\r
120                 \r
121                 String jsonString = "";\r
122                 //convert with Jackson\r
123                 ObjectMapper mapper = new ObjectMapper();\r
124                 mapper.setSerializationInclusion(Include.NON_NULL);\r
125                 \r
126                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();\r
127                 try {\r
128                         jsonString = ow.writeValueAsString(this);\r
129                 } catch (Exception e){\r
130 \r
131                         LOGGER.debug("Exception :",e);\r
132                 }\r
133                 return jsonString;\r
134         }\r
135         \r
136         /**\r
137          * Returns a string representation of this object.\r
138          */\r
139          @Override\r
140         public String toString() {\r
141                 return this.toJsonString();\r
142         }\r
143 }\r