Closed dpinol closed 8 months ago
Hi, this should be possible.
Note that NamedTuple is a UnionAll and not a DataType. This is probably the problem. If writeas constructs the proper NamedTuple type, it should work.
Still, you shouldn't (need to) do this... If things go right, this conversion method would not change the stored data (only the type metadata).
There are some constraints:
JLD2.writeas
needs to return a DataType
JLD2.wconvert
needs to return an instance of the exact same DataType
and signature needs to be able to match it
Here's a version that "works":
Base.@kwdef mutable struct T1
f
end
const FS = NamedTuple
JLD2.writeas(::Type{T1}) = NamedTuple{(fieldnames(T1)...,), Tuple{fieldtypes(T1)...}}
function JLD2.wconvert(::Type{<:FS}, t::T1) @info "saving $(typeof(t))"
Any
field# and the (f=t.f) constructor will always check for instance types
return NamedTuple{(fieldnames(T1)...,), Tuple{fieldtypes(T1)...}}(t.f)
end
function JLD2.rconvert(::Type{T1}, fs::FS) @info "loading $T1" return T1(; fs...) end
Should custom serialization be able to serialize though NamedTuple's?
I get this error
thanks