AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / json / 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.json;\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 \r
38 import org.openecomp.mso.logger.MsoLogger;\r
39 \r
40 @JsonInclude(Include.NON_NULL)\r
41 public abstract class JsonWrapper implements Serializable  {\r
42         \r
43 \r
44         private static final long serialVersionUID = 8633550139273639875L;\r
45 \r
46         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
47         @JsonInclude(Include.NON_NULL)\r
48         public String toJsonString(){\r
49                 \r
50                 String jsonString = "";\r
51                 //convert with Jackson\r
52                 ObjectMapper mapper = new ObjectMapper();\r
53                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
54                 mapper.setSerializationInclusion(Include.NON_NULL);\r
55                 \r
56                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();\r
57                 try {\r
58                         jsonString = ow.writeValueAsString(this);\r
59 //              } catch (JsonGenerationException e) {\r
60 //                      // TODO Auto-generated catch block\r
61 //                      e.printStackTrace();\r
62 //              } catch (JsonMappingException e) {\r
63 //                      // TODO Auto-generated catch block\r
64 //                      e.printStackTrace();\r
65 //              } catch (IOException e) {\r
66 //                      // TODO Auto-generated catch block\r
67 //                      e.printStackTrace();\r
68 //              }\r
69                 } catch (Exception e){\r
70 \r
71                         LOGGER.debug("Exception :",e);\r
72                 }\r
73                 return jsonString;\r
74         }\r
75         \r
76         @JsonInclude(Include.NON_NULL)\r
77         public JSONObject toJsonObject() {\r
78 \r
79         ObjectMapper mapper = new ObjectMapper();\r
80        // mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
81         //mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);\r
82 \r
83         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
84        // mapper.enable(com.fasterxml.jackson.map.DeserializationFeature.UNWRAP_ROOT_VALUE);\r
85         JSONObject json = new JSONObject();\r
86          try {\r
87                         json = new JSONObject(mapper.writeValueAsString(this));\r
88                 } catch (JSONException | IOException e) {\r
89                         LOGGER.debug("Exception :",e);\r
90                 }\r
91         return json;\r
92         }\r
93         \r
94         public String listToJson(List list)  {\r
95         ObjectMapper mapper = new ObjectMapper();\r
96         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
97         \r
98                 String jsonString = "";\r
99                 try {\r
100                         jsonString = mapper.writeValueAsString(list);\r
101                 } catch (IOException e) {\r
102                         LOGGER.debug("Exception :",e);\r
103                 }\r
104                 return jsonString;\r
105         }\r
106         \r
107         @JsonInclude(Include.NON_NULL)\r
108         public String toJsonStringNoRootName(){\r
109                 \r
110                 String jsonString = "";\r
111                 //convert with Jackson\r
112                 ObjectMapper mapper = new ObjectMapper();\r
113                 mapper.setSerializationInclusion(Include.NON_NULL);\r
114                 \r
115                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();\r
116                 try {\r
117                         jsonString = ow.writeValueAsString(this);\r
118                 } catch (Exception e){\r
119 \r
120                         LOGGER.debug("Exception :",e);\r
121                 }\r
122                 return jsonString;\r
123         }\r
124         \r
125         /**\r
126          * Returns a string representation of this object.\r
127          */\r
128         public String toString() {\r
129                 return this.toJsonString();\r
130         }\r
131 }\r