JuliaIO / JSON.jl

JSON parsing and printing
Other
313 stars 101 forks source link

Can't serialize empty NamedTuple #279

Closed cchderrick closed 4 years ago

cchderrick commented 5 years ago

I am using JSON v0.20.0 on Julia Version 1.0.3 Code to reproduce:

x = NamedTuple()
y = (a=1,b=2)
julia> typeof(x)
NamedTuple{(),Tuple{}}

julia> typeof(y)
NamedTuple{(:a, :b),Tuple{Int64,Int64}}

Correctly serialize stuffed NamedTuple.

julia> JSON.json(y)
"{\"a\":1,\"b\":2}"

Expecting empty NamedTuple serialized to an empty object.

julia> JSON.json(x)
"{}"

But got this:

julia> JSON.json(x)
ERROR: Cannot serialize type NamedTuple{(),Tuple{}}
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] lower(::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:47
 [3] show_json(::JSON.Writer.CompactContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::JSON.Serializations.StandardSerialization, ::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:314
 [4] #show_json#3(::Nothing, ::Function, ::Base.GenericIOBuffer{Array{UInt8,1}}, ::JSON.Serializations.StandardSerialization, ::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:325
 [5] show_json(::Base.GenericIOBuffer{Array{UInt8,1}}, ::JSON.Serializations.StandardSerialization, ::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:324
 [6] print(::Base.GenericIOBuffer{Array{UInt8,1}}, ::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:349
 [7] #sprint#324(::Nothing, ::Int64, ::Function, ::Function, ::NamedTuple{(),Tuple{}}) at .\strings\io.jl:101
 [8] sprint(::Function, ::NamedTuple{(),Tuple{}}) at .\strings\io.jl:97
 [9] json(::NamedTuple{(),Tuple{}}) at .\.julia\packages\JSON\ebvl3\src\Writer.jl:354
 [10] top-level scope at none:0