JSON.jl icon indicating copy to clipboard operation
JSON.jl copied to clipboard

Serialization of Pair and Vector{Pair} returns unexpected result

Open hhaensel opened this issue 5 months ago • 1 comments

From JSON3 I expected a Pair to be serialized as an Object, but I find:

julia> JSON3.write("message" => "Hello World!")
"{\"message\":\"Hello World!\"}"

julia> JSON.json("message" => "Hello World!")
"{\"first\":\"message\",\"second\":\"Hello World!\"}"

and

julia> JSON3.write(["message" => "Hello World!"])
"[{\"message\":\"Hello World!\"}]"

julia> JSON.json(["message" => "Hello World!"])
"[\"Hello World!\"]"

Is this behaviour by purpose?

I could solve the issue by defining

JSON.lower(p::Pair) = JSON.Object(Symbol(p.first) => p.second)
JSON.lower(::JSON.JSONStyle, pp::AbstractVector{<:Pair}) = JSON.Object(pp)

But I guess such lowering should be done rather in a private JSONStyle then?

hhaensel avatar Oct 14 '25 10:10 hhaensel

@quinnj

This is behavior is breaking our migration from JSON3 to JSON v1 of GenieFramework. We could, of cause, eliminate that single call where our tests break, but end users may have setup their applications assuming that json of Pairs are Objects, as it used to be for both JSON 0.x and JSON3. We could as well do the lowering as mentioned above, but I expect that more users will be concerned sooner or later. What do you think? Concerning Vectors of Pairs it's not clear to me why we should expect an output of values without keys.

hhaensel avatar Nov 04 '25 21:11 hhaensel

@quinnj what about the serialisation of a Pair?

hhaensel avatar Nov 08 '25 18:11 hhaensel

It was fixed via https://github.com/JuliaServices/StructUtils.jl/pull/22

quinnj avatar Nov 10 '25 13:11 quinnj