Merge branch '1.1-rel'
[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.ArrayList;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.JsonMappingException;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33
34 /**
35  * 
36  * This class provides utility for parsing JSON object and converting data to
37  * JSON string.
38  * 
39  */
40 public class JSONUtil {
41
42     private static ObjectMapper mapper = new ObjectMapper();
43
44     public static String parseJSON(String jsonString, String key) {
45
46         if (jsonString == null || jsonString.equals(""))
47             return null;
48
49         String value = null;
50
51         try {
52             @SuppressWarnings("unchecked")
53             Map<String, String> jsonMap = mapper.readValue(jsonString,
54                     Map.class);
55             value = jsonMap.get(key);
56         } catch (IOException ioe) {
57             ioe.printStackTrace();
58         }
59
60         return value;
61     }
62     
63     public static ArrayList<String> parseJSON(byte[] payload, String key) {
64
65         if (payload == null)
66             return null;
67
68         ArrayList<String> value = null;
69
70         try {
71             @SuppressWarnings("unchecked")
72             Map<String, ArrayList<String>> jsonMap = mapper.readValue(payload,
73                     Map.class);
74             value = jsonMap.get(key);
75         } catch (IOException ioe) {
76             ioe.printStackTrace();
77         }
78
79         return value;
80     }
81
82     public static Map<String, String> parseJSON(String jsonString)
83             throws JsonParseException, JsonMappingException, IOException {
84
85         Map<String, String> map = null;
86
87         try {
88             @SuppressWarnings("unchecked")
89             Map<String, String> jsonMap = mapper.readValue(jsonString,
90                     Map.class);
91             map = jsonMap;
92         } catch (IOException ioe) {
93             ioe.printStackTrace();
94         }
95
96         return map;
97     }
98
99     public static String writeJSON(HashMap<Object, Object> data) {
100         if (data == null)
101             return null;
102
103         String json = null;
104         try {
105             json = mapper.writeValueAsString(data);
106         } catch (JsonProcessingException e) {
107             e.printStackTrace();
108         }
109
110         if (json == null)
111             json = "{}";
112
113         return json;
114     }
115 }