Skip to content

Commit

Permalink
started namedtuple
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydellavecchia committed Nov 11, 2024
1 parent 7255064 commit 02b2191
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 52 deletions.
59 changes: 12 additions & 47 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,24 @@ function save_object(s::SerializerState, x::Vector)
end
end

# this should eventually become deprecated
function load_object(s::DeserializerState, ::Type{<: Vector}, params::Type)
function load_object(s::DeserializerState, ::Type{<: Vector{T}}) where T
load_node(s) do v
if serialize_with_id(params)
loaded_v::Vector{params} = load_array_node(s) do _
if serialize_with_id(T)
loaded_v::Vector{T} = load_array_node(s) do _
load_ref(s)
end
else
loaded_v = params[]
loaded_v = T[]
for (i, entry) in enumerate(v)
push!(loaded_v, load_object(s, params, i))
push!(loaded_v, load_object(s, T, i))
end
end
return loaded_v
end
end

function load_object(s::DeserializerState, ::Type{<: Vector{params}}) where params
load_node(s) do v
if serialize_with_id(params)
loaded_v::Vector{params} = load_array_node(s) do _
load_ref(s)
end
else
loaded_v = params[]
for (i, entry) in enumerate(v)
push!(loaded_v, load_object(s, params, i))
end
end
return loaded_v
end
function load_object(s::DeserializerState, U::Type{<: Vector{T}}, ::Nothing) where T
load_object(s, U)
end

function load_object(s::DeserializerState, ::Type{<: Vector{T}}, R::Ring) where T
Expand Down Expand Up @@ -131,17 +118,10 @@ function save_object(s::SerializerState, obj::Tuple)
end
end

function load_object(s::DeserializerState, ::Type{<:Tuple}, params::Vector)
function load_object(s::DeserializerState, T::Type{<:Tuple}, params::Vector)
entries = load_array_node(s) do (i, entry)
if params[i] isa Type
if serialize_with_id(params[i])
return load_ref(s)
else
return load_object(s, params[i])
end
else
return load_object(s, params[i][1], params[i][2])
end
S = fieldtype(T, i)
load_object(s, S, params[i])
end
return Tuple(entries)
end
Expand All @@ -150,23 +130,8 @@ end
# Saving and loading NamedTuple
@register_serialization_type NamedTuple uses_params

function save_type_params(s::SerializerState, obj::T) where T <: NamedTuple
save_data_dict(s) do
save_object(s, encode_type(NamedTuple), :name)
save_data_dict(s, :params) do
save_data_array(s, :tuple_params) do
for (i, value) in enumerate(values(obj))
U = fieldtype(T, i)
if serialize_with_params(U)
save_type_params(s, value)
else
save_object(s, encode_type(U))
end
end
end
save_object(s, keys(obj), :names)
end
end
function type_params(obj::T) where T <: NamedTuple
return Dict(k => (typeof(v), type_params(v)) for (k, v) in pairs(obj))
end

function save_object(s::SerializerState, obj::NamedTuple)
Expand Down
7 changes: 4 additions & 3 deletions src/Serialization/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function save_type_params(s::SerializerState, obj::T) where T
save_data_dict(s, :params) do
if params isa Dict
for (k, v) in params
save_typed_object(s, v, k)
save_type_params(s, v, k)
end
elseif params isa Tuple
# obj has a vector of parameters
Expand All @@ -214,7 +214,7 @@ function save_type_params(s::SerializerState, obj::T) where T
save_object(s, encode_type(entry_type), :name)
save_typed_object(s, entry_params, :params)
else
save_object(s, encode_type(entry_type))
save_object(s, encode_type(entry_type), type_key)
end
else
save_typed_object(s, params)
Expand Down Expand Up @@ -245,7 +245,7 @@ function load_type_params(s::DeserializerState)
end
return collect(zip(tuple_params...))
else
load_type_params(s)
return load_type_params(s)
end
end
if T <: MatVecType
Expand All @@ -256,6 +256,7 @@ function load_type_params(s::DeserializerState)
return T, params
end
else
!haskey(s, :data) && !Base.issingletontype(T) && return T, nothing
return T, load_typed_object(s)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
end
end

@test_skip @testset "Testing (de)serialization of Vector{$(T)}" for T in
@testset "Testing (de)serialization of Vector{$(T)}" for T in
(
UInt, UInt128, UInt16, UInt32, UInt64, UInt8,
Int, Int128, Int16, Int32, Int64, Int8,
Expand All @@ -105,7 +105,7 @@
end
end

@test_skip @testset "(de)serialization NamedTuple{$(S), $(T)}" for (S, T) in
@testset "(de)serialization NamedTuple{$(S), $(T)}" for (S, T) in
(
(UInt, UInt128), (UInt16, UInt32), (UInt64, UInt8),
(Int, Int128), (Int16, Int32), (Int64, Int8),
Expand Down

0 comments on commit 02b2191

Please sign in to comment.