# JSON3.jl parse custom type with NaN

**URL:** https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295
**Category:** Data
**Tags:** question, json3
**Created:** [May 17, 2021, 11:15am UTC](https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295 "2021-05-17T11:15:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [May 17, 2021, 11:15am UTC](https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295/1 "2021-05-17T11:15:24Z")

</div>

The following works:

```julia
struct Foo
    a::Vector{Float64}
    b::Float64
end

JSON3.StructType(::Type{Foo}) = JSON3.Struct()

str = JSON3.write(Foo([NaN, 2.3], NaN), allow_inf=true)
JSON3.read(str, Foo, allow_inf=true)

```

With a custom type it does not:

```julia
struct Foo
    a::Vector{Float64}
    b::Float64
end

JSON3.StructType(::Type{Foo}) = JSON3.CustomStruct()
function JSON3.StructTypes.lower(x::Foo)
    return Dict(
        "a" => x.a,
        "b" => x.b,
    )
end
function JSON3.StructTypes.construct(::Type{Foo}, x)
    Foo(x["a"], x["b"])
end

str = JSON3.write(Foo([NaN, 2.3], NaN), allow_inf=true)
JSON3.read(str, Foo, allow_inf=true)

```

```julia
ERROR: LoadError: ArgumentError: invalid JSON at byte position 6 while parsing type Any: InvalidChar
{"b":NaN,"a":[NaN,2.3]}

Stacktrace:
  [1] invalid(error::JSON3.Error, buf::Base.CodeUnits{UInt8, String}, pos::Int64, T::Type)
    @ JSON3 .julia\packages\JSON3\A0XBc\src\JSON3.jl:29
  [2] read(::StructTypes.UnorderedStruct, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Any}; allow_inf::Bool, kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:100
  [3] read(::StructTypes.UnorderedStruct, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Any})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:76
  [4] read(::StructTypes.DictType, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Dict{String, Any}}, ::Type{String}, ::Type{Any}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:342
  [5] read(::StructTypes.DictType, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Dict{String, Any}}, ::Type{String}, ::Type{Any})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:291
  [6] read(::StructTypes.DictType, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Dict{String, Any}}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:288
  [7] read
    @ .julia\packages\JSON3\A0XBc\src\structs.jl:288 [inlined]
  [8] read(::StructTypes.UnorderedStruct, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Any}; allow_inf::Bool, kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ JSON3 .julia\packages\JSON3\A0XBc\src\structs.jl:77
  [9] #read#37
    @ .julia\packages\JSON3\A0XBc\src\structs.jl:371 [inlined]
 [10] #read#16
    @ .julia\packages\JSON3\A0XBc\src\structs.jl:34 [inlined]
 [11] top-level scope
    @ dev.jl:81
in expression starting at dev.jl:63

```

Is this a bug or do I do something wrong?

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [May 17, 2021, 6:16pm UTC](https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295/2 "2021-05-17T18:16:51Z")

</div>

Out of curiosity what is written to the JSON file in the example that works? I don’t believe NaN, infinity, or negative infinity are part of the JSON standard.

---

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [May 17, 2021, 6:37pm UTC](https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295/3 "2021-05-17T18:37:15Z")

</div>

NaN  
But it is true, this is not standard JSON.

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [May 25, 2021, 3:45am UTC](https://discourse.julialang.org/t/json3-jl-parse-custom-type-with-nan/61295/4 "2021-05-25T03:45:49Z")

</div>

Yeah, this is a bug; thanks for reporting. The problem here is because you don’t define a specific `StructTypes.lowertype` for your custom struct (which is fine), we default to parsing a `Dict{String, Any}`, and the `allow_inf` keyword wasn’t getting passed through recursively for nested array/objects. Fix is up here: [Fix issue reported on discourse where allow\_inf not being passed thro… by quinnj · Pull Request #159 · quinnj/JSON3.jl · GitHub](https://github.com/quinnj/JSON3.jl/pull/159)
