aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/decomposition/JsonWrapper.java
blob: dcc6852c4e4b41c92c3d6466c67bdb18c6500fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.openecomp.mso.bpmn.core.decomposition;

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;


//import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * Wrapper encapsulates needed JSON functionality 
 * to be extended by MSO service decomposition objects
 * providing ways to convert to and from JSON
 * 
 * @author 
 *
 */
@JsonInclude(Include.NON_NULL)
public abstract class JsonWrapper {

	@JsonInclude(Include.NON_NULL)
	public String toJsonString(){
		
		

		String jsonString = "";
		//convert with Jackson
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE);
		
		mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
		
		ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
		try {
			jsonString = ow.writeValueAsString(this);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
	
	@JsonInclude(Include.NON_NULL)
	public JSONObject toJsonObject(){

        ObjectMapper mapper = new ObjectMapper();
       // mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        //mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

        mapper.enable(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE);
       // mapper.enable(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
        JSONObject json = new JSONObject();
         try {
			json = new JSONObject(mapper.writeValueAsString(this));
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         return json; 
	}
	
	public String listToJson(List list) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE);
        
		String jsonString = "";
		try {
			jsonString = mapper.writeValueAsString(list);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jsonString;
	}
	
	/**
	 * Method to construct Service Decomposition object converting
	 * JSON structure
	 * 
	 * @param jsonString - input in JSON format confirming ServiceDecomposition
	 * @return - ServiceDecomposition object
	 */
	public ServiceDecomposition JsonToServiceDecomposition(String jsonString) {
        
        ServiceDecomposition serviceDecomposition = new ServiceDecomposition();
        ObjectMapper om = new ObjectMapper();
        om.configure(Feature.UNWRAP_ROOT_VALUE, true);
       
		try {
			serviceDecomposition = om.readValue(jsonString, ServiceDecomposition.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return serviceDecomposition;
	}
	
}