Skip to content

Commit facacbf

Browse files
committed
#16 - Parse error with skipValue() when using internal parser
1 parent a5701df commit facacbf

File tree

5 files changed

+100
-9
lines changed

5 files changed

+100
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.customer.repo;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
@Json
6+
public class Repo {
7+
public long id;
8+
public String name;
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example.customer.repo;
2+
3+
import io.avaje.jsonb.JsonType;
4+
import io.avaje.jsonb.Jsonb;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.InputStream;
8+
import java.util.List;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
class SkipValueTest {
13+
14+
Jsonb jsonb = Jsonb.newBuilder().build();
15+
16+
@Test
17+
void test_skipping() {
18+
JsonType<List<Repo>> list = jsonb.type(Repo.class).list();
19+
20+
InputStream is = SkipValueTest.class.getResourceAsStream("/skip-value-test-data.json");
21+
List<Repo> repos = list.fromJson(is);
22+
23+
assertThat(repos).hasSize(9);
24+
String asJson = list.toJson(repos);
25+
assertThat(asJson).isEqualTo("[{\"id\":1,\"name\":\"n0\"},{\"id\":2,\"name\":\"n1\"},{\"id\":3,\"name\":\"n2\"},{\"id\":4,\"name\":\"n3\"},{\"id\":5,\"name\":\"n4\"},{\"id\":6,\"name\":\"n5\"},{\"id\":7,\"name\":\"n6\"},{\"id\":8,\"name\":\"n7\"},{\"id\":9,\"name\":\"n8\"}]");
26+
}
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "n0",
5+
"skip_string": "234"
6+
},
7+
{
8+
"id": 2,
9+
"name": "n1",
10+
"skip_int": 139
11+
},
12+
{
13+
"id": 3,
14+
"name": "n2",
15+
"skip_dec": 895.34
16+
},
17+
{
18+
"id": 4,
19+
"node_id": "MDEwOlJlcG9zaXRvcnkxNTc2MDI1Mw==",
20+
"name": "n3",
21+
"skip_object": {
22+
"id": 208973,
23+
"node_id": "MDQ6VXNlcjIwODk3Mw=="
24+
}
25+
},
26+
{
27+
"id": 5,
28+
"name": "n4",
29+
"skip_array": [1,2,3,4]
30+
},
31+
{
32+
"id": 6,
33+
"name": "n5",
34+
"skip_null": null
35+
},
36+
{
37+
"id": 7,
38+
"name": "n6",
39+
"skip_true": true
40+
},
41+
{
42+
"id": 8,
43+
"name": "n7",
44+
"skip_false": false
45+
},
46+
{
47+
"id": 9,
48+
"skip_string": "234",
49+
"name": "n8"
50+
}
51+
]

jsonb/src/main/java/io/avaje/jsonb/stream/JParser.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -746,15 +746,20 @@ private byte skipString() {
746746
return nextToken();
747747
}
748748

749+
@Override
750+
public void skipValue() {
751+
skipNextValue();
752+
// go back one as nextToken() is called next
753+
last = buffer[--currentIndex];
754+
}
755+
749756
/**
750757
* Skip to next non-whitespace token (byte)
751758
* Will not allocate memory while skipping over JSON input.
752759
*
753760
* @return next non-whitespace byte
754-
* @throws IOException unable to read next byte (end of stream, invalid JSON, ...)
755761
*/
756-
@Override
757-
public byte skipValue() {
762+
private byte skipNextValue() {
758763
if (last == '"') return skipString();
759764
if (last == '{') {
760765
return skipObject();
@@ -782,10 +787,10 @@ public byte skipValue() {
782787

783788
private byte skipArray() {
784789
nextToken();
785-
byte nextToken = skipValue();
790+
byte nextToken = skipNextValue();
786791
while (nextToken == ',') {
787792
nextToken();
788-
nextToken = skipValue();
793+
nextToken = skipNextValue();
789794
}
790795
if (nextToken != ']') throw newParseError("Expecting ']' for array end");
791796
return nextToken();
@@ -801,7 +806,7 @@ private byte skipObject() {
801806
}
802807
if (nextToken != ':') throw newParseError("Expecting ':' after attribute name");
803808
nextToken();
804-
nextToken = skipValue();
809+
nextToken = skipNextValue();
805810
while (nextToken == ',') {
806811
nextToken = nextToken();
807812
if (nextToken == '"') {
@@ -811,7 +816,7 @@ private byte skipObject() {
811816
}
812817
if (nextToken != ':') throw newParseError("Expecting ':' after attribute name");
813818
nextToken();
814-
nextToken = skipValue();
819+
nextToken = skipNextValue();
815820
}
816821
if (nextToken != '}') throw newParseError("Expecting '}' for object end");
817822
return nextToken();
@@ -968,7 +973,6 @@ public void endObject() {
968973
private final Formatter errorFormatter = new Formatter(error);
969974

970975
JsonDataException newParseError(final String description) {
971-
if (errorInfo == ErrorInfo.MINIMAL) return new JsonDataException(description);
972976
error.setLength(0);
973977
error.append(description);
974978
error.append(". Found ");

jsonb/src/main/java/io/avaje/jsonb/stream/JsonParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ interface JsonParser extends Closeable {
5959
* This is typically use when we have read <code>nextField()</code>
6060
* and deemed that we are not interested in the value for that field.
6161
*/
62-
byte skipValue();
62+
void skipValue();
6363

6464
/**
6565
* Return true if the value to be read is NULL.

0 commit comments

Comments
 (0)