Skip to content

Commit

Permalink
Implement JsonStructureParser#getValue methods (#140)
Browse files Browse the repository at this point in the history
Implement the following methods in JsonStructureParser

- getValue
- getArray
- getObject
  • Loading branch information
marschall authored Nov 21, 2024
1 parent 7c8c244 commit 9d67960
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
38 changes: 38 additions & 0 deletions impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ public BigDecimal getBigDecimal() {
return getNumberValue(JsonNumber::bigDecimalValue, JsonMessages::PARSER_GETBIGDECIMAL_ERR);
}

@Override
public JsonValue getValue() {
switch (state) {
case START_ARRAY:
case START_OBJECT:
case VALUE_STRING:
case VALUE_NUMBER:
case VALUE_TRUE:
case VALUE_FALSE:
case VALUE_NULL:
return current.getJsonValue();
case KEY_NAME:
return new JsonStringImpl(((ObjectScope)current).key);
case END_ARRAY:
case END_OBJECT:
default:
throw new IllegalStateException(JsonMessages.PARSER_GETVALUE_ERR(state));
}
}

@Override
public JsonArray getArray() {
if (state != Event.START_ARRAY) {
throw new IllegalStateException(
JsonMessages.PARSER_GETARRAY_ERR(state));
}
return (JsonArray) scopeStack.peek().getJsonValue();
}

@Override
public JsonObject getObject() {
if (state != Event.START_OBJECT) {
throw new IllegalStateException(
JsonMessages.PARSER_GETOBJECT_ERR(state));
}
return (JsonObject) scopeStack.peek().getJsonValue();
}

@Override
public JsonLocation getLocation() {
return JsonLocationImpl.UNKNOWN;
Expand Down
119 changes: 119 additions & 0 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.StringReader;

import jakarta.json.*;
import jakarta.json.JsonValue.ValueType;
import jakarta.json.stream.JsonLocation;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParser.Event;
Expand Down Expand Up @@ -506,6 +507,124 @@ static void testObjectStringValue(JsonParser parser, String name, String value)
Assertions.assertEquals(value, parser.getString());
}

@Test
void testGetValueStructure() {
try (JsonParser parser = Json.createParserFactory(null).createParser(
Json.createArrayBuilder()
.add(1)
.add(new BigDecimal("1.1"))
.add(true)
.add(false)
.add("aString")
.addNull()
.add(Json.createArrayBuilder().build())
.add(Json.createObjectBuilder()
.add("key1", 2)
.add("key2", new BigDecimal("2.2"))
.add("key3", true)
.add("key4", false)
.add("key5", "bString")
.addNull("key6")
.add("key7", Json.createObjectBuilder().build())
.build())
.build())) {
testGetValueStructure(parser);
}
}

private void testGetValueStructure(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());

Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
JsonValue value = parser.getValue();
Assertions.assertTrue(value instanceof JsonNumber);
JsonNumber number = (JsonNumber) value;
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(Event.VALUE_TRUE, parser.next());
value = parser.getValue();
Assertions.assertSame(ValueType.TRUE, value.getValueType());

Assertions.assertEquals(Event.VALUE_FALSE, parser.next());
value = parser.getValue();
Assertions.assertSame(ValueType.FALSE, value.getValueType());

Assertions.assertEquals(Event.VALUE_STRING, parser.next());
value = parser.getValue();
Assertions.assertTrue(value instanceof JsonString);
JsonString string = (JsonString) value;
assertEquals("aString", string.getString());

Assertions.assertEquals(Event.VALUE_NULL, parser.next());
value = parser.getValue();

Assertions.assertSame(ValueType.NULL, value.getValueType());

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());

Assertions.assertEquals(Event.KEY_NAME, parser.next());
value = parser.getValue();
Assertions.assertTrue(value instanceof JsonString);
string = (JsonString) value;
Assertions.assertEquals("key1", string.getString());

Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
value = parser.getValue();
Assertions.assertTrue(value instanceof JsonNumber);
number = (JsonNumber) value;
Assertions.assertEquals(number.longValueExact(), 2L);

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
value = parser.getValue();
Assertions.assertTrue(value instanceof JsonNumber);
number = (JsonNumber) value;
Assertions.assertEquals(number.bigDecimalValue(), new BigDecimal("2.2"));

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.VALUE_TRUE, parser.next());
value = parser.getValue();
Assertions.assertSame(ValueType.TRUE, value.getValueType());

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.VALUE_FALSE, parser.next());
value = parser.getValue();
Assertions.assertSame(ValueType.FALSE, value.getValueType());

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.VALUE_STRING, parser.next());
value = parser.getValue();
Assertions.assertTrue(value instanceof JsonString);
string = (JsonString) value;
Assertions.assertEquals("bString", string.getString());

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.VALUE_NULL, parser.next());
value = parser.getValue();
Assertions.assertSame(ValueType.NULL, value.getValueType());

Assertions.assertEquals(Event.KEY_NAME, parser.next());
Assertions.assertEquals(Event.START_OBJECT, parser.next());
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 testNestedArrayReader() {
try (JsonParser parser = Json.createParser(new StringReader("[[],[[]]]"))) {
Expand Down

0 comments on commit 9d67960

Please sign in to comment.