What is the up-to-date, idiomatic method of deserializing a stream of un-padded binary values from an external source into Julia struct?
StrPack.jl carries a big warning that it is not actively maintained.
What is the up-to-date, idiomatic method of deserializing a stream of un-padded binary values from an external source into Julia struct?
StrPack.jl carries a big warning that it is not actively maintained.
So far the best solution that I found looks like this:
function Base.read(io::IO, strc_t::Union{Type{S1},
Type{S2},
Type{S3}})
fn = fieldnames(strc_t)
vals = Vector{Any}(undef, length(fn))
for (n, f) in enumerate(fn)
vals[n] = read(io, fieldtype(strc_t, f))
end
return strc_t(vals...)
end
function Base.write(io::IO, strc::Union{S1, S2, S3})
sum([write(io, getfield(strc, f))
for f in fieldnames(typeof(strc))])
end
That should work for scenario where types are like this
struct S1
a::Int8
b::UInt16
...
end
struct S2
c::Float64
d::Int
...
end
struct S3
x::S1
y::S2
end
Criticisms are welcome.
(By the way, I was surprised that there is no default write method for tuples, why is that?)