2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.core.domain;
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.List;
28 import org.json.JSONException;
29 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 org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
42 * Wrapper encapsulates needed JSON functionality to be extended by MSO service decomposition objects providing ways to
43 * convert to and from JSON
46 @JsonInclude(Include.NON_NULL)
47 public abstract class JsonWrapper implements Serializable {
49 private static final Logger logger = LoggerFactory.getLogger(JsonWrapper.class);
51 @JsonInclude(Include.NON_NULL)
52 public String toJsonString() {
54 String jsonString = "";
55 // convert with Jackson
56 ObjectMapper mapper = new ObjectMapper();
57 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
59 mapper.setSerializationInclusion(Include.NON_NULL);
61 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
63 jsonString = ow.writeValueAsString(this);
64 } catch (Exception e) {
66 logger.debug("Exception :", e);
71 @JsonInclude(Include.NON_NULL)
72 public JSONObject toJsonObject() {
74 ObjectMapper mapper = new ObjectMapper();
75 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
76 JSONObject json = new JSONObject();
78 json = new JSONObject(mapper.writeValueAsString(this));
79 } catch (JsonGenerationException e) {
80 logger.debug("Exception :", e);
81 } catch (JsonMappingException e) {
82 logger.debug("Exception :", e);
83 } catch (JSONException e) {
84 logger.debug("Exception :", e);
85 } catch (IOException e) {
86 logger.debug("Exception :", e);
91 public String listToJson(List list) {
92 ObjectMapper mapper = new ObjectMapper();
93 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
95 String jsonString = "";
97 jsonString = mapper.writeValueAsString(list);
98 } catch (JsonGenerationException e) {
99 logger.debug("Exception :", e);
100 } catch (JsonMappingException e) {
101 logger.debug("Exception :", e);
102 } catch (IOException e) {
103 logger.debug("Exception :", e);
108 @JsonInclude(Include.NON_NULL)
109 public String toJsonStringNoRootName() {
111 String jsonString = "";
112 // convert with Jackson
113 ObjectMapper mapper = new ObjectMapper();
114 mapper.setSerializationInclusion(Include.NON_NULL);
116 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
118 jsonString = ow.writeValueAsString(this);
119 } catch (Exception e) {
121 logger.debug("Exception :", e);
127 * Returns a string representation of this object.
129 public String toString() {
130 return this.toJsonString();