-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonString.java
More file actions
157 lines (137 loc) · 4.84 KB
/
JsonString.java
File metadata and controls
157 lines (137 loc) · 4.84 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
150
151
152
153
154
155
156
157
package com.lupcode.JSON;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import com.lupcode.JSON.exceptions.JsonParseException;
import com.lupcode.JSON.exceptions.JsonParseUnfinishedException;
import com.lupcode.JSON.utils.LineColumnTracker;
import com.lupcode.Utilities.streams.UTF8CharInputStream;
/** Represents a JSON string
* @author LupCode.com (Luca Vogels)
* @since 2020-12-23
*/
public class JsonString extends JSON<JsonString> {
private String value = null;
public JsonString(){
}
public JsonString(String value){
this.value = value;
}
/**
* Returns the value as {@link String}
* @return String value
*/
public String getValue(){
return value;
}
/**
* Sets the value
* @param value String value that should be set
* @return This instance
*/
public JsonString setValue(String value){
this.value = value; return this;
}
@Override
protected void toJSON(OutputStream output, boolean prettyPrint, String whitespace) throws IOException {
if(value!=null){
output.write("\"".getBytes(StandardCharsets.UTF_8));
output.write(value.getBytes(StandardCharsets.UTF_8));
output.write("\"".getBytes(StandardCharsets.UTF_8));
} else {
new JsonNull().toJSON(output, prettyPrint, whitespace);
}
}
@Override
protected JsonString parseJSON(UTF8CharInputStream input, LineColumnTracker lct) throws JsonParseException, IOException {
boolean ignore = false, ended = false;
StringBuilder sb = new StringBuilder();
String c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(new char[]{'"', '\''}, lct); }
if(!c.equals("\"") && !c.equals("\'"))
throw new JsonParseException(new char[]{'"', '\''}, c, lct);
String opener = c;
lct.increaseColumn();
while((c = input.readChar())!=null){
if(!isLineBreaker(c)) {
lct.increaseColumn();
} else { lct.increaseLine(); }
if(c.equals(opener)){
if(ignore){
sb.append(c);
ignore = false;
} else {
ended = true;
break;
}
} else if(c.equals("\\")){
if(ignore){
sb.append(c);
ignore = false;
} else {
ignore = true;
}
} else {
sb.append(c);
ignore = false;
}
}
if(!ended){ throw new JsonParseUnfinishedException(opener.charAt(0), lct); }
this.value = sb.toString();
return this;
}
/** Tries to parse a given JSON string as a {@link JsonString}
* @param json String that should be parsed
* @return Parsed JSON string
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonString parse(String json) throws JsonParseException, NullPointerException {
return new JsonString().parseJSON(json);
}
/** Tries to parse a given JSON string as a {@link JsonString}
* @param json String that should be parsed
* @param offset Offset where to start reading the JSON string
* @return Parse JSON string
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonString parse(String json, int offset) throws JsonParseException, NullPointerException {
return new JsonString().parseJSON(json, offset);
}
/** Tries to parse a {@link JsonString} from a given {@link InputStream} in UTF-8
* @param json Stream in UTF-8 that should be parsed
* @return Parse JSON string
* @throws JsonParseException if input could not be parsed correctly
* @throws IOException if an error while reading {@link InputStream} occurred
* @throws NullPointerException if input is null
*/
public static JsonString parse(UTF8CharInputStream input) throws JsonParseException, IOException, NullPointerException {
return new JsonString().parseJSON(input);
}
/**
* Tries to parse a {@link JsonString} from a given {@link File}
* @param file File the JSON data should be read from
* @return Parsed JSON string
* @throws JsonParseException if file could not be parsed correctly
* @throws NullPointerException if file is null
* @throws IOException if an error occurs while reading the {@link File}
*/
public static JsonString parse(File file) throws JsonParseException, NullPointerException, IOException{
return new JsonString().parseJSON(file);
}
/**
* Tries to parse a {@link JsonString} from a given {@link URL}
* @param url URL the JSON data should be read from
* @return Parsed JSON string
* @throws JsonParseException if {@link URL} could not be parsed correctly
* @throws NullPointerException if {@link URL} is null
* @throws IOException if an error occurs while reading the from the {@link URL}
*/
public static JsonString parse(URL url) throws JsonParseException, NullPointerException, IOException{
return new JsonString().parseJSON(url);
}
}