Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/gentypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end
struct Top end

# get the type from a named tuple, given a name
get_type(NT, k) = hasfield(NT, k) ? fieldtype(NT, k) : Nothing
get_type(NT, k) = hasfield(NT, k) ? fieldtype(NT, k) : Missing

# unify two types to a single type
function promoteunion(T, S)
Expand All @@ -36,7 +36,7 @@ function unify(
for (k, v) in zip(B, fieldtypes(b))
if !(k in ks)
push!(ks, k)
push!(ts, unify(v, Nothing))
push!(ts, unify(v, Missing))
end
end

Expand Down Expand Up @@ -161,13 +161,27 @@ function generate_expr!(
mutable::Bool = true,
) where {N,T<:Tuple}
sub_exprs = []
missing_fields = []
for (n, t) in zip(N, fieldtypes(nt))
push!(sub_exprs, generate_field_expr!(exprs, t, n; mutable = mutable))
if Missing <: t
push!(missing_fields, n)
end
end

struct_name = pascalcase(root_name)
missing_kw_args = (length(missing_fields) > 0 ? "; " : "") * join(map(n -> "n=missing", missing_fields), ", ")
if mutable
push!(sub_exprs, Meta.parse("$struct_name() = new()"))
if length(missing_fields) == 0
push!(sub_exprs, Meta.parse("$struct_name() = new()"))
else
missing_assigns = join(map(n -> "x.$n = missing", missing_fields), "\n")
push!(sub_exprs, Meta.parse("""function $struct_name()
x = new()
$missing_assigns
return x
end"""))
end
end

push!(exprs, Expr(:struct, mutable, struct_name, Expr(:block, sub_exprs...)))
Expand Down
3 changes: 2 additions & 1 deletion src/structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function read(::Struct, buf, pos, len, b, U::Union; kw...)
# Julia implementation detail: Unions are sorted :)
# This lets us avoid the below try-catch when U <: Union{Missing,T}
if U.a === Nothing || U.a === Missing
if buf[pos] == UInt8('n')
# fallback to nothing if Union{Missing, Nothing}
if buf[pos] == UInt8('n') && !(Nothing <: U.b)
return read(StructType(U.a), buf, pos, len, b, U.a)
else
return read(StructType(U.b), buf, pos, len, b, U.b; kw...)
Expand Down
18 changes: 18 additions & 0 deletions test/gentypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,22 @@
@test length(json_arr) == 2
@test json_arr[1].menu.header == "SVG Viewer"
end

@testset "Missingness" begin
json = """
[
{"a": "w", "b": 5, "c": 9, "d": null},
{"a": 3, "b": 4, "c": 2},
{"a": 7, "b": 7, "c": 0, "d": 10}
]
"""

JSON3.@generatetypes(json)
res = JSON3.read(json, Vector{JSONTypes.Root})

@test res[3].d == 10
@test ismissing(res[2].d)
@test res[1].d === nothing

Choose a reason for hiding this comment

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

Nit: can we reorder the asserts to be the same order as the input data, and also check for missing and nothing in the same way? Eg,

@test res[1].d === nothing
@test res[2].d === missing
@test res[3].d == 10


end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ obj = JSON3.read("""
@test JSON3.read("\"1\"", Union{String, Int}) == "1"
@test JSON3.read("null", Union{Int, String, Nothing}) === nothing
@test JSON3.read("1.0", Union{Float64, Int}) === 1.0
@test JSON3.read("null", Union{Missing, Nothing, Int}) === nothing

@test JSON3.read("1", Any) == 1
@test JSON3.read("3.14", Any) == 3.14
Expand Down