diff --git a/src/extensions.jl b/src/extensions.jl index 8a7bc7e..8acfd88 100644 --- a/src/extensions.jl +++ b/src/extensions.jl @@ -108,8 +108,9 @@ tags[:array] = d -> # Structs +struct Undef end structdata(x) = isprimitivetype(typeof(x)) ? reinterpret_(UInt8, [x]) : - Any[getfield(x,f) for f in fieldnames(typeof(x)) if isdefined(x, f)] + Any[isdefined(x, f) ? getfield(x,f) : Undef() for f in fieldnames(typeof(x))] function lower(x) BSONDict(:tag => "struct", :type => typeof(x), :data => structdata(x)) @@ -119,6 +120,7 @@ initstruct(T) = ccall(:jl_new_struct_uninit, Any, (Any,), T) function newstruct!(x, fs...) for (i, f) = enumerate(fs) + isa(f, Undef) && continue f = convert(fieldtype(typeof(x),i), f) ccall(:jl_set_nth_field, Nothing, (Any, Csize_t, Any), x, i-1, f) end