Repository navigation
Do not drop empty or duplicate keys in fractured_json table rows - #2908
Merged
Merged
Conversation
When fractured_json lays out an array of objects as a table, it can lose
keys. With default options:
fractured_json_string(R"([[{"":1}]])") // [ [ {1} ] ]
fractured_json_string(R"([[{"a":1,"a":2}]])") // [ [ {"a": 1} ] ]
The first output is not valid JSON. The table code used an empty
table_column::key to mean "array row", so a column for the valid key ""
was written without its key and colon. Mark object columns with an
explicit has_key flag instead.
The second output silently loses "a":2. A table column holds one value
per row, so later duplicates of a key had nowhere to go. Duplicate names
are valid JSON (RFC 8259 only says they SHOULD be unique), and the DOM,
minify() and prettify() keep them. When a row has a duplicate key, do
not lay out the array as a table or as aligned compact rows, so the
regular formatter writes every member. Duplicates are detected by
recording the last row that filled each column, which adds no hashing
or per-row allocation.
Add regression tests for both cases.
Member
|
Running tests. |
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fractured_jsonloses object keys when it lays out an array of objectsas a table. With default options:
The first output is not valid JSON. The second silently drops
"a":2.Both reproduce on v5.0.0 to v5.0.2 and on master.
Changes
table_columnused an emptykeyto mean "array row",so a column for the valid key
""was written without its key andcolon. Add an explicit
has_keyflag and use it in the three placesthat tested
!key.empty().duplicates of a key were dropped. Duplicate names are valid JSON
(RFC 8259 only says they SHOULD be unique), and the DOM,
minify()andprettify()keep them.build_table_columns()now returns no columnswhen a row has a duplicate key, so the array is not laid out as a table
(or as aligned compact rows) and the regular formatter writes every
member. Duplicates are detected by recording the last row that filled
each column, so there is no extra hashing or per-row allocation.
table_format_empty_key_testandtable_format_duplicate_key_test(including an escaped duplicate,
"a"). Each checks the exactoutput and that re-parsing it gives the same content as the input.
Arrays without empty or duplicate keys are formatted exactly as before.
Verification
fractured_json_tests: PASS with clang 22.1.8 (ASan + UBSan,-Wall -Wextra -Werror) and GCC 15.2.0 (-Wall -Wextra -Wpedantic -Werror).SIMDJSON_SANITIZE=ONandSIMDJSON_DEVELOPER_MODE=ON(clang 22.1.8, Linux x86_64): 148/148 PASS.git diff --check: PASSfractured_json()on 200k rows such as{"id":1,"name":"n","x":1.5,"ok":true,"tags":[1,2]}(clang -O3, onepinned core, best of 7) takes 215-232 ms on both master and this branch.
Type of change
AI Assistance
I found these bugs with a libFuzzer harness that formats each parsed
document with random
fractured_json_options, re-parses the output andcompares it with the input. Claude (an AI assistant) helped write the
harness, analyse the failures, draft the fix and tests, and review the
patch. I reviewed the final change and verified it with the steps above.
After the fix, the harness ran about 5.5M inputs without a failure.