Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 35e721d

Browse files
authored
Merge pull request #12 from LSafer-Agile/master
Bugfix in json parsers
2 parents 333876b + 75c9cd0 commit 35e721d

10 files changed

Lines changed: 65 additions & 27 deletions

File tree

src/main/java/org/cufy/http/json/JsonArray.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ public JsonArray(@NotNull Consumer<JsonArray> builder) {
104104
public static JsonArray parse(@NotNull @Language("json") String source) {
105105
Objects.requireNonNull(source, "source");
106106
try {
107-
return new JsonArrayToken(new JsonTokenSource(new StringReader(source)))
108-
.nextElement();
107+
JsonArrayToken token = new JsonArrayToken(new JsonTokenSource(new StringReader(source)));
108+
token.nextWhitespace();
109+
JsonArray array = token.nextElement();
110+
token.nextWhitespace();
111+
token.assertFinished();
112+
return array;
109113
} catch (JsonTokenException e) {
110114
throw new IllegalArgumentException(e.formatMessage(source), e);
111115
} catch (IOException e) {

src/main/java/org/cufy/http/json/JsonBoolean.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ public JsonBoolean(boolean value) {
7171
public static JsonBoolean parse(@NotNull @Language("json") String source) {
7272
Objects.requireNonNull(source, "source");
7373
try {
74-
return new JsonBooleanToken(new JsonTokenSource(new StringReader(source)))
75-
.nextElement();
74+
JsonBooleanToken token = new JsonBooleanToken(new JsonTokenSource(new StringReader(source)));
75+
token.nextWhitespace();
76+
JsonBoolean b = token.nextElement();
77+
token.nextWhitespace();
78+
token.assertFinished();
79+
return b;
7680
} catch (JsonTokenException e) {
7781
throw new IllegalArgumentException(e.formatMessage(source), e);
7882
} catch (IOException e) {

src/main/java/org/cufy/http/json/JsonNull.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.cufy.http.json;
1717

18+
import org.cufy.http.json.token.JsonNumberToken;
1819
import org.cufy.http.json.token.JsonTokenException;
1920
import org.cufy.http.json.token.JsonTokenSource;
2021
import org.cufy.http.json.token.JsonNullToken;
@@ -62,8 +63,12 @@ public JsonNull() {
6263
public static JsonNull parse(@NotNull @Language("json") String source) {
6364
Objects.requireNonNull(source, "source");
6465
try {
65-
return new JsonNullToken(new JsonTokenSource(new StringReader(source)))
66-
.nextElement();
66+
JsonNullToken token = new JsonNullToken(new JsonTokenSource(new StringReader(source)));
67+
token.nextWhitespace();
68+
JsonNull n = token.nextElement();
69+
token.nextWhitespace();
70+
token.assertFinished();
71+
return n;
6772
} catch (JsonTokenException e) {
6873
throw new IllegalArgumentException(e.formatMessage(source), e);
6974
} catch (IOException e) {

src/main/java/org/cufy/http/json/JsonNumber.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ public static JsonNumber from(double number) {
114114
public static JsonNumber parse(@NotNull @Language("json") String source) {
115115
Objects.requireNonNull(source, "source");
116116
try {
117-
return new JsonNumberToken(new JsonTokenSource(new StringReader(source)))
118-
.nextElement();
117+
JsonNumberToken token = new JsonNumberToken(new JsonTokenSource(new StringReader(source)));
118+
token.nextWhitespace();
119+
JsonNumber number = token.nextElement();
120+
token.nextWhitespace();
121+
token.assertFinished();
122+
return number;
119123
} catch (JsonTokenException e) {
120124
throw new IllegalArgumentException(e.formatMessage(source), e);
121125
} catch (IOException e) {

src/main/java/org/cufy/http/json/JsonObject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ public JsonObject(@NotNull Consumer<JsonObject> builder) {
103103
public static JsonObject parse(@NotNull @Language("json") String source) {
104104
Objects.requireNonNull(source, "source");
105105
try {
106-
return new JsonObjectToken(new JsonTokenSource(new StringReader(source)))
107-
.nextElement();
106+
JsonObjectToken token = new JsonObjectToken(new JsonTokenSource(new StringReader(source)));
107+
token.nextWhitespace();
108+
JsonObject object = token.nextElement();
109+
token.nextWhitespace();
110+
token.assertFinished();
111+
return object;
108112
} catch (JsonTokenException e) {
109113
throw new IllegalArgumentException(e.formatMessage(source), e);
110114
} catch (IOException e) {

src/main/java/org/cufy/http/json/JsonString.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ public JsonString(@NotNull String string) {
7474
public static JsonString parse(@NotNull @Language("json") String source) {
7575
Objects.requireNonNull(source, "source");
7676
try {
77-
return new JsonStringToken(new JsonTokenSource(new StringReader(source)))
78-
.nextElement();
77+
JsonStringToken token = new JsonStringToken(new JsonTokenSource(new StringReader(source)));
78+
token.nextWhitespace();
79+
JsonString string = token.nextElement();
80+
token.nextWhitespace();
81+
token.assertFinished();
82+
return string;
7983
} catch (JsonTokenException e) {
8084
throw new IllegalArgumentException(e.formatMessage(source), e);
8185
} catch (IOException e) {

src/main/java/org/cufy/http/json/token/AbstractJsonToken.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ protected AbstractJsonToken(@NotNull JsonTokenSource source) {
5252
this.source = source;
5353
}
5454

55+
/**
56+
* Assert the source finished.
57+
*
58+
* @throws JsonTokenException if the source has not finished.
59+
* @throws IOException if any I/O exception occurs.
60+
* @since 1.0.0 ~2022.01.07
61+
*/
62+
@Contract(pure = true)
63+
public void assertFinished() throws IOException {
64+
this.source.mark(1);
65+
if (this.source.read() != -1) {
66+
this.source.reset();
67+
throw new JsonTokenException(
68+
"Unexpected token",
69+
this.source.nextIndex()
70+
);
71+
}
72+
}
73+
5574
/**
5675
* Peek the next character.
5776
*
@@ -61,7 +80,7 @@ protected AbstractJsonToken(@NotNull JsonTokenSource source) {
6180
* @since 0.3.0 ~2021.11.24
6281
*/
6382
@Contract(pure = true)
64-
protected int maybePeekChar() throws IOException {
83+
public int maybePeekChar() throws IOException {
6584
this.source.mark(1);
6685
int read = this.source.read();
6786

@@ -81,7 +100,7 @@ protected int maybePeekChar() throws IOException {
81100
* @since 0.3.0 ~2021.11.23
82101
*/
83102
@Contract(mutates = "this")
84-
protected char nextChar() throws IOException {
103+
public char nextChar() throws IOException {
85104
int read = this.source.read();
86105

87106
if (read == -1)
@@ -102,7 +121,7 @@ protected char nextChar() throws IOException {
102121
* @since 0.3.0 ~2021.11.24
103122
*/
104123
@Contract(mutates = "this")
105-
protected JsonElement nextChildElement() throws IOException {
124+
public JsonElement nextChildElement() throws IOException {
106125
char c = this.peekChar();
107126

108127
if (c == '\"')
@@ -134,7 +153,7 @@ protected JsonElement nextChildElement() throws IOException {
134153
* @since 0.3.0 ~2021.11.24
135154
*/
136155
@Contract(mutates = "this")
137-
protected void nextWhitespace() throws IOException {
156+
public void nextWhitespace() throws IOException {
138157
while (true)
139158
switch (this.maybePeekChar()) {
140159
case ' ':
@@ -157,7 +176,7 @@ protected void nextWhitespace() throws IOException {
157176
* @since 0.3.0 ~2021.11.24
158177
*/
159178
@Contract(pure = true)
160-
protected char peekChar() throws IOException {
179+
public char peekChar() throws IOException {
161180
this.source.mark(1);
162181
int read = this.source.read();
163182

src/main/java/org/cufy/http/json/token/JsonContextToken.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ public JsonElement nextElement() throws IOException {
4747
this.nextWhitespace();
4848
JsonElement element = this.nextChildElement();
4949
this.nextWhitespace();
50-
51-
if (this.source.read() != -1)
52-
throw new JsonTokenException(
53-
"Unexpected token",
54-
this.source.nextIndex()
55-
);
56-
50+
this.assertFinished();
5751
return element;
5852
}
5953
}

src/main/java/org/cufy/http/json/token/JsonNumberToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public BigDecimal nextNumber() throws IOException {
8888
* @since 0.3.0 ~2021.11.23
8989
*/
9090
@Contract(mutates = "this")
91-
protected int maybeNextNumberChar() throws IOException {
91+
public int maybeNextNumberChar() throws IOException {
9292
this.source.mark(1);
9393

9494
int read = this.source.read();

src/main/java/org/cufy/http/json/token/JsonStringToken.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public String nextString() throws IOException {
9292
* @since 0.3.0 ~2021.11.23
9393
*/
9494
@Contract(mutates = "this")
95-
protected char nextEncoded() throws IOException {
95+
public char nextEncoded() throws IOException {
9696
char[] buffer = {0, 0, 0, 0};
9797
int index = 0;
9898

@@ -124,7 +124,7 @@ protected char nextEncoded() throws IOException {
124124
*/
125125
@NotNull
126126
@Contract(mutates = "this")
127-
protected String nextEscaped() throws IOException {
127+
public String nextEscaped() throws IOException {
128128
char c = this.nextChar();
129129

130130
switch (c) {

0 commit comments

Comments
 (0)