Parse and Serialize JSON-RPC2 messages in free-pascal or Delphi application. Git Repository: https://github.com/r3code/pascal-jsonrpc-lite-2
This is an enhanced version of https://github.com/r3code/pascal-jsonrpc-lite
Inspired by https://github.com/teambition/jsonrpc-lite.
An implementation of JSON-RPC 2.0 specifications
SuperObject JSON parsing library https://github.com/hgourvest/superobject
- Download.
- Extract.
- Add
ujsonrpc2.pas
to your project or add path toujsonrpc2.pas
to Lib Path.
uses ujsonrpc2;
Returns object as ISuperObject.
Returns a JSON string object representation.
Params:
indent
: {boolean} indent resulting JSONescape
: {boolean} escape special chars
Creates a JSON-RPC 2.0 request object, return IJsonRpcMessage object. Realizes IJsonRpcMessage. Returns {IJsonRpcError}.
Params:
id
: {String}method
: {String}params
: {ISuperObject}
Example:
var requestObj: TJsonRpcMessage;
requestObj := TJsonRpcMessage.Request('123', 'update', SO('{list: [1, 2, 3]}'));
writeln(requestObj.asString);
// {
// jsonrpc: '2.0',
// id: '123',
// method: 'update',
// params: {list: [1, 2, 3]}
// }
Creates a JSON-RPC 2.0 notification object, returns {IJsonRpcMessage}.
Params:
method
: {String}params
: {ISuperObject}
Example:
var notificationObj: TJsonRpcMessage;
notificationObj := TJsonRpcMessage.notification('update', SO('{list: [1, 2, 3]}'));
writeln(notificationObj.asString);
// {
// jsonrpc: '2.0',
// method: 'update',
// params: {list: [1, 2, 3]}
// }
Creates a JSON-RPC 2.0 success response object, returns {IJsonRpcMessage}.
Params:
id
: {String}result
: {ISuperObject}
Example:
var msg: TJsonRpcMessage;
msg := TJsonRpcMessage.success('123', 'OK');
writeln(msg.asString);
// {
// jsonrpc: '2.0',
// id: '123',
// result: 'OK',
// }
Creates a JSON-RPC 2.0 error response object, returns {IJsonRpcMessage}.
Params:
id
: {String}error
: {IJsonRpcError} use TJsonRpcError or it's siblings
Example:
var msg: TJsonRpcMessage;
msg := TJsonRpcMessage.Error('123', TJsonRpcError.Create(-32000, 'some error', 'blabla'));
writeln(msg.asString);
// {
// jsonrpc: '2.0',
// id: '123',
// error: {code: -32000, 'message': 'some error', data: 'blabla'},
// }
Takes a JSON-RPC 2.0 payload (string) and tries to parse it into a JSON.
If successful, determine what object is it (response, notification, success,
error, or invalid), and return it's type and properly formatted object.
In case of error see details in err
value.
Params:
s
: {String}err
: {IJsonRpcError}
Returns one of {IJsonRpcParsedMessage} realizations if OK,
else nil
and err
set to one of {IJsonRpcError} realizations (e.g. TJsonRpcError).
Shows the type of message detected during Parse
.
Types: jotInvalid
, jotRequest
, jotNotification
, jotSuccess
, jotError
.
Returns one of {TJsonRpcObjectType}.
Returns stored ref to {IJsonRpcMessage}.
Realizes interface {IJsonRpcParsedMessage}.
Create an instance.
Params:
objType
: {TJsonRpcObjectType} message format typepayload
: {IJsonRpcMessage} message body
Returns error code as {Integer}.
Returns error message as {String}.
Returns error Data as {ISuperObject}.
Returns object as {ISuperObject}.
Describes JSON-RPC 2.0 Error object. Realizes interface {IJsonRpcMessage}.
Creates an instance.
Params:
code
: {Integer}message
: {String}data
: {ISuperObject} optional
Examples:
var error: TJsonRpcError;
error = TJsonRpcError.Create(-32651, 'some error', 'some data');
or
var error: TJsonRpcError;
error = TJsonRpcError.Create(-32651, 'some error', SO('{ a: 1, extra: "some data"}'));
Create {TJsonRpcError} object with error code -32600.
Params:
data
: {ISuperObject|nil} - extra data
Create {TJsonRpcError} object with error code -32601.
Params:
data
: {ISuperObject|nil} - extra data
Create {TJsonRpcError} object with error code -32602.
Params:
data
: {ISuperObject|nil} - extra data
Create {TJsonRpcError} object with error code -32603.
Params:
data
: {ISuperObject|nil} - extra data
Create {TJsonRpcError} object with error code -32700.
Params:
data
: {String} - error text
Describes JSON-RPC 2.0 Notification object. Realizes interface {IJsonRpcMessage}.
Creates an instance.
Params:
method
: {String}params
: {TSuperArray|ISuperObject} optional
For TSuperArray use SA() or SO([array, items]).
Returns object as {ISuperObject}
Describes JSON-RPC 2.0 Request object. Realizes interface {IJsonRpcMessage}.
Creates an instance.
Params:
id
: {String}method
: {String}params
: {TSuperArray|ISuperObject} optional
For TSuperArray use SA() or SO([array, items]).
Returns object as {ISuperObject}
Describes JSON-RPC 2.0 Success object. Realizes interface {IJsonRpcMessage}.
Create a {TJsonRpcSuccessObject} instance.
Params:
id
: {String}result
: {ISuperObject}
To place scalar value as result use superobject as wrapper: SO('string value'); SO(921)
Returns object as {ISuperObject}
Describes JSON-RPC 2.0 Error object. Realizes interface {IJsonRpcMessage}.
Creates an instance.
Params:
id
: {String}result
: {ISuperObject}
To place scalar value as result use superobject as wrapper: SO('string value'); SO(921)
Returns object as {ISuperObject}