JuliaIO / BSON.jl

Other
157 stars 39 forks source link

FileIO `Stream` Interface not supported #64

Open goretkin opened 4 years ago

goretkin commented 4 years ago
julia> import FileIO

julia> FileIO.save(FileIO.Stream(FileIO.format"BSON", IOBuffer()), Dict(:hey=>3))
Error encountered while saving nothing.

Fatal error:
ERROR: MethodError: no method matching bson(::Nothing, ::Dict{Symbol,Int64})
Closest candidates are:
  bson(::IO, ::AbstractDict) at /Users/goretkin/.julia/dev/BSON/src/write.jl:81
  bson(::String, ::AbstractDict) at /Users/goretkin/.julia/dev/BSON/src/write.jl:83
Stacktrace:
 [1] handle_error(::MethodError, ::Stream{DataFormat{:BSON},Base.GenericIOBuffer{Array{UInt8,1}}}) at /Users/goretkin/.julia/dev/FileIO/src/error_handling.jl:82
 [2] handle_exceptions(::Array{Any,1}, ::String) at /Users/goretkin/.julia/dev/FileIO/src/error_handling.jl:77
 [3] #save#30(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(save), ::Formatted, ::Any) at /Users/goretkin/.julia/dev/FileIO/src/loadsave.jl:211
 [4] save(::Formatted, ::Any) at /Users/goretkin/.julia/dev/FileIO/src/loadsave.jl:194
 [5] top-level scope at none:0
dehann commented 3 years ago

Not sure if this helps but if you looking to avoid a physical file:

d = Dict(:hi =>3)
io_ = IOBuffer()
BSON.@save io_ d

bd = take!(io)

EDIT:

io__ = PipeBuffer(take!(io_))

BSON.@load io__ d2

For completeness also see String safe Base64: https://docs.julialang.org/en/v1/stdlib/Base64/#Base64.Base64EncodePipe

Search keywords: BSON with IOBuffer no file


I'm was having trouble going the other way:

BSON.@load io_ d
ERROR: EOFError: read end of file
Stacktrace:
 [1] peek at ./iobuffer.jl:180 [inlined]
 [2] read at ./iobuffer.jl:190 [inlined]
 [3] parse_doc(::Base.GenericIOBuffer{Array{UInt8,1}}) at /home/dehann/.julia/packages/BSON/XAts7/src/read.jl:48
 [4] parse at /home/dehann/.julia/packages/BSON/XAts7/src/read.jl:101 [inlined]
 [5] load(::Base.GenericIOBuffer{Array{UInt8,1}}) at /home/dehann/.julia/packages/BSON/XAts7/src/read.jl:104
 [6] top-level scope at /home/dehann/.julia/packages/BSON/XAts7/src/BSON.jl:52