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.json;
\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
38 import org.openecomp.mso.logger.MsoLogger;
\r
40 @JsonInclude(Include.NON_NULL)
\r
41 public abstract class JsonWrapper implements Serializable {
\r
44 private static final long serialVersionUID = 8633550139273639875L;
\r
46 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
\r
47 @JsonInclude(Include.NON_NULL)
\r
48 public String toJsonString(){
\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
56 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\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
69 } catch (Exception e){
\r
71 LOGGER.debug("Exception :",e);
\r
76 @JsonInclude(Include.NON_NULL)
\r
77 public JSONObject toJsonObject() {
\r
79 ObjectMapper mapper = new ObjectMapper();
\r
80 // mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
81 //mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
\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
87 json = new JSONObject(mapper.writeValueAsString(this));
\r
88 } catch (JSONException | IOException e) {
\r
89 LOGGER.debug("Exception :",e);
\r
94 public String listToJson(List list) {
\r
95 ObjectMapper mapper = new ObjectMapper();
\r
96 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
\r
98 String jsonString = "";
\r
100 jsonString = mapper.writeValueAsString(list);
\r
101 } catch (IOException e) {
\r
102 LOGGER.debug("Exception :",e);
\r
107 @JsonInclude(Include.NON_NULL)
\r
108 public String toJsonStringNoRootName(){
\r
110 String jsonString = "";
\r
111 //convert with Jackson
\r
112 ObjectMapper mapper = new ObjectMapper();
\r
113 mapper.setSerializationInclusion(Include.NON_NULL);
\r
115 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
\r
117 jsonString = ow.writeValueAsString(this);
\r
118 } catch (Exception e){
\r
120 LOGGER.debug("Exception :",e);
\r
126 * Returns a string representation of this object.
\r
128 public String toString() {
\r
129 return this.toJsonString();
\r