Skip to content

to_string_pretty: valid JSON for tuple variants - #487

Merged
Licenser merged 1 commit into
simd-lite:mainfrom
yuxi-liu-wired:pr/pretty-tuple-variant
Oct 3, 2026
Merged

Licenser merged 1 commit into
simd-lite:mainfrom
yuxi-liu-wired:pr/pretty-tuple-variant

Conversation

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor

fix: to_string_pretty wrote invalid JSON for tuple variants

SerializeTupleVariant::end for the pretty serializer closed the array
of fields with } instead of ] and never closed the object
{"Variant": ...} that serialize_tuple_variant opened:

enum E { C(i64, String), Z() }
to_string_pretty(&E::C(1, "s".into()))   {"C": [1, "s" }   (one line per item)
to_string_pretty(&E::Z())                {"Z": []

Neither is JSON; the compact serializer and serde_json write
{"C": [1, "s"]}. end now closes the array (when it has fields; an
empty one was already written as []) and then the object, with the
same indentation as serde_json.

Test: pretty_print_tuple_variant (fails before this change), which
also compares a nested case with serde_json::to_string_pretty.

Testing

Each commit adds a regression test that fails before the change. The full test suite passes and
cargo fmt --check is clean on the changed files.

Found with a differential fuzzer comparing serde_json, simd-json, sonic-rs and jiter.

@Licenser Licenser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@Licenser
Licenser enabled auto-merge (squash) October 3, 2026 14:28
`SerializeTupleVariant::end` for the pretty serializer closed the array
of fields with `}` instead of `]` and never closed the object
`{"Variant": ...}` that `serialize_tuple_variant` opened:

    enum E { C(i64, String), Z() }
    to_string_pretty(&E::C(1, "s".into()))   {"C": [1, "s" }   (one line per item)
    to_string_pretty(&E::Z())                {"Z": []

Neither is JSON; the compact serializer and serde_json write
`{"C": [1, "s"]}`. `end` now closes the array (when it has fields; an
empty one was already written as `[]`) and then the object, with the
same indentation as serde_json.

Test: `pretty_print_tuple_variant` (fails before this change), which
also compares a nested case with serde_json::to_string_pretty.

Found by a differential fuzzer (serde_json, simd-json, sonic-rs, jiter)
extended to typed serialization.
@Licenser
Licenser force-pushed the pr/pretty-tuple-variant branch from 280ee02 to 2c94b92 Compare October 3, 2026 15:53
@Licenser
Licenser merged commit af18817 into simd-lite:main Oct 3, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants