Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 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 @@ -91,7 +92,9 @@ public BigDecimal getBigDecimal() {
public JsonValue getValue() {
switch (state) {
case START_ARRAY:
return getArray();
case START_OBJECT:
return getObject();
case VALUE_STRING:
case VALUE_NUMBER:
case VALUE_TRUE:
Expand All @@ -113,7 +116,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 +128,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 +280,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 +308,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 +341,7 @@ private static class ObjectScope extends Scope {
private String key;

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

Expand Down
103 changes: 98 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,105 @@ 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 testGetArrayRootByValue() {
try (JsonParser parser = Json.createParserFactory(null).createParser(
Json.createArrayBuilder()
.add(1)
.add(2)
.add(3)
.build())) {
testGetArrayRootByValue(parser);
}
}

private void testGetArrayRootByValue(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());
JsonValue value = parser.getValue();
Assertions.assertEquals(ValueType.ARRAY, value.getValueType());
JsonArray actual = (JsonArray) value;
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 testGetObjectRootByValue() {
try (JsonParser parser = Json.createParserFactory(null).createParser(
Json.createObjectBuilder()
.add("key1", "value1")
.build())) {
testGetObjectRootByValue(parser);
}
}

private void testGetObjectRootByValue(JsonParser parser) {
Assertions.assertEquals(Event.START_OBJECT, parser.next());
JsonValue value = parser.getValue();
Assertions.assertEquals(ValueType.OBJECT, value.getValueType());
JsonObject actual = (JsonObject) value;
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
Loading