Skip to content

Avoiding type piracy for custom serialization of type that I do not own #708

@rasmushenningsson

Description

@rasmushenningsson

If we want custom serialization for a type that I do not own, I currently need to wrap it in my own type to avoid type piracy for JLD2.writeas:

struct MyWrappedType
    x::TypeIDontOwn
end

JLD2.writeas(::Type{MyWrappedType}) = ...

But this only works to a certain degree, if I want to serialize a Vector/Tuple/Pair/etc. that contains TypeIDontOwn, it becomes much harder.

One solution would be if it was possible to supply a "context" object to e.g. jldopen, that was passed along to the relevant functions. Then we could write

JLD2.writeas(::MyContext, ::Type{TypeIDontOwn} = ...`

thus avoiding piracy. (This strategy is used by e.g. StableHashTraits.jl.)
It would naturally fallback to calling without a context, so this is not a breaking change.

Is there any other way to achieve this currently?

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