| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook.model;
|
|
|
18 |
|
|
|
19 |
import android.annotation.SuppressLint;
|
|
|
20 |
import org.json.JSONException;
|
|
|
21 |
import org.json.JSONObject;
|
|
|
22 |
|
|
|
23 |
import java.util.*;
|
|
|
24 |
|
|
|
25 |
class JsonUtil {
|
|
|
26 |
static void jsonObjectClear(JSONObject jsonObject) {
|
|
|
27 |
@SuppressWarnings("unchecked")
|
|
|
28 |
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
|
|
|
29 |
while (keys.hasNext()) {
|
|
|
30 |
keys.next();
|
|
|
31 |
keys.remove();
|
|
|
32 |
}
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
static boolean jsonObjectContainsValue(JSONObject jsonObject, Object value) {
|
|
|
36 |
@SuppressWarnings("unchecked")
|
|
|
37 |
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
|
|
|
38 |
while (keys.hasNext()) {
|
|
|
39 |
Object thisValue = jsonObject.opt(keys.next());
|
|
|
40 |
if (thisValue != null && thisValue.equals(value)) {
|
|
|
41 |
return true;
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
return false;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
private final static class JSONObjectEntry implements Map.Entry<String, Object> {
|
|
|
48 |
private final String key;
|
|
|
49 |
private final Object value;
|
|
|
50 |
|
|
|
51 |
JSONObjectEntry(String key, Object value) {
|
|
|
52 |
this.key = key;
|
|
|
53 |
this.value = value;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
@SuppressLint("FieldGetter")
|
|
|
57 |
@Override
|
|
|
58 |
public String getKey() {
|
|
|
59 |
return this.key;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@Override
|
|
|
63 |
public Object getValue() {
|
|
|
64 |
return this.value;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
@Override
|
|
|
68 |
public Object setValue(Object object) {
|
|
|
69 |
throw new UnsupportedOperationException("JSONObjectEntry is immutable");
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
static Set<Map.Entry<String, Object>> jsonObjectEntrySet(JSONObject jsonObject) {
|
|
|
75 |
HashSet<Map.Entry<String, Object>> result = new HashSet<Map.Entry<String, Object>>();
|
|
|
76 |
|
|
|
77 |
@SuppressWarnings("unchecked")
|
|
|
78 |
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
|
|
|
79 |
while (keys.hasNext()) {
|
|
|
80 |
String key = keys.next();
|
|
|
81 |
Object value = jsonObject.opt(key);
|
|
|
82 |
result.add(new JSONObjectEntry(key, value));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return result;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
static Set<String> jsonObjectKeySet(JSONObject jsonObject) {
|
|
|
89 |
HashSet<String> result = new HashSet<String>();
|
|
|
90 |
|
|
|
91 |
@SuppressWarnings("unchecked")
|
|
|
92 |
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
|
|
|
93 |
while (keys.hasNext()) {
|
|
|
94 |
result.add(keys.next());
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return result;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
static void jsonObjectPutAll(JSONObject jsonObject, Map<String, Object> map) {
|
|
|
101 |
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
|
|
|
102 |
for (Map.Entry<String, Object> entry : entrySet) {
|
|
|
103 |
try {
|
|
|
104 |
jsonObject.putOpt(entry.getKey(), entry.getValue());
|
|
|
105 |
} catch (JSONException e) {
|
|
|
106 |
throw new IllegalArgumentException(e);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
static Collection<Object> jsonObjectValues(JSONObject jsonObject) {
|
|
|
112 |
ArrayList<Object> result = new ArrayList<Object>();
|
|
|
113 |
|
|
|
114 |
@SuppressWarnings("unchecked")
|
|
|
115 |
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
|
|
|
116 |
while (keys.hasNext()) {
|
|
|
117 |
result.add(jsonObject.opt(keys.next()));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return result;
|
|
|
121 |
}
|
|
|
122 |
}
|