Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize skip methods of JsonStructureParser #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
46 changes: 2 additions & 44 deletions impl/src/main/java/org/eclipse/parsson/JsonStructureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,56 +158,14 @@ public void close() {
@Override
public void skipObject() {
if (current instanceof ObjectScope) {
int depth = 1;
do {
if (state == Event.KEY_NAME) {
state = getState(current.getJsonValue());
switch (state) {
case START_OBJECT:
depth++;
break;
case END_OBJECT:
depth--;
break;
default:
//no-op
}
} else {
if (current.hasNext()) {
current.next();
state = Event.KEY_NAME;
} else {
state = Event.END_OBJECT;
depth--;
}
}
} while (state != Event.END_OBJECT && depth > 0);
state = Event.END_OBJECT;
}
}

@Override
public void skipArray() {
if (current instanceof ArrayScope) {
int depth = 1;
do {
if (current.hasNext()) {
current.next();
state = getState(current.getJsonValue());
switch (state) {
case START_ARRAY:
depth++;
break;
case END_ARRAY:
depth--;
break;
default:
//no-op
}
} else {
state = Event.END_ARRAY;
depth--;
}
} while (!(state == Event.END_ARRAY && depth == 0));
state = Event.END_ARRAY;
}
}

Expand Down
45 changes: 45 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 @@ -886,4 +886,49 @@ private void checkExceptionFromNext(String input) {
}
Assertions.fail();
}

@Test
void testSkipStructure() {
try (JsonParser parser = Json.createParserFactory(null).createParser(
Json.createArrayBuilder()
.add(1)
.add(Json.createArrayBuilder()
.add(2)
.build())
.add(3)
.add(Json.createObjectBuilder()
.add("key", 4)
.build())
.add(5)
.add(6)
.build())) {
testSkipStructure(parser);
}
}

static void testSkipStructure(JsonParser parser) {
Assertions.assertEquals(Event.START_ARRAY, parser.next());
Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
Assertions.assertEquals(1, parser.getInt());

Assertions.assertEquals(Event.START_ARRAY, parser.next());
parser.skipArray();
Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent());

Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
Assertions.assertEquals(3, parser.getInt());

Assertions.assertEquals(Event.START_OBJECT, parser.next());
parser.skipObject();
Assertions.assertEquals(Event.END_OBJECT, parser.currentEvent());

Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
Assertions.assertEquals(5, parser.getInt());

Assertions.assertEquals(Event.VALUE_NUMBER, parser.next());
Assertions.assertEquals(6, parser.getInt());

Assertions.assertEquals(Event.END_ARRAY, parser.next());
Assertions.assertFalse(parser.hasNext());
}
}
Loading