[Simulator] Minor UI changes fixing the IOT-1087.
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / util / JSONUtil.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.util;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import com.fasterxml.jackson.core.JsonParseException;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 /**
34  * 
35  * This class provides utility for parsing JSON object and converting data to
36  * JSON string.
37  * 
38  */
39 public class JSONUtil {
40
41     private static ObjectMapper mapper = new ObjectMapper();
42
43     public static String parseJSON(String jsonString, String key) {
44
45         if (jsonString == null || jsonString.equals(""))
46             return null;
47
48         String value = null;
49
50         try {
51             @SuppressWarnings("unchecked")
52             Map<String, String> jsonMap = mapper.readValue(jsonString,
53                     Map.class);
54             value = jsonMap.get(key);
55         } catch (IOException ioe) {
56             ioe.printStackTrace();
57         }
58
59         return value;
60     }
61
62     public static Map<String, String> parseJSON(String jsonString)
63             throws JsonParseException, JsonMappingException, IOException {
64
65         Map<String, String> map = null;
66
67         try {
68             @SuppressWarnings("unchecked")
69             Map<String, String> jsonMap = mapper.readValue(jsonString,
70                     Map.class);
71             map = jsonMap;
72         } catch (IOException ioe) {
73             ioe.printStackTrace();
74         }
75
76         return map;
77     }
78
79     public static String writeJSON(HashMap<Object, Object> data) {
80         if (data == null)
81             return null;
82
83         String json = null;
84         try {
85             json = mapper.writeValueAsString(data);
86         } catch (JsonProcessingException e) {
87             e.printStackTrace();
88         }
89
90         if (json == null)
91             json = "{}";
92
93         return json;
94     }
95 }