2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.bpmn.core.domain;
\r
23 import java.io.IOException;
\r
24 import java.io.Serializable;
\r
25 import java.util.List;
\r
27 import org.json.JSONException;
\r
28 import org.json.JSONObject;
\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
39 import org.openecomp.mso.logger.MsoLogger;
\r
40 //import org.codehaus.jackson.map.SerializationConfig.Feature;
\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
49 @JsonInclude(Include.NON_NULL)
\r
50 public abstract class JsonWrapper implements Serializable {
\r
52 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
\r
53 @JsonInclude(Include.NON_NULL)
\r
54 public String toJsonString(){
\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
61 mapper.setSerializationInclusion(Include.NON_NULL);
\r
63 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\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
76 } catch (Exception e){
\r
78 LOGGER.debug("Exception :",e);
\r
83 @JsonInclude(Include.NON_NULL)
\r
84 public JSONObject toJsonObject(){
\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
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
94 json = new JSONObject(mapper.writeValueAsString(this));
\r
95 } catch (JSONException | IOException e) {
\r
96 LOGGER.debug("Exception :",e);
\r
101 public String listToJson(List list) {
\r
102 ObjectMapper mapper = new ObjectMapper();
\r
103 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
105 String jsonString = "";
\r
107 jsonString = mapper.writeValueAsString(list);
\r
108 } catch (IOException e) {
\r
109 LOGGER.debug("Exception :",e);
\r
114 @JsonInclude(Include.NON_NULL)
\r
115 public String toJsonStringNoRootName(){
\r
117 String jsonString = "";
\r
118 //convert with Jackson
\r
119 ObjectMapper mapper = new ObjectMapper();
\r
120 mapper.setSerializationInclusion(Include.NON_NULL);
\r
122 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\r
124 jsonString = ow.writeValueAsString(this);
\r
125 } catch (Exception e){
\r
127 LOGGER.debug("Exception :",e);
\r
133 * Returns a string representation of this object.
\r
136 public String toString() {
\r
137 return this.toJsonString();
\r