Skip to content

Commit c850737

Browse files
committed
JsonStructureParser fixes
- fix getObject() to work on root object - fix getObject() to advance parser state - fix getArray() to work on root array - fix getArray() to advance parser state
1 parent 34177db commit c850737

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import jakarta.json.*;
2020
import jakarta.json.stream.JsonLocation;
2121
import jakarta.json.stream.JsonParser;
22+
2223
import java.math.BigDecimal;
2324
import java.util.ArrayDeque;
2425
import java.util.Deque;
@@ -113,7 +114,10 @@ public JsonArray getArray() {
113114
throw new IllegalStateException(
114115
JsonMessages.PARSER_GETARRAY_ERR(state));
115116
}
116-
return (JsonArray) scopeStack.peek().getJsonValue();
117+
JsonArray array = (JsonArray) current.getJsonStructure();
118+
// #transition() will pop the stack
119+
this.state = Event.END_ARRAY;
120+
return array;
117121
}
118122

119123
@Override
@@ -122,7 +126,10 @@ public JsonObject getObject() {
122126
throw new IllegalStateException(
123127
JsonMessages.PARSER_GETOBJECT_ERR(state));
124128
}
125-
return (JsonObject) scopeStack.peek().getJsonValue();
129+
JsonObject object = (JsonObject) current.getJsonStructure();
130+
// #transition() will pop the stack
131+
this.state = Event.END_OBJECT;
132+
return object;
126133
}
127134

128135
@Override
@@ -271,8 +278,19 @@ private static Event getState(JsonValue value) {
271278
}
272279

273280
private static abstract class Scope implements Iterator {
281+
282+
private final JsonStructure jsonStructure;
283+
284+
Scope(JsonStructure jsonStructure) {
285+
this.jsonStructure = jsonStructure;
286+
}
287+
274288
abstract JsonValue getJsonValue();
275289

290+
JsonStructure getJsonStructure() {
291+
return jsonStructure;
292+
}
293+
276294
static Scope createScope(JsonValue value) {
277295
if (value instanceof JsonArray) {
278296
return new ArrayScope((JsonArray)value);
@@ -288,6 +306,7 @@ private static class ArrayScope extends Scope {
288306
private JsonValue value;
289307

290308
ArrayScope(JsonArray array) {
309+
super(array);
291310
this.it = array.iterator();
292311
}
293312

@@ -320,6 +339,7 @@ private static class ObjectScope extends Scope {
320339
private String key;
321340

322341
ObjectScope(JsonObject object) {
342+
super(object);
323343
this.it = object.entrySet().iterator();
324344
}
325345

impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,13 @@ private void testGetValueStructure(JsonParser parser) {
515515
JsonValue value = parser.getValue();
516516
Assertions.assertTrue(value instanceof JsonNumber);
517517
JsonNumber number = (JsonNumber) value;
518-
assertEquals(number.longValueExact(), 1L);
518+
Assertions.assertEquals(number.longValueExact(), 1L);
519519

520520
Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
521521
value = parser.getValue();
522522
Assertions.assertTrue(value instanceof JsonNumber);
523523
number = (JsonNumber) value;
524-
assertEquals(number.bigDecimalValue(), new BigDecimal("1.1"));
524+
Assertions.assertEquals(number.bigDecimalValue(), new BigDecimal("1.1"));
525525

526526
Assertions.assertEquals(Event.VALUE_TRUE, parser.next());
527527
value = parser.getValue();
@@ -535,7 +535,7 @@ private void testGetValueStructure(JsonParser parser) {
535535
value = parser.getValue();
536536
Assertions.assertTrue(value instanceof JsonString);
537537
JsonString string = (JsonString) value;
538-
assertEquals("aString", string.getString());
538+
Assertions.assertEquals("aString", string.getString());
539539

540540
Assertions.assertEquals(Event.VALUE_NULL, parser.next());
541541
value = parser.getValue();
@@ -545,7 +545,6 @@ private void testGetValueStructure(JsonParser parser) {
545545
Assertions.assertEquals(Event.START_ARRAY, parser.next());
546546
JsonArray array = parser.getArray();
547547
Assertions.assertTrue(array.isEmpty());
548-
Assertions.assertEquals(Event.END_ARRAY, parser.next());
549548

550549
Assertions.assertEquals(Event.START_OBJECT, parser.next());
551550

@@ -595,11 +594,55 @@ private void testGetValueStructure(JsonParser parser) {
595594
JsonObject object = parser.getObject();
596595
Assertions.assertTrue(object.isEmpty());
597596
Assertions.assertEquals(Event.END_OBJECT, parser.next());
598-
Assertions.assertEquals(Event.END_OBJECT, parser.next());
599597
Assertions.assertEquals(Event.END_ARRAY, parser.next());
600598
Assertions.assertFalse(parser.hasNext());
601599
}
602600

601+
@Test
602+
void testGetArrayRoot() {
603+
try (JsonParser parser = Json.createParserFactory(null).createParser(
604+
Json.createArrayBuilder()
605+
.add(1)
606+
.add(2)
607+
.add(3)
608+
.build())) {
609+
testGetArrayRoot(parser);
610+
}
611+
}
612+
613+
private void testGetArrayRoot(JsonParser parser) {
614+
Assertions.assertEquals(Event.START_ARRAY, parser.next());
615+
JsonArray actual = parser.getArray();
616+
JsonArray expected = Json.createArrayBuilder()
617+
.add(1)
618+
.add(2)
619+
.add(3)
620+
.build();
621+
Assertions.assertEquals(expected, actual);
622+
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
623+
Assertions.assertFalse(parser.hasNext());
624+
}
625+
626+
@Test
627+
void testGetObjectRoot() {
628+
try (JsonParser parser = Json.createParserFactory(null).createParser(
629+
Json.createObjectBuilder()
630+
.add("key1", "value1")
631+
.build())) {
632+
testGetObjectRoot(parser);
633+
}
634+
}
635+
636+
private void testGetObjectRoot(JsonParser parser) {
637+
Assertions.assertEquals(Event.START_OBJECT, parser.next());
638+
JsonObject actual = parser.getObject();
639+
JsonObject expected = Json.createObjectBuilder()
640+
.add("key1", "value1")
641+
.build();
642+
Assertions.assertEquals(expected, actual);
643+
Assertions.assertEquals(Event.END_OBJECT, parser.currentEvent());
644+
Assertions.assertFalse(parser.hasNext());
645+
}
603646

604647
@Test
605648
void testNestedArrayReader() {

0 commit comments

Comments
 (0)