Skip to content

Commit

Permalink
Add Tuple deserialization (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: Elizaveta Gryumova <[email protected]>
Co-authored-by: Stanislav Gryumov <[email protected]>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent f8d97b6 commit 1e15159
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Serde"
uuid = "db9b398d-9517-45f8-9a95-92af99003e0e"
version = "3.2.0"
version = "3.3.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
12 changes: 12 additions & 0 deletions src/De/Deser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ function deser(
return map(x -> deser(eltype(T), x), data)
end

function deser(
::ArrayType,
::Type{T},
x::D,
)::T where {T<:Tuple,D<:Union{Tuple,AbstractVector}}
if (T === Tuple) || isa(T, UnionAll)
T(x)
else
T(deser(t, v) for (t, v) in zip(fieldtypes(T), x))
end
end

"""
Serde.custom_name(::Type{T}, ::Val{x}) -> x
Expand Down
41 changes: 41 additions & 0 deletions test/deser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,45 @@ using Test, Dates
@test Serde.deser(WithMissing, Dict("y" => 3, "x" => nothing)) == WithMissing(3, missing)
@test Serde.deser(WithMissing, Dict("y" => 4, "x" => missing)) == WithMissing(4, missing)
end

@testset "Case №39: Deserialization Tuple" begin
struct DifferentTuples
a::Tuple
b::NTuple{2}
c::Tuple{String,String,String}
d::Tuple{String,Int64}
e::Tuple{Float64,Int64}
end

exp_str = """
{
"a": ["1", "2", "3"],
"b": ["1", "2"],
"c": ["a", "b", "c"],
"d": ["s", "1"],
"e": ["0.01", "1"]
}
"""
@test Serde.deser_json(DifferentTuples, exp_str).a == Tuple(("1", "2", "3"))
@test Serde.deser_json(DifferentTuples, exp_str).b == NTuple{2}(("1", "2"))
@test Serde.deser_json(DifferentTuples, exp_str).c == Tuple{String,String,String}(("a", "b", "c"))
@test Serde.deser_json(DifferentTuples, exp_str).d == Tuple{String,Int64}(("s", 1))
@test Serde.deser_json(DifferentTuples, exp_str).e == Tuple{Float64,Int64}((0.01, 1))

struct Tuples
a::Tuple
b::NTuple{3,Int64}
c::Tuple{Int64,String,Int64}
end

exp_kvs = Dict{String,Any}(
"a" => ("a", 1),
"b" => (1, 2, 3),
"c" => (1, 2, 3),
)
exp_obj = Tuples(Tuple(("a", 1)), NTuple{3,Int64}((1, 2, 3)), Tuple{Int64,String,Int64}((1, "2", 3)))
@test Serde.deser(Tuples, exp_kvs).a == exp_obj.a
@test Serde.deser(Tuples, exp_kvs).b == exp_obj.b
@test Serde.deser(Tuples, exp_kvs).c == exp_obj.c
end
end

2 comments on commit 1e15159

@gryumov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

Release notes:

  • Add Tuple deserialization

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/113210

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.3.0 -m "<description of version>" 1e15159758e02a7d6d2c886d14beefed343c42b6
git push origin v3.3.0

Please sign in to comment.