如何在Julia中为结构实现自定义序列化/反序列化?

问题描述 投票:1回答:1

Base.serializeBase.deserialize的默认实现为整个给定对象执行序列化/反序列化。

排除字段序列化并仍能正确反序列化的正确方法是什么?

这是一个简化的代码示例:

# The target struct
struct Foo
    x::Int
    y::Union{Int, Void} #we do not want to serialize this field
end

foo1 = Foo(1,2)

# Serialization
write_iob = IOBuffer()
serialize(write_iob, foo1)
seekstart(write_iob)
content = read(write_iob)

# Deserialization
read_iob = IOBuffer(content)
foo2 = deserialize(read_iob)

@show foo1
@show foo2

上面代码的输出是:

foo1 = Foo(1, 2)
foo2 = Foo(1, 2)

并且期望的结果应该是:

foo1 = Foo(1, 2)
foo2 = Foo(1, nothing)

在这里,我假设我们可以为缺失的字段定义一个默认值,例如nothingyin上面的输出。

serialization deserialization julia
1个回答
0
投票

在我当前版本的Julia(0.6.2)中深入研究序列化/反序列化的实现之后,我找到了一个解决方案。以下是问题中示例的解决方案:

# Custom Serialization of a Foo instance
function Base.Serializer.serialize(s::AbstractSerializer, instance::Foo)
    Base.Serializer.writetag(s.io, Base.Serializer.OBJECT_TAG)
    Base.Serializer.serialize(s, Foo)
    Base.Serializer.serialize(s, instance.x)
end

# Custom Deserialization of a Foo instance
function Base.Serializer.deserialize(s::AbstractSerializer, ::Type{Foo})
    x = Base.Serializer.deserialize(s)
    Foo(x,nothing)
end

现在,如果再次运行测试代码:

# The target struct
struct Foo
    x::Int
    y::Union{Int, Void} #we do not want to serialize this field
end

foo1 = Foo(1,2)

# Serialization
write_iob = IOBuffer()
serialize(write_iob, foo1)
seekstart(write_iob)
content = read(write_iob)

# Deserialization
read_iob = IOBuffer(content)
foo2 = deserialize(read_iob)

@show foo1
@show foo2

测试代码输出:

foo1 = Foo(1, 2)
foo2 = Foo(1, nothing)

我应该提一下,上面的解决方案取决于序列化/反序列化的当前实现(在Julia 0.6.2中),并且不能保证其未来的稳定性。因此,我仍然会留意寻找更好的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.