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

std: add json parsing option to skip unknown fields #5852

Draft
wants to merge 1 commit into
base: master
from

Conversation

@daurnimator
Copy link
Collaborator

daurnimator commented Jul 12, 2020

Requested by @Sobeston.

I had to have the user pass in a buffer via ParseOptions to use for tracking the ignored state

@daurnimator daurnimator force-pushed the daurnimator:json-skip-values branch from 5e87466 to 404bbd3 Jul 12, 2020
@zigazeljko
Copy link
Contributor

zigazeljko commented Jul 12, 2020

Why is this unknown_field_buffer necessary? For skipping over fields in properly nested json you only need a single integer for tracking the current nesting level. And the StreamingParser already checks that the json is properly nested (via the stack field).

@daurnimator
Copy link
Collaborator Author

daurnimator commented Jul 12, 2020

Why is this unknown_field_buffer necessary? For skipping over fields in properly nested json you only need a single integer for tracking the current nesting level.

You need to verify that the json is well formed as you ignore things: you need to track if you are expecting a brace vs square brackets, and in which order e.g. ]]]]}]]}}}}}]]] vs }]]}]}}]]]}]]]]

And the StreamingParser already checks that the json is properly nested (via the stack field).

ah ha! I didn't know about this field.

Bit-stack for nested object/map literals (max 255 nestings).

That's a limitation I didn't know about either; seems to be undocumented outside of the code.

@daurnimator
Copy link
Collaborator Author

daurnimator commented Jul 13, 2020

And the StreamingParser already checks that the json is properly nested (via the stack field).

ah ha! I didn't know about this field.

Trying to rewrite this PR, the detection for this in StreamingParser doesn't seem to work correctly?

test "mismatched close" {
    var p = TokenStream.init("[102, 111, 111}");
    checkNext(&p, .ArrayBegin);
    checkNext(&p, .Number);
    checkNext(&p, .Number);
    checkNext(&p, .Number);
    testing.expectError(error.UnexpectedClosingBracket, p.next());
}

I get:

.expected error.UnexpectedClosingBracket, found Token{ .ObjectEnd = void }
@zigazeljko
Copy link
Contributor

zigazeljko commented Jul 13, 2020

The detection for this is implemented here:

zig/lib/std/json.zig

Lines 372 to 374 in 75a7205

if (p.stack & 1 != object_bit) {
return error.UnexpectedClosingBracket;
}

zig/lib/std/json.zig

Lines 398 to 400 in 75a7205

if (p.stack & 1 != array_bit) {
return error.UnexpectedClosingBrace;
}

Closing brackets are handled in both the ValueBegin and ValueEnd states, but this check is missing from the latter. Copying those two checks to ValueEnd state should fix it.

@daurnimator
Copy link
Collaborator Author

daurnimator commented Aug 17, 2020

converted to draft while I refactor to try to do #5959 at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.