|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 package org.chromium.sdk.internal; |
|
6 |
|
7 import java.io.IOException; |
|
8 import java.io.StringWriter; |
|
9 import java.util.logging.Level; |
|
10 import java.util.logging.Logger; |
|
11 |
|
12 import org.json.simple.JSONArray; |
|
13 import org.json.simple.JSONObject; |
|
14 import org.json.simple.JSONStreamAware; |
|
15 import org.json.simple.parser.JSONParser; |
|
16 import org.json.simple.parser.ParseException; |
|
17 |
|
18 /** |
|
19 * A utility for JSON-related data conversion. |
|
20 */ |
|
21 public class JsonUtil { |
|
22 |
|
23 private static final Logger LOGGER = Logger.getLogger(JsonUtil.class.getName()); |
|
24 |
|
25 /** |
|
26 * Converts a JSONStreamAware into a String. |
|
27 * |
|
28 * @param object the object to convert |
|
29 * @return a JSON String representation of the object |
|
30 */ |
|
31 public static String streamAwareToJson(JSONStreamAware object) { |
|
32 StringWriter out = new StringWriter(); |
|
33 try { |
|
34 object.writeJSONString(out); |
|
35 } catch (IOException e) { |
|
36 return null; |
|
37 } |
|
38 return out.toString(); |
|
39 } |
|
40 |
|
41 /** |
|
42 * @param json a JSON representation of an object (rather than an array or any |
|
43 * other type) |
|
44 * @return a JSONObject represented by json, or null if json does not |
|
45 * represent a valid JSONObject |
|
46 * @throws ParseException |
|
47 */ |
|
48 public static JSONObject jsonObjectFromJson(String json) throws ParseException { |
|
49 JSONParser p = new JSONParser(); |
|
50 Object parsed = p.parse(json); |
|
51 if (false == parsed instanceof JSONObject) { |
|
52 LOGGER.log(Level.SEVERE, "Not a JSON object: {0}", json); |
|
53 return null; |
|
54 } |
|
55 return (JSONObject) parsed; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Helper function to rip out an integer number from a JSON payload. |
|
60 * |
|
61 * @param obj JSON payload |
|
62 * @param key to look up |
|
63 * @return null if key not found or bad type |
|
64 */ |
|
65 public static Long getAsLong(JSONObject obj, CharSequence key) { |
|
66 String keyString = key.toString(); |
|
67 Object v = obj.get(keyString); |
|
68 if (v instanceof Long || v == null) { |
|
69 return (Long) v; |
|
70 } |
|
71 |
|
72 LOGGER.log(Level.SEVERE, "Key: {0}, found value: {1}", new Object[] {keyString, v}); |
|
73 return null; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Helper function to rip out a double from a JSON payload. |
|
78 * |
|
79 * @param obj JSON payload |
|
80 * @param key to look up |
|
81 * @return null if key not found or bad type |
|
82 */ |
|
83 public static Double getAsDouble(JSONObject obj, CharSequence key) { |
|
84 String keyString = key.toString(); |
|
85 Object v = obj.get(keyString); |
|
86 if (v instanceof Double || v == null) { |
|
87 return (Double) v; |
|
88 } |
|
89 LOGGER.log(Level.SEVERE, "Key: {0}, found value: {1}", new Object[] {keyString, v}); |
|
90 return null; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Helper function to rip out a string from a JSON payload. |
|
95 * |
|
96 * @param obj JSON payload |
|
97 * @param key to look up |
|
98 * @return null if key not found or bad type |
|
99 */ |
|
100 public static String getAsString(JSONObject obj, CharSequence key) { |
|
101 String keyString = key.toString(); |
|
102 Object v = obj.get(keyString); |
|
103 if (v instanceof String || v == null) { |
|
104 return (String) v; |
|
105 } |
|
106 return String.valueOf(v); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Helper function to rip out a Boolean from a JSON payload. |
|
111 * |
|
112 * @param obj JSON payload |
|
113 * @param key to look up |
|
114 * @return Boolean.FALSE if key not found |
|
115 */ |
|
116 public static Boolean getAsBoolean(JSONObject obj, CharSequence key) { |
|
117 String keyString = key.toString(); |
|
118 Object v = obj.get(keyString); |
|
119 if (v instanceof Boolean || v == null) { |
|
120 return v != null |
|
121 ? (Boolean) v |
|
122 : false; |
|
123 } |
|
124 |
|
125 LOGGER.log(Level.SEVERE, "Key: {0}, found value: {1}", new Object[] {keyString, v}); |
|
126 return false; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Helper function to rip out a nested JSON object from the payload. |
|
131 * |
|
132 * @param obj JSON payload |
|
133 * @param key to look up |
|
134 * @return null if key not found |
|
135 */ |
|
136 public static JSONObject getAsJSON(JSONObject obj, CharSequence key) { |
|
137 String keyString = key.toString(); |
|
138 Object v = obj.get(keyString); |
|
139 if (v instanceof JSONObject || v == null) { |
|
140 return (JSONObject) v; |
|
141 } |
|
142 |
|
143 LOGGER.log(Level.SEVERE, "Key: {0}, found value: {1}", new Object[] {keyString, v}); |
|
144 return null; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Helper function to rip out a nested JSON object from the payload or throw an exception. |
|
149 * @param obj JSON payload |
|
150 * @param key to look up |
|
151 * @return not null |
|
152 * @throws JsonException if failed to rip out the object |
|
153 */ |
|
154 public static JSONObject getAsJSONStrict(JSONObject obj, CharSequence key) { |
|
155 JSONObject result = getAsJSON(obj, key); |
|
156 if (result == null) { |
|
157 throw new JsonException("Failed to find property '" + key); |
|
158 } |
|
159 return result; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Helper function to rip out a JSONArray from the payload. |
|
164 * |
|
165 * @param obj JSON payload |
|
166 * @param key to look up |
|
167 * @return null if key not found |
|
168 */ |
|
169 public static JSONArray getAsJSONArray(JSONObject obj, CharSequence key) { |
|
170 String keyString = key.toString(); |
|
171 Object v = obj.get(keyString); |
|
172 if (v instanceof JSONArray || v == null) { |
|
173 return (JSONArray) v; |
|
174 } |
|
175 |
|
176 LOGGER.log(Level.SEVERE, "Key: {0}, found value: {1}", new Object[] {keyString, v}); |
|
177 return null; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Helper function to rip out a JSONArray from the payload or throw an exception. |
|
182 * |
|
183 * @param obj JSON payload |
|
184 * @param key to look up |
|
185 * @return not null |
|
186 * @throws JsonException if failed to rip out the array |
|
187 */ |
|
188 public static JSONArray getAsJSONArrayStrict(JSONObject obj, CharSequence key) { |
|
189 JSONArray result = getAsJSONArray(obj, key); |
|
190 if (result == null) { |
|
191 throw new JsonException("Failed to find property '" + key + "' of array type"); |
|
192 } |
|
193 return result; |
|
194 } |
|
195 |
|
196 /** |
|
197 * @param value to check |
|
198 * @return whether the value can be parsed as an integer |
|
199 */ |
|
200 public static boolean isInteger(String value) { |
|
201 try { |
|
202 Integer.parseInt(value); |
|
203 return true; |
|
204 } catch (NumberFormatException e) { |
|
205 return false; |
|
206 } |
|
207 } |
|
208 |
|
209 public static String quoteString(String string) { |
|
210 return "\"" + string + "\""; |
|
211 } |
|
212 |
|
213 private JsonUtil() { |
|
214 // not instantiable |
|
215 } |
|
216 } |