forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONObjectKeyOrder.java
58 lines (51 loc) · 1.18 KB
/
JSONObjectKeyOrder.java
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
package org.json;
import java.util.ArrayList;
import java.util.List;
/**
* sometimes we need key order in input,hashmap`s keyset is chaos.
* @author wangq
*
*/
public class JSONObjectKeyOrder extends JSONObject {
// store the name in order
private List<String> names;
public JSONObjectKeyOrder(String source) throws JSONException {
this(new JSONTokenerKeyOrder(source));
}
public JSONObjectKeyOrder(JSONTokenerKeyOrder jsonTokenerKeyOrder) {
super(jsonTokenerKeyOrder);
}
public JSONObjectKeyOrder putOnce(String key, Object value) throws JSONException {
super.putOnce(key, value);
if (names == null) {
names = new ArrayList<>();
}
names.add(key);
return this;
}
/**
* null is ugly,and evil
* @param jo
* @return
*/
public static String[] getNames(JSONObjectKeyOrder jo) {
if (jo.names == null) {
return new String[0];
}
return jo.names.toArray(new String[0]);
}
/**
* i do`t like the opt** methods,it`s hard to get type,such the everything can be cast to String
*
* @param key
* @return
*/
public boolean isInt(String key) {
try {
Integer.parseInt(optString(key));
return true;
} catch (Exception e) {
return false;
}
}
}