Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ StructUtils.defaultstate(st::JSONReadStyle) = StructUtils.defaultstate(st.style)
StructUtils.dictlike(st::JSONReadStyle, ::Type{T}) where {T} = StructUtils.dictlike(st.style, T)
StructUtils.arraylike(st::JSONReadStyle, ::Type{T}) where {T} = StructUtils.arraylike(st.style, T)
StructUtils.nulllike(st::JSONReadStyle, ::Type{T}) where {T} = StructUtils.nulllike(st.style, T)
# Keep structlike forwarding specific to custom JSONStyle wrappers so type-level StructStyle
# specializations (for example @nonstruct types) continue to dispatch without ambiguity.
StructUtils.structlike(st::JSONReadStyle{O,N,S}, ::Type{T}) where {O,N,S<:JSONStyle,T} =
StructUtils.structlike(st.style, T)
StructUtils.structlike(st::JSONReadStyle{O,N,S}, ::Type{T}) where {O,N,S<:JSONStyle,T<:NamedTuple} =
StructUtils.structlike(st.style, T)

function jsonreadstyle(::Type{T}, ::Type{O}, null, style::StructStyle, unknown_fields::Symbol) where {T,O}
ignore_unknown_fields =
Expand Down
10 changes: 10 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JSON, StructUtils, UUIDs, Dates, Test

struct CustomJSONStyle <: JSON.JSONStyle end
struct RefValueStyle <: JSON.JSONStyle end

struct A
a::Int
Expand Down Expand Up @@ -236,6 +237,10 @@ Base.valtype(::DictlikeViaCustomStyle) = Int
StructUtils.addkeyval!(a::DictlikeViaCustomStyle, k, v) = StructUtils.addkeyval!(a.vals, k, v)
StructUtils.dictlike(::CustomJSONStyle, ::Type{DictlikeViaCustomStyle}) = true

StructUtils.structlike(::RefValueStyle, ::Type{Base.RefValue{Int}}) = false
StructUtils.lower(::RefValueStyle, x::Base.RefValue{Int}) = x[]
StructUtils.lift(::RefValueStyle, ::Type{Base.RefValue{Int}}, x::Integer) = Ref{Int}(x), nothing

@testset "JSON.parse" begin
@testset "errors" begin
# Unexpected character in array
Expand Down Expand Up @@ -781,6 +786,11 @@ StructUtils.dictlike(::CustomJSONStyle, ::Type{DictlikeViaCustomStyle}) = true
let res = JSON.parse("""{"a": 1, "b": 2}""", DictlikeViaCustomStyle; style=CustomJSONStyle())
@test res.vals == Dict("a" => 1, "b" => 2)
end
# https://github.com/JuliaIO/JSON.jl/issues/462 - structlike dispatch on custom JSONStyle must reach user method
let json = JSON.json(Ref{Int}(1); style=RefValueStyle())
@test json == "1"
@test JSON.parse(json, Base.RefValue{Int}; style=RefValueStyle())[] == 1
end
@test isequal(JSON.parse("{\"num\": 1,\"den\":null}", @NamedTuple{num::Int, den::Union{Int, Missing}}; null=missing, style=StructUtils.DefaultStyle()), (num=1, den=missing))
# choosetype field tag on Any struct field
@test JSON.parse("{\"id\":1,\"any\":{\"type\":\"int\",\"value\":10}}", Q) == Q(1, (type="int", value=10))
Expand Down
Loading