JuliaIO/JLD2.jl

Cannot read custom struct with Dict subfield just written to jld file.

PhyX-Meow opened this issue · 2 comments

Julia version: v1.10
JLD2.jl version: v0.4.44

julia> using JLD2

julia> struct foo
           a::Int
           b::Dict{Int,Int}
       end

julia> x = foo(1,Dict(2=>3))
foo(1, Dict(2 => 3))

julia> jldsave("testData.jld2")

julia> data = jldopen("testData.jld2", "r+")
JLDFile /home/wuli/Dice.jl/testData.jld2 (read/write)
  (no datasets)

julia> data["foo"] = x
foo(1, Dict(2 => 3))

julia> data["foo"]
ERROR: MethodError: Cannot `convert` an object of type Dict{Int64, Int64} to an object of type JLD2.SerializedDict

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  JLD2.SerializedDict(::Any)
   @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/specialcased_types.jl:258

Stacktrace:
  [1] rconvert(T::Type, x::Dict{Int64, Int64})
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/custom_serialization.jl:9
  [2] jlconvert(::JLD2.ReadRepresentation{…}, f::JLD2.JLDFile{…}, ptr::Ptr{…}, ::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/writing_datatypes.jl:315
  [3] jlconvert(::JLD2.ReadRepresentation{…}, f::JLD2.JLDFile{…}, ptr::Ptr{…}, header_offset::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/custom_serialization.jl:66
  [4] macro expansion
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/reconstructing_datatypes.jl:768 [inlined]
  [5] jlconvert(::JLD2.ReadRepresentation{…}, f::JLD2.JLDFile{…}, ptr::Ptr{…}, header_offset::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/data/reconstructing_datatypes.jl:687
  [6] read_scalar(f::JLD2.JLDFile{JLD2.MmapIO}, rr::Any, header_offset::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/dataio.jl:37
  [7] read_data(f::JLD2.JLDFile{…}, rr::Any, read_dataspace::Tuple{…}, attributes::Vector{…})
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/datasets.jl:238
  [8] read_data(f::JLD2.JLDFile{…}, dataspace::JLD2.ReadDataspace, datatype_class::UInt8, datatype_offset::Int64, layout::JLD2.DataLayout, filters::JLD2.FilterPipeline, header_offset::JLD2.RelOffset, attributes::Vector{…})
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/datasets.jl:194
  [9] load_dataset(f::JLD2.JLDFile{JLD2.MmapIO}, offset::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/datasets.jl:125
 [10] getindex(g::JLD2.Group{JLD2.JLDFile{JLD2.MmapIO}}, name::String)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/groups.jl:109
 [11] getindex(f::JLD2.JLDFile{JLD2.MmapIO}, name::String)
    @ JLD2 ~/.julia/packages/JLD2/oYEEg/src/JLD2.jl:494
 [12] top-level scope
    @ REPL[7]:1
Some type information was truncated. Use `show(err)` to see complete types.

Thank you for this report.
It should be fixed by #537

Thank you for the quick fix!