Skip to content

Error deserializing multiple values of custom struct #31337

Closed
@GregPlowman

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions