Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving with bson modifies dict argument #26

Closed
GregPlowman opened this issue Feb 2, 2019 · 7 comments · Fixed by #29
Closed

saving with bson modifies dict argument #26

GregPlowman opened this issue Feb 2, 2019 · 7 comments · Fixed by #29

Comments

@GregPlowman
Copy link

GregPlowman commented Feb 2, 2019

Writing with bson(file, dict) seems to modify the dict argument.
Is this the intended behaviour?

Julia-1.1.0> using BSON

Julia-1.1.0> d = Dict(:a => [1+2im, 3+4im], :b => "Hello, World!")
Dict{Symbol,Any} with 2 entries:
  :a => Complex{Int64}[1+2im, 3+4im]
  :b => "Hello, World!"

Julia-1.1.0> d2 = copy(d)
Dict{Symbol,Any} with 2 entries:
  :a => Complex{Int64}[1+2im, 3+4im]
  :b => "Hello, World!"

Julia-1.1.0> bson("test.bson", d)

Julia-1.1.0> d
Dict{Symbol,Any} with 2 entries:
  :a => Dict{Symbol,Any}(:tag=>"array",:type=>Dict{Symbol,Any}(:tag=>"datatype",:params=>Any[Dict{Symbol,Any}(:tag=>"datatype",:params=>Any[],:name=>Any["Main", "Core", "Int64"])],:name=>Any["Main", …
  :b => "Hello, World!"

Julia-1.1.0> d3 = BSON.load("test.bson")
Dict{Symbol,Any} with 2 entries:
  :a => Complex{Int64}[1+2im, 3+4im]
  :b => "Hello, World!"

Julia-1.1.0> d3 == d
false

Julia-1.1.0> d3 == d2
true
@Datseris
Copy link

Hi, this seems to be a big problem for me as I later want to use the dictionaries I save using BSON. Is there a way to solve this? SOme dictionaries I save have significantly large data, so copy may not be very nice solution always.

@mauro3
Copy link
Contributor

mauro3 commented Mar 5, 2019

Note that this also happens to dicts nested within other data:

julia> using BSON

julia> d = [Dict(:a => [1+2im, 3+4im], :b => "Hello, World!")]
1-element Array{Dict{Symbol,Any},1}:
 Dict(:a=>Complex{Int64}[1+2im, 3+4im],:b=>"Hello, World!")

julia> bson("/tmp/test.bson", Dict(:d=>d))

julia> d[1]
Dict{Symbol,Any} with 2 entries:
  :a => Dict{Symbol,Any}(:tag=>"array",:type=>Dict{Symbol,Any}(:tag=>"datatype",:params=>Any[Dict{Symbol,Any}(:tag=>"datatype",:params=>Any[],:name=>Any["Main", "Core",…
  :b => "Hello, World!"

And also note that this is also present in v0.14, the last Julia 0.6 version.

@mauro3
Copy link
Contributor

mauro3 commented Mar 5, 2019

A workaround:

julia> d = Dict{Any,Any}(:a => [1+2im, 3+4im], :b => "Hello, World!")
Dict{Any,Any} with 2 entries:
  :a => Complex{Int64}[1+2im, 3+4im]
  :b => "Hello, World!"

julia> bson("/tmp/test.bson", d)

julia> d
Dict{Any,Any} with 2 entries:
  :a => Complex{Int64}[1+2im, 3+4im]
  :b => "Hello, World!"

@Datseris
Copy link

Datseris commented Mar 5, 2019

So the workaround is to define the dictionary as {Any, Any} ?

@mauro3
Copy link
Contributor

mauro3 commented Mar 5, 2019

Yes

mauro3 added a commit to mauro3/BSON.jl that referenced this issue Mar 5, 2019
@mauro3 mauro3 mentioned this issue Mar 5, 2019
@mauro3
Copy link
Contributor

mauro3 commented Mar 5, 2019

In #29 is a start to fix this. But I cannot spend more time on it. Maybe one of you can?

@racinmat
Copy link
Contributor

racinmat commented Jun 12, 2020

The workaround does not work for me :(
I have some deep structure in sch, and all of these are mutating the sch

BSON.bson("sth.bson", Dict{Any, Any}(:a=>sch))
BSON.bson("sth.bson", Dict{Any, Any}("a"=>sch))
BSON.bson("sth.bson", Dict("a"=>sch))

Btw, I think tt should be somewhere more clearly stated that the serialization function modifies the argument, it caused me some nasty bugs, looking forward to the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants