|
| 1 | +@testitem "Custom JSON serialization" setup=[NamedPipes] begin |
| 2 | + using JSON |
| 3 | + using JSON: StructuralContext, begin_object, show_pair, end_object, show_json, Serializations.StandardSerialization |
| 4 | + |
| 5 | + struct OurSerialization <: JSON.Serializations.CommonSerialization end |
| 6 | + |
| 7 | + struct OurStruct |
| 8 | + a::String |
| 9 | + b::String |
| 10 | + end |
| 11 | + |
| 12 | + function JSON.show_json(io::StructuralContext, s::OurSerialization, f::OurStruct) |
| 13 | + show_json(io, StandardSerialization(), "$(f.a):$(f.b)") |
| 14 | + end |
| 15 | + |
| 16 | + x = OurStruct("Hello", "World") |
| 17 | + |
| 18 | + socket1, socket2 = NamedPipes.get_named_pipe() |
| 19 | + |
| 20 | + task_done = Base.Event() |
| 21 | + |
| 22 | + messages_back = Channel(Inf) |
| 23 | + |
| 24 | + @async try |
| 25 | + ep2 = JSONRPCEndpoint(socket1, socket1, nothing, OurSerialization()) |
| 26 | + |
| 27 | + run(ep2) |
| 28 | + |
| 29 | + msg = JSONRPC.get_next_message(ep2) |
| 30 | + put!(messages_back, msg) |
| 31 | + |
| 32 | + msg2 = JSONRPC.get_next_message(ep2) |
| 33 | + put!(messages_back, msg2) |
| 34 | + send_success_response(ep2, msg2, [x]) |
| 35 | + |
| 36 | + msg3 = JSONRPC.get_next_message(ep2) |
| 37 | + put!(messages_back, msg3) |
| 38 | + send_error_response(ep2, msg3, 5, "Error", [x]) |
| 39 | + |
| 40 | + close(ep2) |
| 41 | + finally |
| 42 | + notify(task_done) |
| 43 | + catch err |
| 44 | + Base.display_error(err, catch_backtrace()) |
| 45 | + end |
| 46 | + |
| 47 | + ep1 = JSONRPCEndpoint(socket2, socket2, nothing, OurSerialization()) |
| 48 | + |
| 49 | + run(ep1) |
| 50 | + |
| 51 | + send_notification(ep1, "foo", [x]) |
| 52 | + |
| 53 | + response1 = send_request(ep1, "bar", [x]) |
| 54 | + try |
| 55 | + send_request(ep1, "bar", [x]) |
| 56 | + catch err_msg |
| 57 | + if err_msg isa JSONRPC.JSONRPCError |
| 58 | + @test err_msg.data == Any["Hello:World"] |
| 59 | + else |
| 60 | + rethrow(err_msg) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + close(ep1) |
| 65 | + |
| 66 | + wait(task_done) |
| 67 | + |
| 68 | + msg1 = take!(messages_back) |
| 69 | + msg2 = take!(messages_back) |
| 70 | + msg3 = take!(messages_back) |
| 71 | + |
| 72 | + @test msg1.params == ["Hello:World"] |
| 73 | + @test msg2.params == ["Hello:World"] |
| 74 | + @test msg3.params == ["Hello:World"] |
| 75 | +end |
0 commit comments