Add object to existing BSON file?

Suppose I saved a BSON file with BSON.@save "path/to/file.bson" obj1 obj2 . Then later on I create a new obj3 and I want to also add to this file, as if I had executed BSON.@save "path/to/file.bson" obj1 obj2 obj3 in the first place (but I can’t do that). At this point I no longer have obj1,obj2 in memory. What’s the best way of adding obj3 to the bson file? Do I have to BSON.@load "path/to/file.bson" obj1 obj2 first to recover obj1,obj2 and then BSON.@save "path/to/file.bson" obj1 obj2 obj3 ? Or is there a more direct way?

BSON only reads the first serialized object, and this syntax saves a dict with keys "a" and "b"

a=5
b=6
c=7
 BSON.@save "path/to/file.bson" a b

You can append c with

open("path/to/file.bson", "a") do io
  BSON.@save io c
end

But then you need to issue two reads to get everything back

julia> open("test.bson", "r") do io
            println(BSON.load(io))
            println(BSON.load(io))
       end
Dict{Symbol,Any}(:a => 5,:b => 6)
Dict{Symbol,Any}(:c => 7)

Not very convenient, but maybe useful?

1 Like