Skip to content

Fallback serialization for any AbstractArray subtype that is convertable from Array? #647

@lassepe

Description

@lassepe

There are some special array types, such as CUDA.CuArray which currently silently fail when writing to disk (e.g., in the case of CuArray, it writes the array meta-data and device pointer to disk which is no longer valid when de-serializing).

One way to fix this is to add extensions to those packages to customize the serialization logic. For example, I opened such a PR here: JuliaGPU/CUDA.jl#2789

However, I am opening this issue to discuss if it may make sense to provide a more general fallback directly in JLD2. Here is what I had in mind:

function JLD2.writeas(t::Type{<:AbstractArray{T, N}}) where {T, N}
   if hasmethod(convert, Tuple{Type{t}, Array{T, N}})
       Array{T,N}
   else
       t
   end
end

The reason for checking for convertability here rather than doing this for all AbstractArray is that some abstract arrays (such as those from ComponentArrays.jl) may have additional meta-data that does not allow them to be constructed from AbstractArray alone.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions