From c850737abb3e05b8719d812a1ecde646c73e49ff Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Sat, 14 Dec 2024 17:57:14 +0100 Subject: [PATCH] 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 --- .../eclipse/parsson/JsonStructureParser.java | 24 ++++++++- .../eclipse/parsson/tests/JsonParserTest.java | 53 +++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java b/impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java index 1bd43cb..ed49562 100644 --- a/impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java +++ b/impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java @@ -19,6 +19,7 @@ import jakarta.json.*; import jakarta.json.stream.JsonLocation; import jakarta.json.stream.JsonParser; + import java.math.BigDecimal; import java.util.ArrayDeque; import java.util.Deque; @@ -113,7 +114,10 @@ public JsonArray getArray() { throw new IllegalStateException( JsonMessages.PARSER_GETARRAY_ERR(state)); } - return (JsonArray) scopeStack.peek().getJsonValue(); + JsonArray array = (JsonArray) current.getJsonStructure(); + // #transition() will pop the stack + this.state = Event.END_ARRAY; + return array; } @Override @@ -122,7 +126,10 @@ public JsonObject getObject() { throw new IllegalStateException( JsonMessages.PARSER_GETOBJECT_ERR(state)); } - return (JsonObject) scopeStack.peek().getJsonValue(); + JsonObject object = (JsonObject) current.getJsonStructure(); + // #transition() will pop the stack + this.state = Event.END_OBJECT; + return object; } @Override @@ -271,8 +278,19 @@ private static Event getState(JsonValue value) { } private static abstract class Scope implements Iterator { + + private final JsonStructure jsonStructure; + + Scope(JsonStructure jsonStructure) { + this.jsonStructure = jsonStructure; + } + abstract JsonValue getJsonValue(); + JsonStructure getJsonStructure() { + return jsonStructure; + } + static Scope createScope(JsonValue value) { if (value instanceof JsonArray) { return new ArrayScope((JsonArray)value); @@ -288,6 +306,7 @@ private static class ArrayScope extends Scope { private JsonValue value; ArrayScope(JsonArray array) { + super(array); this.it = array.iterator(); } @@ -320,6 +339,7 @@ private static class ObjectScope extends Scope { private String key; ObjectScope(JsonObject object) { + super(object); this.it = object.entrySet().iterator(); } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java index 822b763..c3be5bd 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java @@ -515,13 +515,13 @@ private void testGetValueStructure(JsonParser parser) { JsonValue value = parser.getValue(); Assertions.assertTrue(value instanceof JsonNumber); JsonNumber number = (JsonNumber) value; - assertEquals(number.longValueExact(), 1L); + Assertions.assertEquals(number.longValueExact(), 1L); Assertions.assertEquals(Event.VALUE_NUMBER, parser.next()); value = parser.getValue(); Assertions.assertTrue(value instanceof JsonNumber); number = (JsonNumber) value; - assertEquals(number.bigDecimalValue(), new BigDecimal("1.1")); + Assertions.assertEquals(number.bigDecimalValue(), new BigDecimal("1.1")); Assertions.assertEquals(Event.VALUE_TRUE, parser.next()); value = parser.getValue(); @@ -535,7 +535,7 @@ private void testGetValueStructure(JsonParser parser) { value = parser.getValue(); Assertions.assertTrue(value instanceof JsonString); JsonString string = (JsonString) value; - assertEquals("aString", string.getString()); + Assertions.assertEquals("aString", string.getString()); Assertions.assertEquals(Event.VALUE_NULL, parser.next()); value = parser.getValue(); @@ -545,7 +545,6 @@ private void testGetValueStructure(JsonParser parser) { Assertions.assertEquals(Event.START_ARRAY, parser.next()); JsonArray array = parser.getArray(); Assertions.assertTrue(array.isEmpty()); - Assertions.assertEquals(Event.END_ARRAY, parser.next()); Assertions.assertEquals(Event.START_OBJECT, parser.next()); @@ -595,11 +594,55 @@ private void testGetValueStructure(JsonParser parser) { JsonObject object = parser.getObject(); Assertions.assertTrue(object.isEmpty()); Assertions.assertEquals(Event.END_OBJECT, parser.next()); - Assertions.assertEquals(Event.END_OBJECT, parser.next()); Assertions.assertEquals(Event.END_ARRAY, parser.next()); Assertions.assertFalse(parser.hasNext()); } + @Test + void testGetArrayRoot() { + try (JsonParser parser = Json.createParserFactory(null).createParser( + Json.createArrayBuilder() + .add(1) + .add(2) + .add(3) + .build())) { + testGetArrayRoot(parser); + } + } + + private void testGetArrayRoot(JsonParser parser) { + Assertions.assertEquals(Event.START_ARRAY, parser.next()); + JsonArray actual = parser.getArray(); + JsonArray expected = Json.createArrayBuilder() + .add(1) + .add(2) + .add(3) + .build(); + Assertions.assertEquals(expected, actual); + Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent()); + Assertions.assertFalse(parser.hasNext()); + } + + @Test + void testGetObjectRoot() { + try (JsonParser parser = Json.createParserFactory(null).createParser( + Json.createObjectBuilder() + .add("key1", "value1") + .build())) { + testGetObjectRoot(parser); + } + } + + private void testGetObjectRoot(JsonParser parser) { + Assertions.assertEquals(Event.START_OBJECT, parser.next()); + JsonObject actual = parser.getObject(); + JsonObject expected = Json.createObjectBuilder() + .add("key1", "value1") + .build(); + Assertions.assertEquals(expected, actual); + Assertions.assertEquals(Event.END_OBJECT, parser.currentEvent()); + Assertions.assertFalse(parser.hasNext()); + } @Test void testNestedArrayReader() {