JuliaIO / JLD2.jl

HDF5-compatible file format in pure Julia
Other
560 stars 92 forks source link

`Dict` saved in julia 1.7 can't be opened in julia 1.6 #380

Closed yha closed 2 years ago

yha commented 2 years ago

julia 1.7:

using JLD2
x = Dict(1 => 2)
jldsave("x.tmp.jld2"; x)

julia 1.6:

julia> using JLD2

julia> JLD2.load_object("x.tmp.jld2")
┌ Warning: type Core.Pair{Int64,Int64} does not exist in workspace; reconstructing
└ @ JLD2 C:\Users\sternlab\.julia\packages\JLD2\b0tI9\src\data\reconstructing_datatypes.jl:456
ERROR: MethodError: Cannot `convert` an object of type Vector{JLD2.ReconstructedTypes.var"##Core.Pair{Int64,Int64}#272"} to an object of type Dict{Int64, Int64}
Closest candidates are:
  convert(::Type{T}, ::T) where T<:AbstractDict at abstractdict.jl:520
  convert(::Type{T}, ::AbstractDict) where T<:AbstractDict at abstractdict.jl:522
  convert(::Type{T}, ::T) where T at essentials.jl:205
  ...

I'm not sure if this is "officially" supposed to work. But if it isn't, maybe there should be a warning when loading a jld2 file saved by a different julia version, and some mention in the docs about saving and loading across julia versions?

JonasIsensee commented 2 years ago

Hi @yha,

the definition of Pair was moved from Base to Core in v1.7. You can tell JLD2 about this explicitly:

# julia v1.6 loading the file created with v1.7
julia> load("x.tmp.jld2", "x"; typemap=Dict("Core.Pair" => Base.Pair))
Dict{Int64, Int64} with 1 entry:
  1 => 2