fix: Support decoding N-dimensional arrays - #1
Merged
Merged
Conversation
N-dimensional arrays are a draft 3 construct that this library rejected as invalid, so valid draft 3 data could not be read. `[$type#[Nx Ny ...]` now decodes to nested lists, keeping the innermost axis as the typed list. The nesting is built from views rather than copies, so an N-dimensional array is still one contiguous allocation once decoded. Dimension arrays are accepted in both optimized and non-optimized form. The column-major form, `[$type#[[Nx Ny ...]]` as MATLAB and FORTRAN write it, is reordered into row-major order so that it reads the same way as an array of the same shape written row-major. Elements are permuted whole, whatever their width, so this works for every strong type. Encoding N-dimensional arrays is not supported, so a decoded array is written back as nested arrays; the values are unchanged. Also throws a FormatException rather than a RangeError when a string or buffer runs past the end of the input, which a payload shorter than its dimensions makes easy to hit. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Owner
Author
|
Base of a two-PR stack:
Merge this one first; #2 retargets to 🤖 Generated with Claude Code |
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.
Fixes a decoding gap: N-dimensional arrays are a draft 3 construct that this library
rejected as invalid, so valid draft 3 data could not be read at all.
What it does
[$type#[Nx Ny ...]decodes to nested lists, with the innermost axis kept as the typedlist, so the spec's 2×3×4 example comes back as a
List<List<Uint8List>>matching theJSON in the specification verbatim.
contiguous allocation once decoded — a test pins the slices at offsets 0 and 20 into a
single 24-byte buffer.
[$type#[[Nx Ny ...]]is how MATLAB andFORTRAN write it, and given JSONLab shares an author with the spec it is likely common
in real data. Elements are permuted whole, at whatever width, so it reads identically to
a row-major array of the same shape. Verified against the specification's own
column-major byte sequence.
counted by one is rejected.
Also throws a
FormatExceptionrather than aRangeErrorwhen a string or buffer runspast the end of the input, which a payload shorter than its dimensions makes easy to hit.
Not included
Encoding N-dimensional arrays. A decoded array is written back as nested arrays — the
values are unchanged but the format is not. That is added in the follow-up PR, which also
makes the round-trip byte-exact.
Verification
Both worked examples from the specification decode byte-for-byte. 203 tests on the VM and
183 on Chrome, plus
dart format,dart analyze --fatal-infosand the examples.🤖 Generated with Claude Code