diff --git a/src/write.jl b/src/write.jl index 775ab49..b81e766 100644 --- a/src/write.jl +++ b/src/write.jl @@ -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) diff --git a/test/json.jl b/test/json.jl index 4af4b64..4d33312 100644 --- a/test/json.jl +++ b/test/json.jl @@ -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}}}"