quinnj / JSON3.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`car` example fails? v1.3.0 (in README)

JeffreySarnoff opened this issue · comments

the example in the test file works, the one in the README did not

julia> using JSON3
[ Info: Precompiling JSON3 [0f8b85d8-7281-11e9-16c2-39a750bddbf1]

julia> abstract type Vehicle end

julia>

julia> struct Car <: Vehicle
           type::String
           make::String
           model::String
           seatingCapacity::Int
           topSpeed::Float64
       end

julia>

julia> struct Truck <: Vehicle
           type::String
           make::String
           model::String
           payloadCapacity::Float64
       end

julia>

julia> JSON3.StructType(::Type{Vehicle}) = JSON3.AbstractType()

julia> JSON3.subtypekey(::Type{Vehicle}) = :type

julia> JSON3.subtypes(::Type{Vehicle}) = (car=Car, truck=Truck)

julia>

julia> car = JSON3.read("""
       {
           "type": "car",
           "make": "Mercedes-Benz",
           "model": "S500",
           "seatingCapacity": 5,
           "topSpeed": 250.1
       }""", Vehicle)
ERROR: ArgumentError: Car doesn't have a defined `JSON3.StructType`
Stacktrace:
 [1] read(::JSON3.NoStructType, ::Base.CodeUnits{UInt8,String}, ::Int64, ::Int64, ::UInt8, ::Type{Car}) at C:\Users\jas\.julia\packages\JSON3\PVBst\src\structs.jl:301
 [2] read(::String, ::Type{Vehicle}) at C:\Users\jas\.julia\packages\JSON3\PVBst\src\structs.jl:920
 [3] top-level scope at REPL[24]:1

adding this makes it work (should it happen automatically)
JSON3.StructType(::Type{Car}) = JSON3.Struct()
JSON3.StructType(::Type{Truck}) = JSON3.Struct()

Fixed on master; thanks for the report and suggested fix @JeffreySarnoff!