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