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

Lower with respect to serialization context

Open twavv opened this issue 5 years ago • 1 comments

Would it be possible to pass the serialization context to JSON.lower? Typically, the thing I want to override is the shape of the thing that's being lowered rather than the formatting and this would make it much easier.

e.g. I could define a VerboseSerializationContext and just have overloads of the form

function JSON.lower(::VerboseSerializationContext, s::MyStruct)
    return Dict(
        "debugId" => ...,
    )
end

One issue with the proposal above is that it would probably require existing definitions of JSON.lower(x::MyType) = ... to be re-written as JSON.lower(::CommonSerialization, x::MyType). Either that or define

# Let the single-arg form be the "common" form
JSON.lower(::CommonSerialization, x::Any) = JSON.lower(x)

which wouldn't be breaking (afaict).

If there are better ways to do what I'm describing, I'm all ears! :^)

twavv avatar Jan 05 '20 22:01 twavv

I think your second proposal (the JSON.lower(::CommonSerialization, x::Any) = JSON.lower(x)) is pretty reasonable, and is compatible with the direction we've gone (support for serialization contexts while making it "easy" to use the default one). A PR is certainly welcome!

TotalVerb avatar Jan 06 '20 02:01 TotalVerb