-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.java
More file actions
149 lines (127 loc) · 3.3 KB
/
Document.java
File metadata and controls
149 lines (127 loc) · 3.3 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package unimelb.bitbox.util;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Helper class for using JSON. Example usage:
* <pre>
* {@code
* Document doc1 = new Document();
* doc1.append("host","localhost");
* doc1.append("port",8111);
* String host = doc1.getString("host");
* int port = doc1.getInteger("port");
* String json1 = doc1.toJson(); // convert Document to a JSON String
* Document doc2 = Document.parse(json1); // convert JSON String back to Document
* ArrayList<Document> docs = new ArrayList<Document>();
* docs.add(doc1);
* docs.add(doc2);
* Document doc3 = new Document();
* doc3.append("docList",docs);
* doc3.toJson(); // {"docList":[{"host":"localhost","port":8111},{"host":"localhost","port":8111}]}
* ArrayList<Document> docs2 = (ArrayList<Document>) doc3.get("docList");
* }
* </pre>
* @author aaron
*
*/
public class Document {
//protected JSONObject obj;
public JSONObject obj;
public Document(){
obj=new JSONObject();
}
public Document(JSONObject obj){
this.obj = obj;
}
@SuppressWarnings("unchecked")
public void append(String key,String val){
if(val==null){
obj.put(key, null);
} else {
obj.put(key, new String(val));
}
}
@SuppressWarnings("unchecked")
public void append(String key,Document doc){
obj.put(key, doc.obj);
}
@SuppressWarnings("unchecked")
public void append(String key,boolean val){
obj.put(key, new Boolean(val));
}
@SuppressWarnings("unchecked")
public void append(String key,ArrayList<?> val){
JSONArray list = new JSONArray();
for(Object o : val){
if(o instanceof Document){
list.add(((Document)o).obj);
} else {
list.add(o);
}
}
obj.put(key,list);
}
@SuppressWarnings("unchecked")
public void append(String key,long val){
obj.put(key, new Long(val));
}
@SuppressWarnings("unchecked")
public void append(String key,int val){
obj.put(key, new Integer(val));
}
public String toJson(){
return obj.toJSONString();
}
public static Document parse(String json) {
JSONParser parser = new JSONParser();
try {
JSONObject obj = (JSONObject) parser.parse(json);
return new Document(obj);
} catch (ParseException e) {
return new Document();
} catch (ClassCastException e){
return new Document();
}
}
public boolean containsKey(String key){
return obj.containsKey(key);
}
public String getString(String key){
return (String) obj.get(key);
}
private ArrayList<Object> getList(JSONArray o){
ArrayList<Object> list = new ArrayList<Object>();
for(Object l : (JSONArray)o){
if(l instanceof JSONObject){
list.add(new Document((JSONObject) l));
} else if(l instanceof JSONArray){
list.add(getList((JSONArray) l));
} else {
list.add(l);
}
}
return list;
}
public Object get(String key){
Object o = obj.get(key);
if(o instanceof JSONObject){
return (Object) new Document((JSONObject) o);
} else if(o instanceof JSONArray){
return getList((JSONArray)o);
} else {
return o;
}
}
public int getInteger(String key){
return (int) obj.get(key);
}
public long getLong(String key){
return (long) obj.get(key);
}
public boolean getBoolean(String key){
return (boolean) obj.get(key);
}
}