-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonBoolean.java
167 lines (138 loc) · 5.61 KB
/
JsonBoolean.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
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 boolean
* @author LupCode.com (Luca Vogels)
* @since 2020-12-23
*/
public class JsonBoolean extends JSON<JsonBoolean> {
private boolean value = false;
public JsonBoolean(){
}
public JsonBoolean(boolean value){
this.value = value;
}
/**
* Returns the value as boolean
* @return Boolean value
*/
public boolean getValue(){
return value;
}
/**
* Sets the value
* @param value Boolean value that should be set
* @return This instance
*/
public JsonBoolean setValue(boolean value){
this.value = value; return this;
}
@Override
protected void toJSON(OutputStream output, boolean prettyPrint, String whitespace) throws IOException {
output.write((value ? "true" : "false").getBytes(StandardCharsets.UTF_8));
}
@Override
protected JsonBoolean parseJSON(UTF8CharInputStream input, LineColumnTracker lct) throws JsonParseException, IOException {
String c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(new char[]{'t','f'}, lct); }
lct.increaseColumn();
if(c.equalsIgnoreCase("t")){
// parse true
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('r', lct); }
if(!c.equalsIgnoreCase("r")){ throw new JsonParseException('r', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('u', lct); }
if(!c.equalsIgnoreCase("u")){ throw new JsonParseException('u', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('e', lct); }
if(!c.equalsIgnoreCase("e")){ throw new JsonParseException('e', c, lct); }
lct.increaseColumn();
this.value = true;
return this;
} else if(c.equalsIgnoreCase("f")){
// parse false
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('a', lct); }
if(!c.equalsIgnoreCase("a")){ throw new JsonParseException('a', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('l', lct); }
if(!c.equalsIgnoreCase("l")){ throw new JsonParseException('l', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('s', lct); }
if(!c.equalsIgnoreCase("s")){ throw new JsonParseException('s', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('e', lct); }
if(!c.equalsIgnoreCase("e")){ throw new JsonParseException('e', c, lct); }
lct.increaseColumn();
this.value = false;
return this;
} else {
throw new JsonParseException(new char[]{'t','f'}, c, lct);
}
}
/** Tries to parse a given JSON string as an {@link JsonBoolean}
* @param json String that should be parsed
* @return Parsed JSON boolean
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonBoolean parse(String json) throws JsonParseException, NullPointerException {
return new JsonBoolean().parseJSON(json);
}
/** Tries to parse a given JSON string as an {@link JsonBoolean}
* @param json String that should be parsed
* @param offset Offset where to start reading the JSON string
* @return Parse JSON boolean
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonBoolean parse(String json, int offset) throws JsonParseException, NullPointerException {
return new JsonBoolean().parseJSON(json, offset);
}
/** Tries to parse a {@link JsonBoolean} from a given {@link InputStream} in UTF-8
* @param json Stream in UTF-8 that should be parsed
* @return Parse JSON boolean
* @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 JsonBoolean parse(UTF8CharInputStream input) throws JsonParseException, IOException, NullPointerException {
return new JsonBoolean().parseJSON(input);
}
/**
* Tries to parse a {@link JsonBoolean} from a given {@link File}
* @param file File the JSON data should be read from
* @return Parsed JSON boolean
* @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 JsonBoolean parse(File file) throws JsonParseException, NullPointerException, IOException{
return new JsonBoolean().parseJSON(file);
}
/**
* Tries to parse a {@link JsonBoolean} from a given {@link URL}
* @param url URL the JSON data should be read from
* @return Parsed JSON boolean
* @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 JsonBoolean parse(URL url) throws JsonParseException, NullPointerException, IOException{
return new JsonBoolean().parseJSON(url);
}
}