Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle undefined values in arrays #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/BSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ end

function applychildren!(f, x::BSONArray)
for i = 1:length(x)
isassigned(x, i) || continue
x[i] = f(x[i])
end
return x
Expand Down
12 changes: 10 additions & 2 deletions src/extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ tags[:unionall] = d -> UnionAll(d[:var], d[:body])
lower(x::Vector{Any}) = copy(x)
lower(x::Vector{UInt8}) = x

function collect_any(xs)
ys = Vector{Any}(length(xs))
for i = 1:length(xs)
isassigned(xs, i) && (ys[i] = xs[i])
end
return ys
end

function lower(x::Array)
ndims(x) == 1 && !isbits(eltype(x)) && return Any[x...]
ndims(x) == 1 && !isbits(eltype(x)) && return collect_any(x)
BSONDict(:tag => "array", :type => eltype(x), :size => Any[size(x)...],
:data => isbits(eltype(x)) ? reinterpret(UInt8, reshape(x, :)) : Any[x...])
:data => isbits(eltype(x)) ? reinterpret(UInt8, reshape(x, :)) : collect_any(x))
end

tags[:array] = d ->
Expand Down
3 changes: 2 additions & 1 deletion src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ end

bson_primitive(io::IO, doc::BSONDict) = bson_doc(io, doc)
bson_primitive(io::IO, x::BSONArray) =
bson_doc(io, [Base.string(i-1) => v for (i, v) in enumerate(x)])
bson_doc(io, [Base.string(i-1) => isassigned(x, i) ? x[i] : nothing
for i = 1:length(x)])

# Lowering

Expand Down