From 6760bd47d23fdd2a6d239891651ad46dba336f21 Mon Sep 17 00:00:00 2001 From: JonasIsensee Date: Fri, 19 Jan 2024 11:37:38 +0100 Subject: [PATCH] add method for mutable custom serialization (#537) --- src/data/custom_serialization.jl | 7 +++++++ test/loadsave.jl | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/data/custom_serialization.jl b/src/data/custom_serialization.jl index a15f432b..3100dd76 100644 --- a/src/data/custom_serialization.jl +++ b/src/data/custom_serialization.jl @@ -52,6 +52,13 @@ jlconvert_canbeuninitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}) jlconvert_isinitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}, ptr::Ptr) where {T,S,ODR} = jlconvert_isinitialized(ReadRepresentation{S,ODR}(), ptr) +function jlconvert(::ReadRepresentation{T,CustomSerialization{S,RelOffset}}, + f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S} + # Concerns objects whose custom serialization is itself only referenced by a RelOffset + # This be important when the original object is mutable + offset = jlunsafe_load(pconvert(Ptr{RelOffset}, ptr)) + return rconvert(T, load_dataset(f, offset)) +end function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}}, f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S,ODR} diff --git a/test/loadsave.jl b/test/loadsave.jl index 19ed4792..0ca9204b 100644 --- a/test/loadsave.jl +++ b/test/loadsave.jl @@ -739,4 +739,14 @@ end jldsave("testopempty.jld2"; op) @test op == load("testopempty.jld2", "op") end +end + +@testset "Issue #536 reading directly after writing" begin + cd(mktempdir()) do + x = (1, Dict(2=>3)) + jldopen("test.jld2", "w") do f + f["x"] = x + @test x == f["x"] + end + end end \ No newline at end of file