2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.util;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
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;
36 * This class provides utility for parsing JSON object and converting data to
40 public class JSONUtil {
42 private static ObjectMapper mapper = new ObjectMapper();
44 public static String parseJSON(String jsonString, String key) {
46 if (jsonString == null || jsonString.equals(""))
52 @SuppressWarnings("unchecked")
53 Map<String, String> jsonMap = mapper.readValue(jsonString,
55 value = jsonMap.get(key);
56 } catch (IOException ioe) {
57 ioe.printStackTrace();
63 public static ArrayList<String> parseJSON(byte[] payload, String key) {
68 ArrayList<String> value = null;
71 @SuppressWarnings("unchecked")
72 Map<String, ArrayList<String>> jsonMap = mapper.readValue(payload,
74 value = jsonMap.get(key);
75 } catch (IOException ioe) {
76 ioe.printStackTrace();
82 public static Map<String, String> parseJSON(String jsonString)
83 throws JsonParseException, JsonMappingException, IOException {
85 Map<String, String> map = null;
88 @SuppressWarnings("unchecked")
89 Map<String, String> jsonMap = mapper.readValue(jsonString,
92 } catch (IOException ioe) {
93 ioe.printStackTrace();
99 public static String writeJSON(HashMap<Object, Object> data) {
105 json = mapper.writeValueAsString(data);
106 } catch (JsonProcessingException e) {