Skip to content

Commit

Permalink
JsonStructureParser fixes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
marschall committed Dec 14, 2024
1 parent 34177db commit c850737
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
24 changes: 22 additions & 2 deletions impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -288,6 +306,7 @@ private static class ArrayScope extends Scope {
private JsonValue value;

ArrayScope(JsonArray array) {
super(array);
this.it = array.iterator();
}

Expand Down Expand Up @@ -320,6 +339,7 @@ private static class ObjectScope extends Scope {
private String key;

ObjectScope(JsonObject object) {
super(object);
this.it = object.entrySet().iterator();
}

Expand Down
53 changes: 48 additions & 5 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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());

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c850737

Please sign in to comment.