JuliaData/YAML.jl

2d arrays get converted into string representation by write_file

Opened this issue · 0 comments

For 2d arrays, the YAML.write_file() function writes a string version of the julia data which is parsed wrongly when read back by the load_file function.

Specs

Package version:
YAML v0.4.9

Julia and machine:

julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e909 (2023-07-05 09:39 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M2 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores

To reproduce:

using YAML
a = rand(10, 10)
d = Dict([("2darray", a)])
println("Writing to file:")
println(typeof(d["2darray"]), " of size ", size(d["2darray"]))
YAML.write_file("2darray.yaml", d)

println("Reading from file:")
d_read = YAML.load_file("2darray.yaml")
println(d_read["2darray"])
println(typeof(d_read["2darray"]), " of length ", length(d_read["2darray"]))

Suggested workaround

using YAML
using JSON
a = rand(3, 3)
b = rand(3)
d = Dict([("2darray", JSON.sprint(a)), ("1darray", b)])
println("Writing to file:")
println("2d array:")
println(d["2darray"])
# println(typeof(d["2darray"]), " of size ", size(d["2darray"]))
YAML.write_file("2darray_json.yaml", d)
println("Reading from file:")
d_read = YAML.load_file("2darray_json.yaml")
for (key, val) in d_read
    if isa(val, String)
        if startswith(val, "[[")
            d_read[key] = mapreduce(permutedims, vcat, JSON.parse(val))
        end
    end
end
println("2d array:")
println(d_read["2darray"])
println(typeof(d_read["2darray"]), " of size ", size(d_read["2darray"]))
println("1d array:")
println(d_read["1darray"])
println(typeof(d_read["1darray"]), " of length ", length(d_read["1darray"]))