1 package org.openecomp.mso.bpmn.core.domain;
\r
3 import java.io.IOException;
\r
4 import java.io.Serializable;
\r
5 import java.util.List;
\r
7 import org.json.JSONException;
\r
8 import org.json.JSONObject;
\r
10 import com.fasterxml.jackson.annotation.JsonInclude;
\r
11 import com.fasterxml.jackson.annotation.JsonInclude.Include;
\r
12 import com.fasterxml.jackson.core.JsonGenerationException;
\r
13 import com.fasterxml.jackson.databind.JsonMappingException;
\r
14 import com.fasterxml.jackson.databind.ObjectMapper;
\r
15 import com.fasterxml.jackson.databind.ObjectWriter;
\r
16 import com.fasterxml.jackson.databind.SerializationFeature;
\r
17 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
\r
19 import org.openecomp.mso.logger.MsoLogger;
\r
20 //import org.codehaus.jackson.map.SerializationConfig.Feature;
\r
24 * Wrapper encapsulates needed JSON functionality
\r
25 * to be extended by MSO service decomposition objects
\r
26 * providing ways to convert to and from JSON
\r
29 @JsonInclude(Include.NON_NULL)
\r
30 public abstract class JsonWrapper implements Serializable {
\r
32 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
\r
33 @JsonInclude(Include.NON_NULL)
\r
34 public String toJsonString(){
\r
36 String jsonString = "";
\r
37 //convert with Jackson
\r
38 ObjectMapper mapper = new ObjectMapper();
\r
39 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
41 mapper.setSerializationInclusion(Include.NON_NULL);
\r
43 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\r
45 jsonString = ow.writeValueAsString(this);
\r
46 // } catch (JsonGenerationException e) {
\r
47 // // TODO Auto-generated catch block
\r
48 // e.printStackTrace();
\r
49 // } catch (JsonMappingException e) {
\r
50 // // TODO Auto-generated catch block
\r
51 // e.printStackTrace();
\r
52 // } catch (IOException e) {
\r
53 // // TODO Auto-generated catch block
\r
54 // e.printStackTrace();
\r
56 } catch (Exception e){
\r
58 LOGGER.debug("Exception :",e);
\r
63 @JsonInclude(Include.NON_NULL)
\r
64 public JSONObject toJsonObject(){
\r
66 ObjectMapper mapper = new ObjectMapper();
\r
67 // mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
\r
68 //mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
\r
70 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
71 // mapper.enable(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
\r
72 JSONObject json = new JSONObject();
\r
74 json = new JSONObject(mapper.writeValueAsString(this));
\r
75 } catch (JsonGenerationException e) {
\r
76 LOGGER.debug("Exception :",e);
\r
77 } catch (JsonMappingException e) {
\r
78 LOGGER.debug("Exception :",e);
\r
79 } catch (JSONException e) {
\r
80 LOGGER.debug("Exception :",e);
\r
81 } catch (IOException e) {
\r
82 LOGGER.debug("Exception :",e);
\r
87 public String listToJson(List list) {
\r
88 ObjectMapper mapper = new ObjectMapper();
\r
89 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
91 String jsonString = "";
\r
93 jsonString = mapper.writeValueAsString(list);
\r
94 } catch (JsonGenerationException e) {
\r
95 LOGGER.debug("Exception :",e);
\r
96 } catch (JsonMappingException e) {
\r
97 LOGGER.debug("Exception :",e);
\r
98 } catch (IOException e) {
\r
99 LOGGER.debug("Exception :",e);
\r
104 @JsonInclude(Include.NON_NULL)
\r
105 public String toJsonStringNoRootName(){
\r
107 String jsonString = "";
\r
108 //convert with Jackson
\r
109 ObjectMapper mapper = new ObjectMapper();
\r
110 mapper.setSerializationInclusion(Include.NON_NULL);
\r
112 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\r
114 jsonString = ow.writeValueAsString(this);
\r
115 } catch (Exception e){
\r
117 LOGGER.debug("Exception :",e);
\r
123 * Returns a string representation of this object.
\r
125 public String toString() {
\r
126 return this.toJsonString();
\r