diff --git a/.gitignore b/.gitignore index 722d5e71..f8509162 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +Manifest.toml diff --git a/Project.toml b/Project.toml index d4349fa5..6dceb4b7 100644 --- a/Project.toml +++ b/Project.toml @@ -14,7 +14,7 @@ TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] -JSON = "0.20, 0.21" +JSON = "1.4" julia = "1" CancellationTokens = "1.1" diff --git a/src/core.jl b/src/core.jl index c33dae4c..344749fb 100644 --- a/src/core.jl +++ b/src/core.jl @@ -107,12 +107,12 @@ end struct Request method::String - params::Union{Nothing,Dict{String,Any},Vector{Any}} + params::Union{Nothing,JSON.Object{String,Any},Vector{Any}} id::Union{Nothing,String,Int} token::Union{CancellationTokens.CancellationToken,Nothing} end -mutable struct JSONRPCEndpoint{IOIn<:IO,IOOut<:IO,S<:JSON.Serialization} +mutable struct JSONRPCEndpoint{IOIn<:IO,IOOut<:IO,S<:JSON.JSONStyle} pipe_in::IOIn pipe_out::IOOut @@ -132,10 +132,10 @@ mutable struct JSONRPCEndpoint{IOIn<:IO,IOOut<:IO,S<:JSON.Serialization} read_task::Union{Nothing,Task} write_task::Union{Nothing,Task} - serialization::S + style::S end -JSONRPCEndpoint(pipe_in, pipe_out, err_handler=nothing, serialization::JSON.Serialization=JSON.StandardSerialization()) = +JSONRPCEndpoint(pipe_in, pipe_out, err_handler=nothing, style::JSON.JSONStyle=JSON.JSONWriteStyle()) = JSONRPCEndpoint( pipe_in, pipe_out, @@ -149,7 +149,7 @@ JSONRPCEndpoint(pipe_in, pipe_out, err_handler=nothing, serialization::JSON.Seri :idle, nothing, nothing, - serialization) + style) function write_transport_layer(stream, response) response_utf8 = transcode(UInt8, response) @@ -339,7 +339,7 @@ function send_notification(x::JSONRPCEndpoint, method::AbstractString, @nospecia message = Dict("jsonrpc" => "2.0", "method" => method, "params" => params) - message_json = sprint(JSON.show_json, x.serialization, message) + message_json = JSON.json(message; style=x.style) put!(x.out_msg_queue, message_json) @@ -355,7 +355,7 @@ function send_request(x::JSONRPCEndpoint, method::AbstractString, @nospecialize( response_channel = Channel{Any}(1) x.outstanding_requests[id] = response_channel - message_json = sprint(JSON.show_json, x.serialization, message) + message_json = JSON.json(message; style=x.style) put!(x.out_msg_queue, message_json) @@ -464,7 +464,7 @@ function send_success_response(endpoint, original_request::Request, @nospecializ response = Dict("jsonrpc" => "2.0", "id" => original_request.id, "result" => result) - response_json = sprint(JSON.show_json, endpoint.serialization, response) + response_json = JSON.json(response; style=endpoint.style) put!(endpoint.out_msg_queue, response_json) end @@ -478,7 +478,7 @@ function send_error_response(endpoint, original_request::Request, @nospecialize( response = Dict("jsonrpc" => "2.0", "id" => original_request.id, "error" => Dict("code" => code, "message" => message, "data" => data)) - response_json = sprint(JSON.show_json, endpoint.serialization, response) + response_json = JSON.json(response; style=endpoint.style) put!(endpoint.out_msg_queue, response_json) end diff --git a/src/interface_def.jl b/src/interface_def.jl index 0e047f99..cb84f35b 100644 --- a/src/interface_def.jl +++ b/src/interface_def.jl @@ -1,23 +1,17 @@ abstract type Outbound end -function JSON.Writer.CompositeTypeWrapper(t::Outbound) - fns = collect(fieldnames(typeof(t))) - dels = Int[] - for i = 1:length(fns) - f = fns[i] - if getfield(t, f) isa Missing - push!(dels, i) - end - end - deleteat!(fns, dels) - JSON.Writer.CompositeTypeWrapper(t, Tuple(fns)) -end - function JSON.lower(a::Outbound) if nfields(a) > 0 - JSON.Writer.CompositeTypeWrapper(a) + d = Dict{String,Any}() + for fn in fieldnames(typeof(a)) + v = getfield(a, fn) + if !(v isa Missing) + d[string(fn)] = v + end + end + return d else - nothing + return nothing end end @@ -60,7 +54,7 @@ macro dict_readable(arg) end ) : nothing) - function $tname(dict::Dict) + function $tname(dict::AbstractDict) end end diff --git a/src/packagedef.jl b/src/packagedef.jl index 6686f485..f0c47c15 100644 --- a/src/packagedef.jl +++ b/src/packagedef.jl @@ -8,7 +8,7 @@ include("interface_def.jl") function _precompile_() ccall(:jl_generating_output, Cint, ()) == 1 || return nothing - E = JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint, JSON.Serializations.StandardSerialization} + E = JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint, JSON.JSONWriteStyle} precompile(Base.run, (E,)) precompile(send_notification, (E, String, Any)) precompile(send_request, (E, String, Any)) diff --git a/test/test_json_serialization.jl b/test/test_json_serialization.jl index 8783b67e..6186d242 100644 --- a/test/test_json_serialization.jl +++ b/test/test_json_serialization.jl @@ -1,17 +1,14 @@ @testitem "Custom JSON serialization" setup=[NamedPipes] begin using JSON - using JSON: StructuralContext, begin_object, show_pair, end_object, show_json, Serializations.StandardSerialization - struct OurSerialization <: JSON.Serializations.CommonSerialization end + struct OurStyle <: JSON.JSONStyle end struct OurStruct a::String b::String end - function JSON.show_json(io::StructuralContext, s::OurSerialization, f::OurStruct) - show_json(io, StandardSerialization(), "$(f.a):$(f.b)") - end + JSON.lower(f::OurStruct) = "$(f.a):$(f.b)" x = OurStruct("Hello", "World") @@ -22,7 +19,7 @@ messages_back = Channel(Inf) @async try - ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurSerialization()) + ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurStyle()) run(ep2) @@ -44,7 +41,7 @@ Base.display_error(err, catch_backtrace()) end - ep1 = JSONRPCEndpoint(socket2, socket2, nothing, OurSerialization()) + ep1 = JSONRPCEndpoint(socket2, socket2, nothing, OurStyle()) run(ep1)