JuliaIO / BSON.jl

Other
158 stars 39 forks source link

T in Dict{Symbol, T} is not preserved #89

Open CarloLucibello opened 3 years ago

CarloLucibello commented 3 years ago

The value type of Dict with symbols keys is not preserved (it is broadened to Any)

julia> d = Dict(:a=>1, :b=>2)
Dict{Symbol, Int64} with 2 entries:
  :a => 1
  :b => 2

julia> bson("test.bson", d = d)

julia> BSON.load("test.bson")
Dict{Symbol, Any} with 1 entry:
  :d => Dict{Symbol, Any}(:a=>1, :b=>2)
s-baumann commented 2 years ago

Maybe related to this, I see something similar with dataframes.

using DataFrames, BSON
temp_path = ENV["TEMP"]
aa = DataFrame(Dict(:A => [1,2,3], :B => [:a,:b,:c]))
typeof(aa.B) # Vector{Symbol} (alias for Array{Symbol, 1})

# Now saving it
fpath = joinpath(temp_path, "test.bson")
dic = Dict()
dic[:dframe] = aa
BSON.bson(fpath, dic)

# Now loading it one of the columns has its type changed.
dic2 = BSON.load(fpath)
bb = dic2[:dframe]
typeof(bb.B) # Vector{Any} (alias for Array{Any, 1})