diff --git a/src/JLD2.jl b/src/JLD2.jl index b0a19a27..b76b1780 100644 --- a/src/JLD2.jl +++ b/src/JLD2.jl @@ -105,6 +105,7 @@ mutable struct JLDFile{T<:IO} path::String writable::Bool written::Bool + plain::Bool compress#::Union{Bool,Symbol} mmaparrays::Bool n_times_opened::Int @@ -125,11 +126,13 @@ mutable struct JLDFile{T<:IO} root_group::Group{JLDFile{T}} types_group::Group{JLDFile{T}} base_address::UInt64 + function JLDFile{T}(io::IO, path::AbstractString, writable::Bool, written::Bool, + plain::Bool, compress,#::Union{Bool,Symbol}, mmaparrays::Bool) where T - f = new(io, path, writable, written, compress, mmaparrays, 1, false, + f = new(io, path, writable, written, plain, compress, mmaparrays, 1, false, OrderedDict{RelOffset,CommittedDatatype}(), H5Datatype[], JLDWriteSession(), Dict{String,Any}(), IdDict(), IdDict(), Dict{RelOffset,WeakRef}(), DATA_START, Dict{RelOffset,GlobalHeap}(), @@ -138,8 +141,8 @@ mutable struct JLDFile{T<:IO} f end end -JLDFile(io::IO, path::AbstractString, writable::Bool, written::Bool, compress, mmaparrays::Bool) = - JLDFile{typeof(io)}(io, path, writable, written, compress, mmaparrays) +JLDFile(io::IO, path::AbstractString, writable::Bool, written::Bool, plain::Bool, compress, mmaparrays::Bool) = + JLDFile{typeof(io)}(io, path, writable, written, plain, compress, mmaparrays) """ fileoffset(f::JLDFile, x::RelOffset) @@ -189,6 +192,7 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool, mmaparrays::Bool=false, typemap::Dict{String}=Dict{String,Any}(), parallel_read::Bool=false, + plain::Bool=false ) where T<:Union{Type{IOStream},Type{MmapIO}} mmaparrays && @warn "mmaparrays keyword is currently ignored" maxlog=1 verify_compressor(compress) @@ -240,7 +244,7 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool, io = openfile(iotype, fname, wr, create, truncate, fallback) created = !exists || truncate rname = realpath(fname) - f = JLDFile(io, rname, wr, created, compress, mmaparrays) + f = JLDFile(io, rname, wr, created, plain, compress, mmaparrays) if !parallel_read OPEN_FILES[rname] = WeakRef(f) diff --git a/src/data/reconstructing_datatypes.jl b/src/data/reconstructing_datatypes.jl index 3eb75960..ca40bc7c 100644 --- a/src/data/reconstructing_datatypes.jl +++ b/src/data/reconstructing_datatypes.jl @@ -98,6 +98,11 @@ function jltype(f::JLDFile, cdt::CommittedDatatype) end datatype = read_attr_data(f, julia_type_attr) + if f.plain && !(datatype isa Upgrade) && !(datatype <: Tuple) + rr = jltype(f, dt) + return f.h5jltype[cdt] = rr + end + if written_type_attr !== nothing # Custom serialization custom_datatype = read_attr_data(f, written_type_attr) @@ -415,6 +420,9 @@ function jlconvert(rr::ReadRepresentation{T,DataTypeODR()}, isunknowntype(m) && return m unknown_params && return UnknownType{m, Tuple{params...}} if hasparams + if f.plain && !(m === Tuple) + return Any + end try m = m{params...} catch e diff --git a/test/test_files.jl b/test/test_files.jl index e4185ebd..dd0802e7 100644 --- a/test/test_files.jl +++ b/test/test_files.jl @@ -257,4 +257,16 @@ end @test getfoo("readas_foo_n_sin.jld2") isa Readas.FooN{typeof(sin)} @test getfoo("readas_foo_a.jld2") isa Readas.Foo{Readas.UndefinedFunction} @test getfoo("readas_foo_n_a.jld2") isa Readas.FooNSerialization +end + + +@testset "plain reconstruction" begin + fn = joinpath(testfiles,"struct_reconstruction.jld2") + data = load(fn; plain=true) + # This is somewhat broken: Tuples are committed with field names "1", "2",... + # these are valid names but break most of the common API incl. the @NamedTuple macro + #@test data["tms"] == @NamedTuple{1::Int64, 2}((1, (a = 1,))) + @test getproperty(data["tms"], Symbol(1)) == 1 + @test data["s"] == (a = 1,) + @test data["ds"].kvvec[1] == (; first = "a", second = (a = 1,)) end \ No newline at end of file