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;
29 import org.json.JSONException;
30 import org.json.JSONObject;
32 import com.fasterxml.jackson.annotation.JsonInclude;
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.core.JsonGenerationException;
35 import com.fasterxml.jackson.databind.JsonMappingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.ObjectWriter;
38 import com.fasterxml.jackson.databind.SerializationFeature;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 * Wrapper encapsulates needed JSON functionality
46 * to be extended by MSO service decomposition objects
47 * providing ways to convert to and from JSON
50 @JsonInclude(Include.NON_NULL)
51 public abstract class JsonWrapper implements Serializable {
53 private static final Logger logger = LoggerFactory.getLogger(JsonWrapper.class);
54 @JsonInclude(Include.NON_NULL)
55 public String toJsonString(){
57 String jsonString = "";
58 //convert with Jackson
59 ObjectMapper mapper = new ObjectMapper();
60 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
62 mapper.setSerializationInclusion(Include.NON_NULL);
64 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
66 jsonString = ow.writeValueAsString(this);
67 } catch (Exception e){
69 logger.debug("Exception :",e);
74 @JsonInclude(Include.NON_NULL)
75 public JSONObject toJsonObject(){
77 ObjectMapper mapper = new ObjectMapper();
78 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
79 JSONObject json = new JSONObject();
81 json = new JSONObject(mapper.writeValueAsString(this));
82 } catch (JsonGenerationException e) {
83 logger.debug("Exception :",e);
84 } catch (JsonMappingException e) {
85 logger.debug("Exception :",e);
86 } catch (JSONException e) {
87 logger.debug("Exception :",e);
88 } catch (IOException e) {
89 logger.debug("Exception :",e);
94 public String listToJson(List list) {
95 ObjectMapper mapper = new ObjectMapper();
96 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
98 String jsonString = "";
100 jsonString = mapper.writeValueAsString(list);
101 } catch (JsonGenerationException e) {
102 logger.debug("Exception :",e);
103 } catch (JsonMappingException e) {
104 logger.debug("Exception :",e);
105 } catch (IOException e) {
106 logger.debug("Exception :",e);
111 @JsonInclude(Include.NON_NULL)
112 public String toJsonStringNoRootName(){
114 String jsonString = "";
115 //convert with Jackson
116 ObjectMapper mapper = new ObjectMapper();
117 mapper.setSerializationInclusion(Include.NON_NULL);
119 ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
121 jsonString = ow.writeValueAsString(this);
122 } catch (Exception e){
124 logger.debug("Exception :",e);
130 * Returns a string representation of this object.
132 public String toString() {
133 return this.toJsonString();