2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.core.domain;
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.List;
27 import org.json.JSONException;
28 import org.json.JSONObject;
30 import com.fasterxml.jackson.annotation.JsonInclude;
31 import com.fasterxml.jackson.annotation.JsonInclude.Include;
32 import com.fasterxml.jackson.core.JsonGenerationException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.ObjectWriter;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
39 import org.onap.so.logger.MsoLogger;
40 //import com.fasterxml.jackson.map.SerializationFeature;
44 * Wrapper encapsulates needed JSON functionality
45 * to be extended by MSO service decomposition objects
46 * providing ways to convert to and from JSON
49 @JsonInclude(Include.NON_NULL)
50 public abstract class JsonWrapper implements Serializable {
52 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, JsonWrapper.class);
53 @JsonInclude(Include.NON_NULL)
54 public String toJsonString(){
56 String jsonString = "";
57 //convert with Jackson
58 ObjectMapper mapper = new ObjectMapper();
59 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
61 mapper.setSerializationInclusion(Include.NON_NULL);
63 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
65 jsonString = ow.writeValueAsString(this);
66 } catch (Exception e){
68 LOGGER.debug("Exception :",e);
73 @JsonInclude(Include.NON_NULL)
74 public JSONObject toJsonObject(){
76 ObjectMapper mapper = new ObjectMapper();
77 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
78 JSONObject json = new JSONObject();
80 json = new JSONObject(mapper.writeValueAsString(this));
81 } catch (JsonGenerationException e) {
82 LOGGER.debug("Exception :",e);
83 } catch (JsonMappingException e) {
84 LOGGER.debug("Exception :",e);
85 } catch (JSONException e) {
86 LOGGER.debug("Exception :",e);
87 } catch (IOException e) {
88 LOGGER.debug("Exception :",e);
93 public String listToJson(List list) {
94 ObjectMapper mapper = new ObjectMapper();
95 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
97 String jsonString = "";
99 jsonString = mapper.writeValueAsString(list);
100 } catch (JsonGenerationException e) {
101 LOGGER.debug("Exception :",e);
102 } catch (JsonMappingException e) {
103 LOGGER.debug("Exception :",e);
104 } catch (IOException e) {
105 LOGGER.debug("Exception :",e);
110 @JsonInclude(Include.NON_NULL)
111 public String toJsonStringNoRootName(){
113 String jsonString = "";
114 //convert with Jackson
115 ObjectMapper mapper = new ObjectMapper();
116 mapper.setSerializationInclusion(Include.NON_NULL);
118 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
120 jsonString = ow.writeValueAsString(this);
121 } catch (Exception e){
123 LOGGER.debug("Exception :",e);
129 * Returns a string representation of this object.
131 public String toString() {
132 return this.toJsonString();