-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonNumber.java
237 lines (196 loc) · 6.97 KB
/
JsonNumber.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
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 number
* @author LupCode.com (Luca Vogels)
* @since 2020-12-23
*/
public class JsonNumber extends JSON<JsonNumber> {
private String value = "0";
private boolean has_comma = false;
public JsonNumber(){
}
public JsonNumber(String value){
setValue(value);
}
public JsonNumber(double value){
setValue(value);
}
public JsonNumber(float value){
setValue(value);
}
public JsonNumber(int value){
setValue(value);
}
public JsonNumber(long value){
setValue(value);
}
public JsonNumber(short value){
setValue(value);
}
public JsonNumber(byte value){
setValue(value);
}
/**
* Returns if the number contains a comma
* @return True if comma is contained
*/
public boolean hasComma(){
return has_comma;
}
/**
* Returns the value as {@link String}
* @return Number value
*/
public String getValue(){
return value;
}
/**
* Sets the value (if null then zero)
* @param value Number value that should be set
* @return This instance
*/
public JsonNumber setValue(String value){
if(value!=null){
parseJSON(value, new LineColumnTracker());
} else { this.value = "0"; this.has_comma = false; }
return this;
}
public JsonNumber setValue(double value){
this.value = value+""; this.has_comma = true; return this;
}
public JsonNumber setValue(float value){
this.value = value+""; this.has_comma = true; return this;
}
public JsonNumber setValue(long value){
this.value = value+""; this.has_comma = false; return this;
}
public JsonNumber setValue(short value){
this.value = value+""; this.has_comma = false; return this;
}
public JsonNumber setValue(byte value){
this.value = value+""; this.has_comma = false; return this;
}
public double getAsDouble(){
return Double.parseDouble(value);
}
public float getAsFloat(){
return Float.parseFloat(value);
}
public int getAsInt(){
return Integer.parseInt(value);
}
public long getAsLong(){
return Long.parseLong(value);
}
public short getAsShort(){
return Short.parseShort(value);
}
public byte getAsByte(){
return Byte.parseByte(value);
}
@Override
protected void toJSON(OutputStream output, boolean prettyPrint, String whitespace) throws IOException {
output.write(value.getBytes(StandardCharsets.UTF_8));
}
@Override
protected JsonNumber parseJSON(UTF8CharInputStream input, LineColumnTracker lct) throws JsonParseException, IOException {
StringBuilder sb = new StringBuilder();
boolean had_number = false, need_number = false, has_comma = false;
String c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(new char[]{'0','1','2','3','4','5','6','7','8','9','-','+','.'}, lct); }
if(c.equals("+") || c.equals("-")){
sb.append(c); need_number = true;
} else if(c.equals(".")){
sb.append("0."); had_number = true; need_number = true; has_comma = true;
} else if(isNumber(c)){
sb.append(c); had_number = true;
} else { throw new JsonParseException(new char[]{'0','1','2','3','4','5','6','7','8','9','-','+','.'}, c, lct); }
lct.increaseColumn();
while((c = skipIgnorers(input, lct))!=null){
if(isNumber(c)){
had_number = true; need_number = false;
sb.append(c);
} else if(c.equals(".")){
if(has_comma){
throw new JsonParseException(new char[]{'0','1','2','3','4','5','6','7','8','9'}, c, lct);
}
has_comma = true; need_number = true;
if(had_number){ sb.append('.'); } else { sb.append("0."); had_number = true; }
} else if(c.equals("+") || c.equals("-")){
throw new JsonParseException(new char[]{'0','1','2','3','4','5','6','7','8','9'}, c, lct);
} else {
input.insertReadAgainAtBeginning(c);
break;
}
lct.increaseColumn();
}
if(need_number){ throw new JsonParseUnfinishedException(new char[]{'0','1','2','3','4','5','6','7','8','9'}, lct); }
this.value = sb.toString();
this.has_comma = has_comma;
return this;
}
private boolean isNumber(String c){
return c!=null && (c.equals("0") || c.equals("1") || c.equals("2") || c.equals("3") ||
c.equals("4") || c.equals("5") || c.equals("6") || c.equals("7") || c.equals("8") || c.equals("9"));
}
/** Tries to parse a given JSON string as a {@link JsonNumber}
* @param json String that should be parsed
* @return Parsed JSON number
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonNumber parse(String json) throws JsonParseException, NullPointerException {
return new JsonNumber().parseJSON(json);
}
/** Tries to parse a given JSON string as a {@link JsonNumber}
* @param json String that should be parsed
* @param offset Offset where to start reading the JSON string
* @return Parse JSON number
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonNumber parse(String json, int offset) throws JsonParseException, NullPointerException {
return new JsonNumber().parseJSON(json, offset);
}
/** Tries to parse a {@link JsonNumber} from a given {@link InputStream} in UTF-8
* @param json Stream in UTF-8 that should be parsed
* @return Parse JSON number
* @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 JsonNumber parse(UTF8CharInputStream input) throws JsonParseException, IOException, NullPointerException {
return new JsonNumber().parseJSON(input);
}
/**
* Tries to parse a {@link JsonNumber} from a given {@link File}
* @param file File the JSON data should be read from
* @return Parsed JSON number
* @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 JsonNumber parse(File file) throws JsonParseException, NullPointerException, IOException{
return new JsonNumber().parseJSON(file);
}
/**
* Tries to parse a {@link JsonNumber} from a given {@link URL}
* @param url URL the JSON data should be read from
* @return Parsed JSON number
* @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 JsonNumber parse(URL url) throws JsonParseException, NullPointerException, IOException{
return new JsonNumber().parseJSON(url);
}
}