JSONUtil.java
2.23 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.cq.mobile.phonegap.impl.build.util;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public abstract class JSONUtil {
public static String getSafeString(JSONObject jsonObject, String key) {
String value = null;
try {
value = jsonObject.getString(key);
}
catch (JSONException ex) {
// empty catch block
}
return value;
}
public static boolean getSafeBoolean(JSONObject jsonObject, String key, boolean defaultIfMissing) {
boolean value = defaultIfMissing;
try {
value = jsonObject.getBoolean(key);
}
catch (JSONException ex) {
// empty catch block
}
return value;
}
public static int getSafeInt(JSONObject jsonObject, String key) {
try {
return jsonObject.getInt(key);
}
catch (JSONException ex) {
throw new RuntimeException("Data missing for:" + key);
}
}
public static JSONObject getSafeJSONObject(JSONObject jsonObject, String key) {
JSONObject value = null;
try {
value = jsonObject.getJSONObject(key);
}
catch (JSONException ex) {
// empty catch block
}
return value;
}
public static JSONArray getSafeJSONArray(JSONObject jsonObject, String key) {
JSONArray value = null;
try {
value = jsonObject.getJSONArray(key);
}
catch (JSONException ex) {
// empty catch block
}
return value;
}
public static Set<String> getKeys(JSONObject jsonObject) {
HashSet<String> keysSet = new HashSet<String>();
Iterator keysIter = jsonObject.keys();
while (keysIter.hasNext()) {
keysSet.add((String)keysIter.next());
}
return keysSet;
}
}