Skip to content
Draft
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
Manifest.toml
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
18 changes: 9 additions & 9 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 10 additions & 16 deletions src/interface_def.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -60,7 +54,7 @@ macro dict_readable(arg)
end
) : nothing)

function $tname(dict::Dict)
function $tname(dict::AbstractDict)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/packagedef.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 4 additions & 7 deletions test/test_json_serialization.jl
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -22,7 +19,7 @@
messages_back = Channel(Inf)

@async try
ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurSerialization())
ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurStyle())

run(ep2)

Expand All @@ -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)

Expand Down
Loading