Skip to content

Commit

Permalink
Implement JsonStructureParser.currentEvent() (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
marschall and jbescos authored Nov 12, 2024
1 parent 47b371b commit f3bb2d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public boolean hasNext() {
return !((state == Event.END_OBJECT || state == Event.END_ARRAY) && scopeStack.isEmpty());
}

@Override
public Event currentEvent() {
return state;
}

@Override
public Event next() {
if (!hasNext()) {
Expand All @@ -121,6 +126,7 @@ private void transition() {
state = Event.END_ARRAY;
}
} else {

// ObjectScope
if (state == Event.KEY_NAME) {
nextStateAndEndOfTheObjectOrArray();
Expand Down
14 changes: 14 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 @@ -180,10 +180,12 @@ static void testEmptyArrayIterator(JsonParser parser) {
Assertions.assertTrue(parser.hasNext());
Assertions.assertTrue(parser.hasNext());
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());

Assertions.assertTrue(parser.hasNext());
Assertions.assertTrue(parser.hasNext());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());

Assertions.assertFalse(parser.hasNext());
Assertions.assertFalse(parser.hasNext());
Expand Down Expand Up @@ -212,7 +214,9 @@ void testEmptyArrayIterator2Structure() {

static void testEmptyArrayIterator2(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
try {
parser.next();
Assertions.fail("Should have thrown a NoSuchElementException");
Expand All @@ -237,7 +241,9 @@ void testEmptyArrayIterator3Structure() {

static void testEmptyArrayIterator3(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
Assertions.assertFalse(parser.hasNext());
try {
parser.next();
Expand Down Expand Up @@ -521,13 +527,21 @@ void testNestedArrayStructure() {

static void testNestedArray(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());
Assertions.assertFalse(parser.hasNext());
Assertions.assertFalse(parser.hasNext());
}
Expand Down

0 comments on commit f3bb2d7

Please sign in to comment.