Skip to content

Deserialize struct variants from a sequence in Value - #488

Merged
Licenser merged 3 commits into
simd-lite:mainfrom
yuxi-liu-wired:pr/struct-variant-seq
Oct 4, 2026
Merged

Licenser merged 3 commits into
simd-lite:mainfrom
yuxi-liu-wired:pr/struct-variant-seq

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #484: the first commit here is the Value commit of #484 (its Remaining helper is used by this fix). Review the last commit; once #484 is merged this PR reduces to it.

fix: reject extra array elements when deserializing from a Value

The Value deserializers (from_owned_value, from_borrowed_value and
the &Value variants) handed arrays to the visitor without checking
afterwards that every element was consumed, so a fixed-length type
silently dropped the rest:

let v = to_owned_value(b"[1,2,3]")?;
from_owned_value::<(u8, u8)>(v)          Ok((1, 2))
from_refowned_value::<[u8; 2]>(&v)       Ok([1, 2])

The text deserializer rejects these (fix/seq-extra-elements), as do
serde_json's from_value and sonic-rs. Arrays now go through
visit_array, which returns invalid_length when elements are left,
like serde's SeqDeserializer::end.

Test: value_fixed_length_sequences_reject_extra_elements (fails before
this change).

fix: deserialize struct variants from a sequence in Value

A struct variant written as a JSON array deserializes from text, but
from an owned or borrowed Value (from_owned_value etc.) it failed with
Unexpected(Some(Object), Some(Array)):

from_slice::<E>(br#"{"S":[5,"k"]}"#)                 Ok(S { a: 5, .. })
from_owned_value::<E>(to_owned_value(..)?)           Err(Unexpected(..))

struct_variant now hands an array to the visitor through
visit_array, which also rejects wrong lengths. serde_json (serde-rs/json#1049) and
sonic-rs had the same gap.

Builds on fix/value-seq-extra-elements (uses its visit_array).

Test: value_struct_variant_from_sequence (fails before this change).

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.

just some minor test addition

Comment thread src/tests/serde.rs
claude added 3 commits October 4, 2026 12:39
The Value deserializers (`from_owned_value`, `from_borrowed_value` and
the `&Value` variants) handed arrays to the visitor without checking
afterwards that every element was consumed, so a fixed-length type
silently dropped the rest:

    let v = to_owned_value(b"[1,2,3]")?;
    from_owned_value::<(u8, u8)>(v)          Ok((1, 2))
    from_refowned_value::<[u8; 2]>(&v)       Ok([1, 2])

The text deserializer rejects these (`fix/seq-extra-elements`), as do
serde_json's `from_value` and sonic-rs. Arrays now go through
`visit_array`, which returns `invalid_length` when elements are left,
like serde's `SeqDeserializer::end`.

Test: `value_fixed_length_sequences_reject_extra_elements` (fails before
this change).

Found by a differential fuzzer (serde_json, simd-json, sonic-rs, jiter).
A struct variant written as a JSON array deserializes from text, but
from an owned or borrowed Value (`from_owned_value` etc.) it failed with
`Unexpected(Some(Object), Some(Array))`:

    from_slice::<E>(br#"{"S":[5,"k"]}"#)                 Ok(S { a: 5, .. })
    from_owned_value::<E>(to_owned_value(..)?)           Err(Unexpected(..))

`struct_variant` now hands an array to the visitor through
`visit_array`, which also rejects wrong lengths. serde_json (#1049) and
sonic-rs had the same gap.

Builds on `fix/value-seq-extra-elements` (uses its `visit_array`).

Test: `value_struct_variant_from_sequence` (fails before this change).

Found by a differential fuzzer (serde_json, simd-json, sonic-rs, jiter).
@Licenser
Licenser force-pushed the pr/struct-variant-seq branch from 6a0c348 to cf629f7 Compare October 4, 2026 10:39
@Licenser
Licenser enabled auto-merge (squash) October 4, 2026 10:39
@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.23%. Comparing base (c7d4fba) to head (cf629f7).
⚠️ Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #488      +/-   ##
==========================================
+ Coverage   72.00%   75.23%   +3.23%     
==========================================
  Files          49       49              
  Lines       11347    11613     +266     
==========================================
+ Hits         8170     8737     +567     
+ Misses       3177     2876     -301     
Flag Coverage Δ
unittests 75.23% <100.00%> (+3.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Licenser
Licenser merged commit 68b8380 into simd-lite:main Oct 4, 2026
31 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