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

Error deserializing multiple values of custom struct #31337

Closed
GregPlowman opened this issue Mar 13, 2019 · 3 comments
Closed

Error deserializing multiple values of custom struct #31337

GregPlowman opened this issue Mar 13, 2019 · 3 comments

Comments

@GregPlowman
Copy link
Contributor

Is this expected to work?

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)
deserialize(io) = 1
deserialize(io) = 2
deserialize(io) = 3

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
@JeffBezanson
Copy link
Member

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.

@GregPlowman
Copy link
Contributor Author

GregPlowman commented Mar 14, 2019

Right. Thank you.

So readdata should be something like:

function readdata(filename::AbstractString)
   open(filename, "r") do io
        s = Serializer(io)
        while !eof(io)
            @show deserialize(s)
        end
    end
end

@StefanKarpinski
Copy link
Member

Perhaps the error message could be improved?

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

No branches or pull requests

3 participants