A modern, attribute-driven JSON-RPC 2.0 framework for Delphi, built on RTTI and the Neon serialization library.
- Protocol Compliance: full support for the JSON-RPC 2.0 specification.
- Attribute-Driven: map Delphi methods to JSON-RPC endpoints with simple attributes like
[JRPCMethod]and[JRPCParam]. - Automatic Marshaling: seamless conversion between JSON and Delphi types (integers, strings, enums, classes, records, arrays).
- Memory Management: built-in garbage collector for request-scoped objects.
- Context Injection: per-request dependency injection via the
[Context]attribute. - Flexible Routing:
TRouteMatcherfor URI-style template matching and parameter extraction. - Flexible Naming:
path+ separator +method, a custom separator, or flat classes ([JRPCPath('')]) whose methods declare the full name (tools/call). - High Performance: powered by Neon, with built-in TStopwatch [PERF] instrumentation in the invoker and the server.
- Transport Agnostic: integrate into any transport layer (HTTP, WebSockets, named pipes, STDIO) β on both sides:
TJRPCServertakes a string and returns one,TJRPCClientis handed a function that does the same, or a streaming transport when the answer arrives as several messages. - Typed Client: call a remote method and get a Delphi value back β
ExecuteAs<T>deserializesresultthrough Neon, and anerrormember arrives as an exception. - Streaming Answers: a transport can deliver notifications before the response (
OnNotification) and long-lived streams of frames (ExecuteStream), without the library linking any transport itself. - Bidirectional Connection:
TJRPCConnectionkeeps calls in flight by id over one stream, with timeouts, cancellation, notifications and peer requests (OnRequest/Respond). - Batch Processing: multiple requests in a single batch.
- Notification Support: full JSON-RPC notification pattern.
| Component | Description |
|---|---|
| JRPC.Core | Base protocol types, message classes (Request, Response, Notification, Error), error codes, serialization logic and the class registry (TJRPCRegistry). |
| JRPC.Invoker | Maps JSON-RPC requests to Delphi method calls using RTTI and attributes. |
| JRPC.Classes | Supporting utilities: garbage collection, context management, route matching. |
| JRPC.Server | Transport-agnostic server component (TJRPCServer) that parses, dispatches and answers requests, including batches. |
| JRPC.Client | Transport-agnostic client component (TJRPCClient): builds requests, correlates answers by id, deserializes result into a Delphi type and raises the peer's error. |
| JRPC.Connection | Bidirectional endpoint (TJRPCConnection) over a blocking message transport: pending calls by id, CallAsync/cancel, timeouts, notifications and peer requests. |
type
[JRPCPath('math')]
TMathApi = class
public
[JRPCMethod('sum')]
function Sum([JRPCParam('a')] A: Integer; [JRPCParam('b')] B: Integer): Integer;
[JRPCMethod('echo')]
function Echo([JRPCParam('message')] const AMessage: string): string;
end;
function TMathApi.Sum(A, B: Integer): Integer;
begin
Result := A + B;
end;uses
JRPC.Core,
JRPC.Server;
// Once, at startup:
TJRPCRegistry.Instance.RegisterClass(TMathApi);
// Per request (a transport feeds the raw JSON in, sends the JSON out):
var
LServer: TJRPCServer;
LResponse: string;
begin
LServer := TJRPCServer.Create(nil);
try
LResponse := LServer.ProcessRequest(
'{"jsonrpc":"2.0","id":1,"method":"math/sum","params":{"a":10,"b":20}}');
// LResponse = {"result":30,"id":1,"jsonrpc":"2.0"}
finally
LServer.Free;
end;
end;ProcessRequest answers a single request with a single Response object, a batch with an array of Response objects, and a notification with an empty string (per the spec, a server never answers a notification).
TJRPCClient is the mirror image: it links no transport either, so you hand it the one function that gets a payload to the peer and the answer back.
uses
JRPC.Core,
JRPC.Client;
var
LClient: TJRPCClient;
begin
LClient := TJRPCClient.Create(nil);
try
LClient.OnTransport :=
function(const ARequest: string): string
begin
Result := MySocket.SendAndReadLine(ARequest); // or LServer.ProcessRequest, to loop back
end;
// The id is generated, sent, and checked against the one echoed back.
WriteLn(LClient.ExecuteAs<Integer>(
TJRPCRequestBuilder.New
.SetMethod('math/sum')
.AddNamedParam('a', 10)
.AddNamedParam('b', 20)));
finally
LClient.Free;
end;
end;A failure the peer reports arrives as EJRPCRemoteError, carrying its Code, Message and structured ErrorData; a failure of the call itself (no transport, unreadable answer, an id that does not match) arrives as EJRPCClientError. Assign OnError to log every failure in one place; it reports and the exception carries on as usual.
ExecuteAsJSONValue returns the raw TJSONValue instead and ExecuteAsJsonString its text, while Execute covers a call made for its effect and a notification, ExecuteBatch a batch, and ExecuteRaw a payload put on the wire untouched.
TJRPCRequestBuilder is interfaced, so the request is freed with it and no call site needs a try/finally. Build only lends the request out β the builder keeps ownership β so anything that adopts a request, TJRPCMessages in a batch above all, gets a plain TJRPCRequest.Create instead.
Because the transport is just a function, a client can be pointed straight at a TJRPCServer in the same process β which is how the suite tests it, with no socket involved.
A JSON-RPC answer is normally one message. Some transports (an SSE body, a WebSocket, a pipe) can carry several: a peer may send progress notifications before the response, and a subscription stream may never send a response at all.
A transport that also implements IJRPCStreamingTransport is used in streaming mode automatically. Each frame is parsed; notifications go to OnNotification; the response resolves the call.
LClient.OnNotification :=
procedure(const ANotification: TJRPCNotification)
begin
// progress, log, ... The object is valid only for the duration of the call.
end;
// An ordinary call: the response still comes back, the notifications do not
// get in the way.
LValue := LClient.ExecuteAs<Integer>(LRequest);
// A long-lived stream: frames are raw JSON, in arrival order, on a thread of
// the client's own.
LStream := LClient.ExecuteStream(
TJRPCRequestBuilder.New.SetMethod('subscriptions/listen'),
function(const AJSON: string): Boolean
begin
HandleFrame(AJSON);
Result := True; // False stops the read
end);
try
LStream.Wait(5000);
finally
LStream.Close; // asks an IJRPCAbortableTransport to abort
end;IJRPCStream.Close is idempotent and unblocks the read when the transport implements IJRPCAbortableTransport; Wait blocks until the read loop ends and Completed says whether it has. The client must outlive its streams: they deliver through its OnAfterReceive/OnError.
NeonConfig lets a client deserialize results with a configuration other than the default camel-cased one β the types of a protocol that spells its members differently, or one with custom serializers.
When one stream carries several calls, replies, notifications and peer requests, use TJRPCConnection instead of the client. The transport is a blocking Receive plus a Send; the connection owns the reader thread and matches replies to pending calls by id.
uses
JRPC.Connection;
LConnection := TJRPCConnection.Create(LTransport); // IJRPCMessageTransport
try
LConnection.OnNotification :=
procedure(const ANotification: TJRPCNotification)
begin
// Runs on the reader thread; do not Call synchronously from here.
end;
LConnection.OnRequest :=
procedure(const ARequest: TJRPCRequest)
var
LResponse: TJRPCResponse;
begin
LResponse := TJRPCResponse.Create;
try
LResponse.Id := ARequest.Id;
LResponse.Result := TJSONString.Create('pong');
LConnection.Respond(LResponse);
finally
LResponse.Free;
end;
end;
LConnection.Open;
// Blocking, with a timeout (0 waits forever); raises EJRPCRemoteError when
// the peer answers with an error.
LResponse := LConnection.Call(LRequest, 5000);
try
Use(LResponse.Result);
finally
LResponse.Free;
end;
// Or without blocking:
LCall := LConnection.CallAsync(LRequest);
if not LCall.Wait(5000) then
LCall.Cancel;
finally
LConnection.Free; // closes and joins the reader
end;CallAsync returns an IJRPCCall: Wait, Completed, Cancel, and TakeResponse/TakeError, which transfer ownership of the reply exactly once. Close fails every pending call with EJRPCClientError, closes the transport and joins the reader; OnClose fires once, whether the connection was closed locally or by the peer. Peer requests are answered with Respond, and notifications with Notify.
A JSON-RPC batch is not fanned out by the connection: one message per frame, and a batch answer is reported to OnError.
By default a method is addressed as path + separator + method, where the separator is / (or whatever a class declares with the separator= tag):
[JRPCPath('math')] // math/sum
[JRPCPath('custom', 'separator=.')] // custom.helloA class that contributes no path - no attribute at all, or an explicitly empty [JRPCPath('')] - is flat: its methods declare the full JSON-RPC method name themselves, which is handy for protocols whose names are fixed (tools/list, resources/read, ...). Classes are registered explicitly, never discovered, so the attribute is only needed when it has something to say:
type
TToolsApi = class // no attribute needed
public
[JRPCMethod('tools/list')]
function ToolsList: string;
[JRPCMethod('tools/call')]
function ToolsCall([JRPCParam('name')] const AName: string): string;
[JRPCMethod('ping')] // no separator needed at all
function Ping: string;
end;Flat and path-based classes coexist in the same registry: a fully qualified flat name is resolved first, and everything else falls back to the usual path lookup. In a flat class every exposed method needs its [JRPCMethod] attribute - there is no implicit fallback to the Delphi method name, and two classes claiming the same flat name are rejected at registration time.
type
[JRPCPath('ctx')]
TContextApi = class
private
[Context] FGC: IGarbageCollector;
[Context] Request: TJRPCRequest;
public
[JRPCMethod('method')]
function GetRequestMethod: string;
end;
function TContextApi.GetRequestMethod: string;
begin
Result := 'request=' + Request.Method;
// Objects registered with the garbage collector are freed automatically
// when the request finishes - no manual Free here.
var LScoped := TStringList.Create;
FGC.Add(LScoped);
LScoped.Add('freed with the request');
end;Demo/JRPCDemo is a console test runner that exercises
the library end to end: named/positional parameters, notifications, batches (including batches with invalid elements), all standard error codes, object results with default and per-class Neon casing, whole-params objects ([JRPCParams]), enum parameters, null-id round trips, context injection, garbage collection and custom separators. It exits with a non-zero code when any assertion fails, and it registers a Logify console adapter at startup so the [PERF] timing lines are printed between every request/response pair.
BuildDemo.bat
Demo/JRPCExample showcases a realistic example API - a notes CRUD service over an in-memory store (object params/results, array results, enums), plus custom separators, per-class Neon casing and context injection - as a guided showcase followed by an interactive prompt where any request can be typed and answered:
BuildExample.bat
- Delphi 11 or newer.
- Neon: high-performance JSON serialization for Delphi (github.com/paolo-rossi/delphi-neon).
- Logify: meta-logger used by the [PERF] instrumentation (github.com/delphi-blocks/Logify).
Clone both dependencies into Libs\ (the demo and test projects resolve them from there):
git clone https://github.com/paolo-rossi/delphi-neon Libs\Neon
git clone https://github.com/delphi-blocks/Logify Libs\Logify
This project is licensed under the MIT License - see the LICENSE file for details.
