Skip to content

Commit 3ac8ac6

Browse files
authored
Fix issue reported on discourse where allow_inf not being passed thro… (#159)
* Fix issue reported on discourse where allow_inf not being passed through for nested custom structs * fix test * fx * ffx * fxxx
1 parent 3f5d033 commit 3ac8ac6

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/structs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ end
7474

7575
@inline function read(::Struct, buf, pos, len, b, ::Type{Any}; allow_inf::Bool=false, kw...)
7676
if b == UInt8('{')
77-
return read(DictType(), buf, pos, len, b, Dict{String, Any}; kw...)
77+
return read(DictType(), buf, pos, len, b, Dict{String, Any}; allow_inf=allow_inf, kw...)
7878
elseif b == UInt8('[')
79-
return read(ArrayType(), buf, pos, len, b, Base.Array{Any}; kw...)
79+
return read(ArrayType(), buf, pos, len, b, Base.Array{Any}; allow_inf=allow_inf, kw...)
8080
elseif b == UInt8('"')
8181
return read(StringType(), buf, pos, len, b, String; kw...)
8282
elseif b == UInt8('n')

test/gentypes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
include(file_path)
251251
parsed = JSON3.read(json, Vector{JSONTypes.Root})
252252

253-
@test !(JSONTypes.Root.mutable)
253+
@test !ismutabletype(JSONTypes.Root)
254254
@test parsed[1].c.d == 4
255255
@test fieldtype(JSONTypes.Root, 1) == Union{Int64, String}
256256
end

test/runtests.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using Test, JSON3, StructTypes, UUIDs, Dates
22

3+
if !isdefined(Base, :ismutabletype)
4+
ismutabletype(T) = T.mutable
5+
end
6+
37
struct data
48
t :: Tuple{Symbol, String}
59
end
@@ -129,6 +133,14 @@ struct Wrapper
129133
x::NamedTuple{(:a, :b), Tuple{Int, String}}
130134
end
131135

136+
struct NaNStruct
137+
x::Float64
138+
end
139+
140+
StructTypes.StructType(::Type{NaNStruct}) = StructTypes.CustomStruct()
141+
StructTypes.lower(x::NaNStruct) = x.x
142+
StructTypes.construct(::Type{NaNStruct}, x) = NaNStruct(x)
143+
132144
@testset "JSON3" begin
133145

134146
@testset "read.jl" begin
@@ -910,8 +922,11 @@ x = JSON3.read(json; jsonlines=true)
910922
# allow_inf consistency
911923
@test_throws ArgumentError JSON3.read("-Infinity")
912924

925+
# https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295
926+
str = JSON3.write(NaNStruct(NaN); allow_inf=true)
927+
@test JSON3.read(str, NaNStruct; allow_inf=true).x === NaN
928+
913929
include("gentypes.jl")
930+
include("stringnumber.jl")
914931

915932
end # @testset "JSON3"
916-
917-
include("stringnumber.jl")

test/stringnumber.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737
end
3838

3939
@inline function mutable_struct_equality(a::T, b::T)::Bool where T
40-
if isstructtype(T) && T.mutable
40+
if isstructtype(T) && ismutabletype(T)
4141
T_fieldnames = fieldnames(T)
4242
T_num_fieldnames = length(T_fieldnames)
4343
temp_vector = Vector{Bool}(undef, T_num_fieldnames)

0 commit comments

Comments
 (0)