-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path183.java
More file actions
132 lines (105 loc) · 2.66 KB
/
Copy path183.java
File metadata and controls
132 lines (105 loc) · 2.66 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
package ca.carleton.gcrc.json;
import java.io.Writer;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONPrettyPrinter {
private Writer writer;
private String tab = "\t";
public JSONPrettyPrinter(Writer writer){
this.writer = writer;
}
public String getTab() {
return tab;
}
public void setTab(String tab) {
this.tab = tab;
}
public void prettyPrint(Object obj) throws Exception {
prettyPrint(obj, 0);
}
public void prettyPrint(Object obj, int indentLevel) throws Exception {
if( obj instanceof JSONObject ){
prettyPrintObject((JSONObject)obj, indentLevel);
} else if( obj instanceof JSONArray ){
prettyPrintArray((JSONArray)obj, indentLevel);
} else {
writer.write( JSONSupport.valueToString(obj) );
}
}
public void prettyPrintObject(JSONObject jsonObj, int indentLevel) throws Exception {
writer.write("{");
Iterator<?> it = jsonObj.keys();
if( it.hasNext() ) {
// Accumulate keys
List<String> keys = new Vector<String>();
while( it.hasNext() ){
Object key = it.next();
if( key instanceof String ){
keys.add( (String)key );
}
}
// Sort keys
Collections.sort(keys);
// Print out each key
boolean first = true;
for(String key : keys){
Object value = jsonObj.get(key);
// Print indentation
writer.write("\n");
for(int i=0,e=indentLevel+1;i<e;++i){
writer.write(tab);
}
// Print comma, if needed
if( first ) {
first = false;
} else {
writer.write(",");
}
// Print key
writer.write(JSONObject.quote(key));
writer.write(":");
// Print value
prettyPrint(value, indentLevel+1);
}
// Print indentation
writer.write("\n");
for(int i=0,e=indentLevel;i<e;++i){
writer.write(tab);
}
}
writer.write("}");
}
public void prettyPrintArray(JSONArray jsonArray, int indentLevel) throws Exception {
writer.write("[");
if( jsonArray.length() > 0 ) {
// Print out each item
boolean first = true;
for(int loop=0,arrayEnd=jsonArray.length(); loop<arrayEnd; ++loop){
Object value = jsonArray.get(loop);
// Print indentation
writer.write("\n");
for(int i=0,e=indentLevel+1;i<e;++i){
writer.write(tab);
}
// Print comma, if needed
if( first ) {
first = false;
} else {
writer.write(",");
}
// Print value
prettyPrint(value, indentLevel+1);
}
// Print indentation
writer.write("\n");
for(int i=0,e=indentLevel;i<e;++i){
writer.write(tab);
}
}
writer.write("]");
}
}