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
1 change: 1 addition & 0 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ StructUtils.lower(::JSONStyle, x::Regex) = x.pattern
StructUtils.lower(::JSONStyle, x::AbstractArray{<:Any,0}) = x[1]
StructUtils.lower(::JSONStyle, x::AbstractArray{<:Any, N}) where {N} = (view(x, ntuple(_ -> :, N - 1)..., j) for j in axes(x, N))
StructUtils.lower(::JSONStyle, x::AbstractVector) = x
StructUtils.arraylike(::JSONStyle, x::AbstractVector{<:Pair}) = false

# for pre-1.0 compat, which serialized Tuple object keys by default
StructUtils.lowerkey(::JSONStyle, x::Tuple) = string(x)
Expand Down
7 changes: 7 additions & 0 deletions test/json.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ end
@test JSON.json(Dict{Int, Int}(1 => 2)) == "{\"1\":2}"
@test JSON.json((a = 1, b = 2)) == "{\"a\":1,\"b\":2}"
@test JSON.json((a = nothing, b=2, c="hey", d=3.14, e=true, f=false)) == "{\"a\":null,\"b\":2,\"c\":\"hey\",\"d\":3.14,\"e\":true,\"f\":false}"
# test Vector{Pair} serializes as object, not array
@test JSON.json(Pair{String, Int}[]) == "{}"
@test JSON.json([:a => 1, :b => 2]) == "{\"a\":1,\"b\":2}"
@test JSON.json(["x" => "value", "y" => 42]) == "{\"x\":\"value\",\"y\":42}"
@test JSON.json([1 => "one", 2 => "two"]) == "{\"1\":\"one\",\"2\":\"two\"}"
# test Vector{Pair} in nested structures
@test JSON.json(Dict("data" => [:x => 1, :y => 2])) == "{\"data\":{\"x\":1,\"y\":2}}"
# test the JSON output of nested array/objects
@test JSON.json([1, [2, 3], [4, [5, 6]]]) == "[1,[2,3],[4,[5,6]]]"
@test JSON.json(Dict{Int, Any}(1 => Dict{Int, Any}(2 => Dict{Int, Any}(3 => 4)))) == "{\"1\":{\"2\":{\"3\":4}}}"
Expand Down
Loading