From da7ff765adb4a885f9b91b2a74132d2063553fee Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 18 Nov 2026 00:01:45 -0700 Subject: [PATCH 1/4] Add failing test for writing multidimensional arrays YAML.write prints a matrix with Julia's string syntax, so loading the output gives a one-element list holding a string. The test asserts that matrices and higher-dimensional arrays load back as nested lists. Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 939adab..de769b9 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -615,6 +615,13 @@ end @test e.problem_mark.line == 3 end +@testset "issue #135" begin + @test YAML.load(YAML.yaml(Dict("m" => [1 2; 3 4]))) == Dict("m" => [[1, 3], [2, 4]]) + @test YAML.load(YAML.yaml([[1 2; 3 4]])) == [[[1, 3], [2, 4]]] + @test YAML.load(YAML.yaml(reshape(1:8, 2, 2, 2))) == [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] + @test YAML.load(YAML.yaml(Dict("m" => zeros(0, 3)))) == Dict("m" => []) +end + # int resolver: a leading-zero scalar that isn't valid octal must stay a string, # not fall into construct_yaml_int's base-8 parse and throw @testset "int resolver leading zero" begin From fee57d77036b7e2107add29d2cff459d50b083a1 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 18 Nov 2026 00:02:27 -0700 Subject: [PATCH 2/4] Write multidimensional arrays as nested sequences Arrays with two or more dimensions had no writer method, so they fell through to the generic fallback, which prints Julia's string syntax such as `[1 2; 3 4]`. That loads back as a list holding one string. Write such an array as a sequence of its slices along the last dimension, so a matrix becomes a sequence of its columns. This matches how JSON.jl writes arrays, and `reduce(hcat, ...)` rebuilds the matrix. The mapping and sequence writers now treat any nonempty array as a block collection. Fixes #135 Co-Authored-By: Claude Opus 5.5 --- src/writer.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/writer.jl b/src/writer.jl index 5127134..a81ec59 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -20,6 +20,9 @@ end Write a YAML representation of some data (e.g. a dictionary or an array) to an IO stream, if provided, or return a string of that representation. + +A multidimensional array is written as nested sequences along its last dimension, so a +matrix becomes a sequence of its columns. """ function write(io::IO, data::Any, prefix::AbstractString="") print(io, prefix) # print the prefix, e.g. a comment at the beginning of the file @@ -53,7 +56,7 @@ function _print(io::IO, dict::AbstractDict, level::Int=0, ignore_level::Bool=fal end # Julia collections written as YAML sequences -const Sequence = Union{AbstractVector,Tuple,AbstractSet} +const Sequence = Union{AbstractArray,Tuple,AbstractSet} _print(io::IO, x::Union{Tuple,AbstractSet}, level::Int=0, ignore_level::Bool=false) = _print(io, collect(x), level, ignore_level) @@ -88,6 +91,15 @@ function _print(io::IO, arr::AbstractVector, level::Int=0, ignore_level::Bool=fa end end +# YAML has no multidimensional sequences. Write an array as a sequence of its slices along the +# last dimension, so a matrix becomes a sequence of its columns and `reduce(hcat, ...)` on the +# loaded value rebuilds it. +function _print(io::IO, arr::AbstractArray, level::Int=0, ignore_level::Bool=false) + n = ndims(arr) + slices = isempty(arr) ? [] : [selectdim(arr, n, i) for i in axes(arr, n)] + _print(io, slices, level, ignore_level) +end + # print a single key-value pair function _print(io::IO, pair::Pair, level::Int=0, ignore_level::Bool=false) k = pair[1] isa Union{Symbol,AbstractChar} ? string(pair[1]) : pair[1] # text keys are strings From 3758e1666b050549c0acb5bc6e35dea861b2a07b Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sat, 21 Nov 2026 20:30:45 -0700 Subject: [PATCH 3/4] Write a zero-dimensional array as the value it holds A zero-dimensional array has no last dimension to slice along, so the multidimensional writer threw a BoundsError for values such as fill(1). Write the one value it holds instead, as JSON.jl does, and decide inline-or-block layout from that value. Co-Authored-By: Claude Opus 5.5 --- src/writer.jl | 7 ++++++- test/runtests.jl | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/writer.jl b/src/writer.jl index a81ec59..1cfc94a 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -73,6 +73,7 @@ _print(io::IO, z::Complex, level::Int=0, ignore_level::Bool=false) = # a nonempty collection key or value starts on the line after its '?' or key starts_block(x) = x isa Union{Mapping,Sequence} && !isempty(x) +starts_block(x::AbstractArray{<:Any,0}) = starts_block(x[]) # recursively print an array function _print(io::IO, arr::AbstractVector, level::Int=0, ignore_level::Bool=false) @@ -80,7 +81,7 @@ function _print(io::IO, arr::AbstractVector, level::Int=0, ignore_level::Bool=fa println(io, "[]") else for elem in arr - if elem isa Sequence && !isempty(elem) # nonempty sequences of sequences must be handled differently + if elem isa Sequence && starts_block(elem) # nonempty sequences of sequences must be handled differently print(io, _indent("-\n", level)) _print(io, elem, level + 1) else @@ -100,6 +101,10 @@ function _print(io::IO, arr::AbstractArray, level::Int=0, ignore_level::Bool=fal _print(io, slices, level, ignore_level) end +# a zero-dimensional array holds one value, so it is written as that value, as JSON.jl does +_print(io::IO, arr::AbstractArray{<:Any,0}, level::Int=0, ignore_level::Bool=false) = + _print(io, arr[], level, ignore_level) + # print a single key-value pair function _print(io::IO, pair::Pair, level::Int=0, ignore_level::Bool=false) k = pair[1] isa Union{Symbol,AbstractChar} ? string(pair[1]) : pair[1] # text keys are strings diff --git a/test/runtests.jl b/test/runtests.jl index de769b9..2db8945 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -620,6 +620,10 @@ end @test YAML.load(YAML.yaml([[1 2; 3 4]])) == [[[1, 3], [2, 4]]] @test YAML.load(YAML.yaml(reshape(1:8, 2, 2, 2))) == [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] @test YAML.load(YAML.yaml(Dict("m" => zeros(0, 3)))) == Dict("m" => []) + # a zero-dimensional array is written as the one value it holds + @test YAML.load(YAML.yaml(fill(1))) == 1 + @test YAML.load(YAML.yaml(Dict("z" => fill(2), "v" => fill([1, 2])))) == Dict("z" => 2, "v" => [1, 2]) + @test YAML.load(YAML.yaml([fill(3), fill(Dict("a" => 4)), fill([5, 6])])) == [3, Dict("a" => 4), [5, 6]] end # int resolver: a leading-zero scalar that isn't valid octal must stay a string, From 64586b211e70ebfc3990b5912a92563a9fdab0c7 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sun, 22 Nov 2026 20:49:59 -0700 Subject: [PATCH 4/4] Normalize zero-dimensional array keys before writing YAML Unwrap scalar array keys before deciding how to quote them. Cover multiline and quoted text, symbols, and matrix keys in the round-trip tests. --- src/writer.jl | 6 +++++- test/runtests.jl | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/writer.jl b/src/writer.jl index 1cfc94a..cc2a7db 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -107,7 +107,11 @@ _print(io::IO, arr::AbstractArray{<:Any,0}, level::Int=0, ignore_level::Bool=fal # print a single key-value pair function _print(io::IO, pair::Pair, level::Int=0, ignore_level::Bool=false) - k = pair[1] isa Union{Symbol,AbstractChar} ? string(pair[1]) : pair[1] # text keys are strings + k = pair[1] + while k isa AbstractArray{<:Any,0} + k = k[] + end + k = k isa Union{Symbol,AbstractChar} ? string(k) : k # text keys are strings if starts_block(k) # a collection key is written as an explicit key: '?', the key's lines, then ':' println(io, _indent("?", level, ignore_level)) diff --git a/test/runtests.jl b/test/runtests.jl index 2db8945..d136647 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -624,6 +624,10 @@ end @test YAML.load(YAML.yaml(fill(1))) == 1 @test YAML.load(YAML.yaml(Dict("z" => fill(2), "v" => fill([1, 2])))) == Dict("z" => 2, "v" => [1, 2]) @test YAML.load(YAML.yaml([fill(3), fill(Dict("a" => 4)), fill([5, 6])])) == [3, Dict("a" => 4), [5, 6]] + @test YAML.load(YAML.yaml(Dict(fill("a\nb") => 1))) == Dict("a\nb" => 1) + @test YAML.load(YAML.yaml(Dict(fill(fill("a\"b")) => 2))) == Dict("a\"b" => 2) + @test YAML.load(YAML.yaml(Dict(fill(Symbol("a: b")) => 3))) == Dict("a: b" => 3) + @test YAML.load(YAML.yaml(Dict(fill([1 2; 3 4]) => 4))) == Dict([[1, 3], [2, 4]] => 4) end # int resolver: a leading-zero scalar that isn't valid octal must stay a string,