JuliaIO / BSON.jl

Other
158 stars 39 forks source link

save and load unexpectedly modify types #84

Open BoZenKhaa opened 3 years ago

BoZenKhaa commented 3 years ago

I have played with the updated BSON after https://github.com/JuliaIO/BSON.jl/pull/29 and I still seem to get unexpected behavior:

  1. Changed type of nested dictionaries on reload
    
    julia> inner_d = Dict(:v=>0)
    Dict{Symbol,Int64} with 1 entry:
    :v => 0

julia> outer_d = Dict(:d=>inner_d) Dict{Symbol,Dict{Symbol,Int64}} with 1 entry: :d => Dict(:v=>0)

julia> save("test.bson", outer_d)

julia> load("test.bson") BSON.BSONDict with 1 entry: :d => BSON.BSONDict(:v=>0)


According to README, i would expect both the inner and outer dictionaries to be of the `Dict` type when loaded. 

2. **Lost array type on reload**
```julia
julia> v = [zeros(1) for i in 1:2]
2-element Array{Array{Float64,1},1}:
 [0.0]
 [0.0]

julia> D = Dict(:d=>v)
Dict{Symbol,Array{Array{Float64,1},1}} with 1 entry:
  :d => [[0.0], [0.0]]

julia> save("test_v.bson", D)

julia> load("test_v.bson")
BSON.BSONDict with 1 entry:
  :d => Any[[0.0], [0.0]]

Here, I would expect the loaded dictionary to be Dict, not BSONDict and the array elements to be of type Array{Float64,1}, not Any.

Quickly looking through the issues, such effects of save and load could imo be the cause of some of the reports (https://github.com/JuliaIO/BSON.jl/issues/76, https://github.com/JuliaIO/BSON.jl/issues/75).

I think it would be worthwhile to mention this behaviour in the README to save people headaches with unexpected errors.

PS: I was having similar issues even before the merge of https://github.com/JuliaIO/BSON.jl/pull/29, the change in https://github.com/JuliaIO/BSON.jl/pull/29 at least made these issues much easier to track.

BoZenKhaa commented 3 years ago

I made a PR to add warning to the README: https://github.com/JuliaIO/BSON.jl/pull/85