Update resource recipe parameter prepare logic
[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(SerializationConfig.Feature.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(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);\r
85         JSONObject json = new JSONObject();\r
86          try {\r
87                         json = new JSONObject(mapper.writeValueAsString(this));\r
88                 } catch (JsonGenerationException e) {\r
89                         LOGGER.debug("Exception :",e);\r
90                 } catch (JsonMappingException e) {\r
91                         LOGGER.debug("Exception :",e);\r
92                 } catch (JSONException e) {\r
93                         LOGGER.debug("Exception :",e);\r
94                 } catch (IOException e) {\r
95                         LOGGER.debug("Exception :",e);\r
96                 }               \r
97          return json; \r
98         }\r
99         \r
100         public String listToJson(List list)  {\r
101         ObjectMapper mapper = new ObjectMapper();\r
102         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);\r
103         \r
104                 String jsonString = "";\r
105                 try {\r
106                         jsonString = mapper.writeValueAsString(list);\r
107                 } catch (JsonGenerationException e) {\r
108                         LOGGER.debug("Exception :",e);\r
109                 } catch (JsonMappingException e) {\r
110                         LOGGER.debug("Exception :",e);\r
111                 } catch (IOException e) {\r
112                         LOGGER.debug("Exception :",e);\r
113                 }\r
114                 return jsonString;\r
115         }\r
116         \r
117         @JsonInclude(Include.NON_NULL)\r
118         public String toJsonStringNoRootName(){\r
119                 \r
120                 String jsonString = "";\r
121                 //convert with Jackson\r
122                 ObjectMapper mapper = new ObjectMapper();\r
123                 mapper.setSerializationInclusion(Include.NON_NULL);\r
124                 \r
125                 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();\r
126                 try {\r
127                         jsonString = ow.writeValueAsString(this);\r
128                 } catch (Exception e){\r
129 \r
130                         LOGGER.debug("Exception :",e);\r
131                 }\r
132                 return jsonString;\r
133         }\r
134         \r
135         /**\r
136          * Returns a string representation of this object.\r
137          */\r
138         public String toString() {\r
139                 return this.toJsonString();\r
140         }\r
141 }