-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathTestJSONParsing.java
More file actions
81 lines (66 loc) · 2.57 KB
/
TestJSONParsing.java
File metadata and controls
81 lines (66 loc) · 2.57 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
import com.realtimetech.opack.codec.json.Json;
import com.realtimetech.opack.exception.DecodeException;
import com.realtimetech.opack.value.OpackValue;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestJSONParsing {
public static String readFileAutoDetect(Path path) throws IOException {
try (InputStream inputStream = Files.newInputStream(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
bufferedInputStream.mark(4);
byte[] bom = new byte[4];
int read = bufferedInputStream.read(bom, 0, bom.length);
Charset encoding;
int bomLength = 0;
if (read >= 3 && bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF) {
encoding = StandardCharsets.UTF_8;
bomLength = 3;
} else if (read >= 2 && bom[0] == (byte) 0xFE && bom[1] == (byte) 0xFF) {
encoding = StandardCharsets.UTF_16BE;
bomLength = 2;
} else if (read >= 2 && bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE) {
encoding = StandardCharsets.UTF_16LE;
bomLength = 2;
} else {
encoding = StandardCharsets.UTF_8;
}
bufferedInputStream.reset();
bufferedInputStream.skip(bomLength);
return new String(bufferedInputStream.readAllBytes(), encoding);
}
}
public static boolean isValidJSON(String jsonString) {
try {
Object object = Json.decodeObject(jsonString);
System.out.println(object);
return true;
} catch (DecodeException exception) {
exception.printStackTrace();
return false;
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java TestJSONParsing file.json");
System.exit(2);
}
try {
String jsonString = readFileAutoDetect(Paths.get(args[0]));
if (isValidJSON(jsonString)) {
System.out.println("valid");
System.exit(0);
}
System.out.println("invalid");
System.exit(1);
} catch (IOException exception) {
System.out.println("not found");
System.exit(2);
}
}
}