-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSON.java
473 lines (406 loc) · 13.6 KB
/
JSON.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package creek;
import java.util.*;
public class JSON extends AbstractTree {
// states for FSM
private static final int INIT = 0;
private static final int FIND_KEY = 1;
private static final int FIND_KEY_COMMENT = 2;
private static final int KEY = 3;
private static final int KEY_UNQUOTED = 4;
private static final int KEY_DELIM = 5;
private static final int VALUE = 6;
private static final int VALUE_COMMENT = 7;
private static final int STRING = 8;
private static final int STRING_NONQUOTE = 9;
private static final int STRING_ESCAPE = 10;
private static final int CHECK_FOR_UNESCAPED_QUOTE = 11;
private static final String[] reverse_state = {
"INIT",
"FIND_KEY",
"FIND_KEY_COMMENT",
"KEY",
"KEY_UNQUOTED",
"KEY_DELIM",
"VALUE",
"VALUE_COMMENT",
"STRING",
"STRING_NONQUOTE",
"STRING_ESCAPE",
"CHECK_FOR_UNESCAPED_QUOTE"
};
// used by FSM
private static final boolean OBJECT_MODE = true;
private static final boolean ARRAY_MODE = false;
private List<Boolean> modes = new ArrayList<Boolean>();
private List<String> keys = new ArrayList<>();
private StringBuilder keyUnderConstruction = null;
private StringBuilder valueUnderConstruction = null;
private String currentSerial = null;
private int charCount;
private int lineCount;
private int absoluteCount;
private boolean trailingComma = false;
// Leniency:
private int leniency;
// 3: as relaxed as possible, 2: just the essentials, 1: very rigorous, 0: overly pedantic
public static final int RELAXED = 3;
public static final int CAREFUL = 2;
public static final int RIGOROUS = 1;
public static final int PEDANTIC = 0;
// Seriousness:
// 3: caution label, 2: important note, 1: just FYI
public static final int CAUTION = 3;
public static final int NOTE = 2;
public static final int INFO = 1;
private static final String[] reverse_seriousness = {
"",
"INFO",
"NOTE",
"CAUTION"
};
// 0: no sorting, 1: maintain original sort order, 2: auto-sort
private int sortMode;
public static final int NO_ORDER = 0;
public static final int RETAIN_ORDER = 1;
public static final int AUTO_ORDER = 2;
// automatically recognize arrays by consecutive integers starting at 0
private boolean printArrays;
// print degug state-change information
private boolean printDebug;
// allow for unescaped embedded quotes by instead using [ non-space_and_non-ctrl .. comma_or_ctrl ] to deliniate string values
private boolean unescapedQuotes;
// normal constructors
public JSON ( int sortMode, int leniency, boolean printArrays, boolean printDebug, boolean unescapedQuotes ) {
this.leniency = leniency;
this.sortMode = sortMode;
this.printArrays = printArrays;
this.printDebug = printDebug;
this.unescapedQuotes = unescapedQuotes;
}
public JSON ( int sortMode, int leniency ) {
this( sortMode, leniency, true, false, false );
}
public JSON ( int sortMode ) {
this( sortMode, RELAXED, true, false, false );
}
public JSON () {
this( RETAIN_ORDER, RELAXED, true, false, false );
}
public Tree create () {
return new JSON( sortMode, leniency, printArrays, printDebug, unescapedQuotes );
}
// constructors that can throw Exception
public JSON ( String serial, int sortMode, int leniency ) throws Exception {
this( sortMode, leniency, true, false, false );
deserialize( serial );
}
public JSON ( String serial, int sortMode ) throws Exception {
this( serial, sortMode, RELAXED );
}
public JSON ( String serial ) throws Exception {
this( serial, RETAIN_ORDER, RELAXED );
}
////////// map( ) //////////
public Map<String,Tree> map () {
if (map==null) {
if (sortMode == 0) map = new HashMap<>();
else if (sortMode == 1) map = new LinkedHashMap<>();
else map = new TreeMap<>();
}
return map;
}
////////// deserialize( ) //////////
private boolean currentMode () {
return modes.get(modes.size()-1).booleanValue();
}
private Tree currentBranch () {
if (keys.size()<1) return this;
return auto(keys.subList(1,keys.size()));
}
private void throwException ( int seriousness, String message ) throws Exception {
if (seriousness > leniency) {
if (printDebug) System.err.println( serialize() );
System.err.println( stackState() );
System.err.println( surroundingText( 100 ) );
throw new Exception( reverse_seriousness[seriousness]+": "+message );
}
}
private String charLocation () {
return "line "+lineCount+" character "+charCount+" (index "+absoluteCount+" of length "+currentSerial.length()+")";
}
private String stackState () {
return "keys="+keys+" modes="+modes+" key="+keyUnderConstruction+" val="+valueUnderConstruction+" comma="+trailingComma;
}
private String currentSubString ( int start, int end ) {
if (end > currentSerial.length()) end = currentSerial.length();
if (start > end-1) start = end-1;
if (start >= 0) return currentSerial.substring( start, end );
return "";
}
private String surroundingText ( int length ) throws Exception {
return
currentSubString( absoluteCount-length, absoluteCount )+
">>>"+currentSubString( absoluteCount, absoluteCount+1 )+"<<<"+
currentSubString( absoluteCount+1, currentSerial.length() )
;
}
private boolean isWord ( char c ) {
return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='.';
}
private boolean isSpace ( char c ) {
return (c==' ' || c=='\t' || c=='\r' || c=='\n');
}
private boolean isComma ( char c ) {
return (c==',');
}
private void pushObjectKey () {
if (keyUnderConstruction == null) keys.add( null );
else {
keys.add( keyUnderConstruction.toString() );
keyUnderConstruction = null;
}
}
private void pushArrayKey () {
keys.add( currentBranch().integerKey() );
keyUnderConstruction = null;
}
private void popKey () throws Exception {
if (trailingComma) throwException( INFO, "trailing comma inferred before "+charLocation() );
if (keys.size()==0) {
throwException( CAUTION, "found more closings ('}' or ']') than openings ('{' or '[') after "+charLocation() );
return;
}
keys.remove( keys.size()-1 );
modes.remove( modes.size()-1 );
}
private void newValue () {
if (currentMode()==OBJECT_MODE) currentBranch().add( keyUnderConstruction.toString(), valueUnderConstruction.toString() );
else currentBranch().add( valueUnderConstruction.toString() );
valueUnderConstruction = null;
keyUnderConstruction = null;
}
private void initKey () {
keyUnderConstruction = new StringBuilder();
}
private void initValue () {
valueUnderConstruction = new StringBuilder();
}
// FSM
public Tree deserialize ( String serial ) throws Exception {
currentSerial = serial;
charCount = 1;
lineCount = 1;
absoluteCount = 0;
int state = VALUE;
//currentMode( OBJECT_MODE );
for (Character c : serial.toCharArray()) {
if (printDebug) System.err.print( c+": "+reverse_state[state]+" -> " );
if (state == FIND_KEY) {
if (c == '"') {
initKey();
state = KEY;
} else if (isWord(c)) {
initKey();
state = KEY_UNQUOTED;
keyUnderConstruction.append(c);
throwException( INFO, "original JSON spec does not allow unquoted keys ("+charLocation()+")" );
} else if (c == '}') {
popKey();
if (modes.size()>0 && currentMode()==ARRAY_MODE) state = VALUE;
} else if (c == ',') {
trailingComma = true;
} else if (c == '/') {
state = FIND_KEY_COMMENT;
} else if (!isSpace(c)) {
throwException( CAUTION, "found non-space character '"+c+"' at "+charLocation() );
}
} else if (state == FIND_KEY_COMMENT) {
if (c == '\n') {
state = FIND_KEY;
}
} else if (state == KEY) {
if (c == '"') {
state = KEY_DELIM;
} else {
keyUnderConstruction.append(c);
}
} else if (state == KEY_UNQUOTED) {
if (c == ':') {
state = VALUE;
} else if (c == ' ' || !isWord(c)) {
state = KEY_DELIM;
} else {
keyUnderConstruction.append(c);
}
} else if (state == KEY_DELIM) {
if (c == ':') {
state = VALUE;
} else if (!isSpace(c)) {
throwException( CAUTION, "found non-space character '"+c+"' before colon at "+charLocation() );
}
} else if (state == VALUE) {
if (c == '{') {
trailingComma = false;
if (modes.size()>0 && currentMode()==ARRAY_MODE) pushArrayKey();
else pushObjectKey();
modes.add( OBJECT_MODE );
keyUnderConstruction = null;
state = FIND_KEY;
} else if (c == '[') {
trailingComma = false;
if (modes.size()==0) throwException( INFO, "original JSON spec does not allow arrays at the root level ("+charLocation()+")" );
if (modes.size()>0 && currentMode()==OBJECT_MODE) pushObjectKey();
else pushArrayKey();
modes.add( ARRAY_MODE );
keyUnderConstruction = null;
//keyUnderConstruction = new StringBuilder( currentBranch().integerKey() );
// no initKey for arrays
} else if (c == '"') {
if (modes.size()==0) throwException( INFO, "original JSON spec does not allow key instantiation at the root level ("+charLocation()+")" );
initValue();
state = STRING;
} else if (isWord(c)) {
initValue();
valueUnderConstruction.append(c);
state = STRING_NONQUOTE;
} else if (c == ']') {
popKey();
if (modes.size()>0 && currentMode()) state = FIND_KEY; // object mode
} else if (c == ',') {
trailingComma = true;
} else if (c == '/') {
state = VALUE_COMMENT;
} else if (!isSpace(c)) {
throwException( CAUTION, "found non-space character '"+c+"' at "+charLocation() );
}
} else if (state == VALUE_COMMENT) {
if (c == '\n') {
state = VALUE;
}
} else if (state == STRING) {
if (c == '\\') {
state = STRING_ESCAPE;
} else if (c == '"') {
if (unescapedQuotes) {
state = CHECK_FOR_UNESCAPED_QUOTE;
} else {
trailingComma = false;
newValue();
if (currentMode()==OBJECT_MODE) state = FIND_KEY;
else state = VALUE; // array mode
}
} else {
valueUnderConstruction.append(c);
}
} else if (state == STRING_NONQUOTE) {
if (c == '\\') {
state = STRING_ESCAPE;
} else if (isSpace(c) || isComma(c)) {
newValue();
if (currentMode()==OBJECT_MODE) state = FIND_KEY;
else state = VALUE; // array mode
/*} else if (! isWord(c)) {
throwException( "found '"+c+"' in non-quoted value ("+valueUnderConstruction+") to be added to "+objectHierarchy()+" at "+charLocation() );
*/
} else if (c == ']' || c == '}') {
newValue();
popKey();
if (currentMode()==OBJECT_MODE) state = FIND_KEY;
else state = VALUE; // array mode
} else {
valueUnderConstruction.append(c);
}
} else if (state == STRING_ESCAPE) {
valueUnderConstruction.append(c);
state = STRING;
} else if (state == CHECK_FOR_UNESCAPED_QUOTE) {
if (c == ',' || c == '\n' || c == '\r' || c == '}' || c == ']') {
if (c != ',') trailingComma = false;
newValue();
if (currentMode()==OBJECT_MODE) state = FIND_KEY;
else state = VALUE; // array mode
} else if (c == '\\') {
valueUnderConstruction.append('"');
state = STRING_ESCAPE;
} else {
valueUnderConstruction.append('"');
valueUnderConstruction.append(c);
state = STRING;
}
}
if (printDebug) System.err.println( reverse_state[state]+" "+stackState() );
absoluteCount++;
charCount++;
if (c=='\n') {
lineCount++;
charCount=0;
}
}
if (keys.size()!=0) {
throwException( NOTE, "found more openings ('{' or '[') than closings ('}' or ']') after end of stream at "+charLocation() );
}
currentSerial = null;
return this;
}
////////// serialize() //////////
private void indent ( StringBuilder sb, int length ) {
for (int i=0; i<length; i++) sb.append( "\t" );
}
private void serialize ( Tree branch, StringBuilder json, boolean readable, int i ) {
boolean isArray = (branch.integerKeys() && printArrays);
if (isArray) json.append("[");
else json.append("{");
String comma = "";
for (Map.Entry<String,Tree> entry : branch.map().entrySet()) {
if (readable) json.append(comma).append("\n");
if (readable) indent( json, i+1 );
if (! isArray) json.append("\"").append( entry.getKey() ).append("\": ");
Tree subBranch = entry.getValue();
if (subBranch.size()==0) {
String value = subBranch.value();
if (value==null) {
json.append("null");
} else if (value.equals("")) {
json.append("\"\"");
} else if (value.equals("true") || value.equals("false") || !Regex.exists( value, "[^\\d]" )) {
json.append( value );
} else {
json.append("\"").append(
value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" )
).append("\"");
}
} else {
serialize( subBranch, json, readable, i+1 );
}
comma = ",";
}
if (readable) json.append("\n");
if (readable) indent( json, i );
if (isArray) json.append("]");
else json.append("}");
}
public String serialize ( boolean readable ) {
StringBuilder json = new StringBuilder();
serialize( this, json, readable, 0 );
return json.toString();
}
public String serialize () {
return serialize( true );
}
}
class ExecCleanJSON {
public static void main ( String[] args ) throws Exception {
int leniency = ( args.length>1 ? Integer.parseInt(args[1]) : 3 );
int sortMode = ( args.length>2 ? Integer.valueOf(args[2]) : 2 );
boolean printArrays = ( args.length>3 ? Boolean.valueOf(args[3]) : true );
boolean printDebug = ( args.length>4 ? Boolean.valueOf(args[4]) : false );
boolean unescapedQuotes = ( args.length>5 ? Boolean.valueOf(args[5]) : false );
Tree json = new JSON( leniency, sortMode, printArrays, printDebug, unescapedQuotes );
json.deserialize(
FileActions.read( args[0] )
);
System.out.println(
json.serialize()
);
}
}