You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Serialization
function writedata(filename::AbstractString, itr)
open(filename, "w") do io
s = Serializer(io)
Serialization.writeheader(s)
for x in itr
serialize(s, x)
end
end
end
function readdata(filename::AbstractString)
open(filename, "r") do io
while !eof(io)
@show deserialize(io)
end
end
end
This code using native Int produces the expected output:
filename = "test.jls"
writedata(filename, [i for i = 1:3])
readdata(filename)
But attempting this with a custom struct, errors on the second deserialize.
struct Foo
a::Int
end
writedata(filename, [Foo(i) for i = 1:3])
readdata(filename)
deserialize(io) = Foo(1)
ERROR: KeyError: key 0 not found
Stacktrace:
[1] getindex at .\abstractdict.jl:599 [inlined]
[2] handle_deserialize(::Serializer{IOStream}, ::Int32) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Serialization\src\Serialization.jl:764
[3] deserialize(::Serializer{IOStream}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Serialization\src\Serialization.jl:731
[4] handle_deserialize(::Serializer{IOStream}, ::Int32) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Serialization\src\Serialization.jl:778
[5] deserialize(::IOStream) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Serialization\src\Serialization.jl:731
[6] macro expansion at .\show.jl:555 [inlined]
[7] (::getfield(Main, Symbol("##7#8")))(::IOStream) at .\REPL[3]:4
[8] #open#310(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::getfield(Main, Symbol("##7#8")), ::String, ::Vararg{String,N} where N) at .\iostream.jl:369
[9] open at .\iostream.jl:367 [inlined]
[10] readdata(::String) at .\REPL[3]:2
[11] top-level scope at none:0
The text was updated successfully, but these errors were encountered:
The values were written with the same Serializer context, so there can be shared references within the stream. They need to be deserialized using the same context object as well.
Is this expected to work?
This code using native
Int
produces the expected output:But attempting this with a custom
struct
, errors on the seconddeserialize
.The text was updated successfully, but these errors were encountered: