2018-08-08 17:11:45 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2019-01-08 12:48:27 +01:00
|
|
|
// Copyright (c) 2010-2019 Daniele Teti and the DMVCFramework Team
|
2018-08-08 17:11:45 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
unit MVCFramework.JSONRPC;
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
{
|
2018-11-21 22:11:58 +01:00
|
|
|
JSON-RPC over HTTP implemented as described here
|
|
|
|
https://www.jsonrpc.org/historical/json-rpc-over-http.html
|
2018-08-08 17:11:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2018-05-16 19:46:29 +02:00
|
|
|
System.Classes,
|
|
|
|
Data.DB,
|
|
|
|
System.SysUtils,
|
|
|
|
jsondataobjects,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
System.Rtti,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
MVCFramework.Serializer.jsondataobjects;
|
2017-09-24 19:40:40 +02:00
|
|
|
|
|
|
|
const
|
|
|
|
JSONRPC_VERSION = '2.0';
|
|
|
|
JSONRPC_HEADER = 'jsonrpc';
|
|
|
|
JSONRPC_METHOD = 'method';
|
|
|
|
JSONRPC_PARAMS = 'params';
|
|
|
|
JSONRPC_ID = 'id';
|
|
|
|
JSONRPC_RESULT = 'result';
|
|
|
|
JSONRPC_ERROR = 'error';
|
|
|
|
JSONRPC_CODE = 'code';
|
|
|
|
JSONRPC_MESSAGE = 'message';
|
|
|
|
JSONRPC_DATA = 'data';
|
|
|
|
|
|
|
|
type
|
2017-09-26 01:02:09 +02:00
|
|
|
IMVCJSONRPCMessage = interface
|
|
|
|
['{73B8D463-75E1-404B-8437-EF4B3C950D2F}']
|
|
|
|
function AsJSONRPCMessage: string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TMVCJSONRPCMessage = class abstract(TInterfacedObject, IMVCJSONRPCMessage)
|
2017-09-24 19:40:40 +02:00
|
|
|
private
|
2019-01-08 12:48:27 +01:00
|
|
|
fJSON: TJDOJsonObject;
|
2017-09-24 19:40:40 +02:00
|
|
|
protected
|
2019-01-08 12:48:27 +01:00
|
|
|
class procedure CheckVersion(const aJSON: TJDOJsonObject);
|
|
|
|
class procedure CheckMethod(const aJSON: TJDOJsonObject);
|
|
|
|
class procedure CheckID(const aJSON: TJDOJsonObject; out aIsNotification: Boolean);
|
2017-09-26 01:02:09 +02:00
|
|
|
constructor Create; overload;
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure Build(const aJSON: TJDOJsonObject); virtual; abstract;
|
2017-09-26 01:02:09 +02:00
|
|
|
{ IMVCJSONRPCMessage }
|
|
|
|
function AsJSONRPCMessage: string;
|
2019-01-08 12:48:27 +01:00
|
|
|
function AsJSON: TJDOJsonObject; virtual;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
IJSONRPCObject = interface
|
|
|
|
['{98E161EE-B106-4023-8722-3C2CB1B4CE87}']
|
|
|
|
procedure SetJsonString(const Value: string);
|
|
|
|
function GetJSONString: string;
|
2019-01-08 12:48:27 +01:00
|
|
|
function GetJSON: TJDOJsonObject;
|
|
|
|
procedure SetJSON(const Value: TJDOJsonObject);
|
|
|
|
property AsJSON: TJDOJsonObject read GetJSON write SetJSON;
|
2018-12-17 00:39:29 +01:00
|
|
|
property AsJSONString: string read GetJSONString write SetJsonString;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCObject = class(TInterfacedObject, IJSONRPCObject)
|
2017-09-26 01:02:09 +02:00
|
|
|
protected
|
2017-09-28 00:14:34 +02:00
|
|
|
procedure SetJsonString(const Value: string); virtual;
|
2018-05-16 19:46:29 +02:00
|
|
|
function GetJSONString: string; virtual;
|
2019-01-08 12:48:27 +01:00
|
|
|
function GetJSON: TJDOJsonObject; virtual;
|
|
|
|
procedure SetJSON(const Value: TJDOJsonObject); virtual;
|
|
|
|
property AsJSON: TJDOJsonObject read GetJSON write SetJSON;
|
2017-09-28 00:14:34 +02:00
|
|
|
property AsJSONString: string read GetJSONString write SetJsonString;
|
2018-12-17 00:39:29 +01:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
TJSONRPCParamDataType = (pdtString, pdtInteger, pdtLongInteger, pdTJDOJsonObject, pdtJSONArray, pdtBoolean, pdtDate,
|
|
|
|
pdtTime, pdtDateTime, pdtFloat, pdtObject);
|
|
|
|
|
|
|
|
TJSONRPCRequestParams = class
|
|
|
|
private
|
|
|
|
function GetItem(const Index: Integer): TValue;
|
|
|
|
function GetItemDataType(const Index: Integer): TJSONRPCParamDataType;
|
|
|
|
protected
|
|
|
|
FParamsValue: TList<TValue>;
|
|
|
|
FParamsType: TList<TJSONRPCParamDataType>;
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure Clear;
|
|
|
|
function Count: Integer;
|
|
|
|
property Items[const Index: Integer]: TValue read GetItem; default;
|
|
|
|
property ItemsType[const Index: Integer]: TJSONRPCParamDataType read GetItemDataType;
|
|
|
|
function ToArray: TArray<TValue>;
|
|
|
|
procedure Add(const Value: String); overload;
|
|
|
|
procedure Add(const Value: Integer); overload;
|
|
|
|
procedure Add(const Value: TJDOJsonObject); overload;
|
|
|
|
procedure Add(const Value: TJDOJsonArray); overload;
|
|
|
|
procedure Add(const Value: Boolean); overload;
|
|
|
|
procedure Add(const Value: TDate); overload;
|
|
|
|
procedure Add(const Value: TTime); overload;
|
|
|
|
procedure Add(const Value: TDateTime); overload;
|
|
|
|
procedure Add(const Value: Double); overload;
|
|
|
|
procedure Add(const Value: TValue; const ParamType: TJSONRPCParamDataType); overload;
|
|
|
|
end;
|
2018-12-17 00:39:29 +01:00
|
|
|
|
|
|
|
IJSONRPCNotification = interface(IJSONRPCObject)
|
|
|
|
['{FAA65A29-3305-4303-833E-825BDBD3FF7F}']
|
|
|
|
procedure SetMethod(const Value: string);
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure FillParameters(const JSON: TJDOJsonObject; const RTTIMethod: TRTTIMethod);
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetMethod: string;
|
|
|
|
function GetParams: TJSONRPCRequestParams;
|
|
|
|
property Method: string read GetMethod write SetMethod;
|
|
|
|
property Params: TJSONRPCRequestParams read GetParams;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCNotification = class(TJSONRPCObject, IJSONRPCObject, IJSONRPCNotification)
|
2018-05-16 19:46:29 +02:00
|
|
|
protected
|
2017-09-26 01:02:09 +02:00
|
|
|
FMethod: string;
|
2018-05-16 19:46:29 +02:00
|
|
|
FParams: TJSONRPCRequestParams;
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure FillParameters(const JSON: TJDOJsonObject; const RTTIMethod: TRTTIMethod);
|
2017-09-26 01:02:09 +02:00
|
|
|
procedure SetMethod(const Value: string);
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetMethod: string;
|
|
|
|
function GetParams: TJSONRPCRequestParams;
|
2019-01-08 12:48:27 +01:00
|
|
|
function GetJSON: TJDOJsonObject; override;
|
2018-12-17 00:39:29 +01:00
|
|
|
property Method: string read GetMethod write SetMethod;
|
|
|
|
property Params: TJSONRPCRequestParams read GetParams;
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2018-12-17 00:39:29 +01:00
|
|
|
constructor Create; overload; override;
|
|
|
|
constructor Create(const aMethod: String); reintroduce; overload;
|
2017-09-26 01:02:09 +02:00
|
|
|
destructor Destroy; override;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
{$SCOPEDENUMS ON}
|
|
|
|
|
|
|
|
TJSONRPCRequestType = (Request, Notification);
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
IJSONRPCRequest = interface(IJSONRPCNotification)
|
|
|
|
['{D8318032-0261-4273-B99D-121899AD52FB}']
|
|
|
|
function GetRequestType: TJSONRPCRequestType;
|
|
|
|
function GetID: TValue;
|
|
|
|
procedure SetID(const Value: TValue);
|
|
|
|
property RequestType: TJSONRPCRequestType read GetRequestType;
|
|
|
|
property RequestID: TValue read GetID write SetID;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCRequest = class(TJSONRPCNotification, IJSONRPCRequest)
|
2017-09-24 19:40:40 +02:00
|
|
|
private
|
2018-05-16 19:46:29 +02:00
|
|
|
FID: TValue;
|
|
|
|
function GetRequestType: TJSONRPCRequestType;
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetID: TValue;
|
2018-05-16 19:46:29 +02:00
|
|
|
protected
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure SetJSON(const JSON: TJDOJsonObject); override;
|
|
|
|
function GetJSON: TJDOJsonObject; override;
|
2018-05-16 19:46:29 +02:00
|
|
|
procedure SetID(const Value: TValue);
|
2018-12-17 00:39:29 +01:00
|
|
|
property RequestType: TJSONRPCRequestType read GetRequestType;
|
|
|
|
property RequestID: TValue read GetID write SetID;
|
2018-05-16 19:46:29 +02:00
|
|
|
public
|
2018-12-17 00:39:29 +01:00
|
|
|
constructor Create(const aID: TValue; const aMethod: String); overload; virtual;
|
|
|
|
constructor Create(const aID: TValue); overload; virtual;
|
|
|
|
constructor Create; reintroduce; overload; virtual;
|
2018-05-16 19:46:29 +02:00
|
|
|
destructor Destroy; override;
|
2018-12-17 00:39:29 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCResponseError = class
|
2017-09-26 01:02:09 +02:00
|
|
|
private
|
2018-12-17 00:39:29 +01:00
|
|
|
FCode: Integer;
|
|
|
|
FMessage: string;
|
|
|
|
procedure SetCode(const Value: Integer);
|
|
|
|
procedure SetMessage(const Value: string);
|
|
|
|
public
|
|
|
|
property Code: Integer read FCode write SetCode;
|
|
|
|
property ErrMessage: string read FMessage write SetMessage;
|
|
|
|
end;
|
|
|
|
|
|
|
|
IJSONRPCResponse = interface(IJSONRPCObject)
|
|
|
|
['{69B43409-14DC-4A36-9E12-425A1626FF3C}']
|
|
|
|
function GetID: TValue;
|
2018-05-16 19:46:29 +02:00
|
|
|
procedure SetID(const Value: TValue);
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetResult: TValue;
|
2017-09-26 01:02:09 +02:00
|
|
|
procedure SetResult(const Value: TValue);
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetError: TJSONRPCResponseError;
|
2017-09-26 01:02:09 +02:00
|
|
|
procedure SetError(const Value: TJSONRPCResponseError);
|
2018-12-17 00:39:29 +01:00
|
|
|
function IsError: Boolean;
|
2019-01-08 12:48:27 +01:00
|
|
|
function ResultAsJSONObject: TJDOJsonObject;
|
|
|
|
function ResultAsJSONArray: TJDOJsonArray;
|
2018-12-17 00:39:29 +01:00
|
|
|
property Result: TValue read GetResult write SetResult;
|
|
|
|
property Error: TJSONRPCResponseError read GetError write SetError;
|
|
|
|
property RequestID: TValue read GetID write SetID;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCResponse = class(TJSONRPCObject, IJSONRPCResponse)
|
|
|
|
private
|
|
|
|
FResult: TValue;
|
|
|
|
FError: TJSONRPCResponseError;
|
|
|
|
FID: TValue;
|
|
|
|
function GetResult: TValue;
|
2017-09-24 19:40:40 +02:00
|
|
|
protected
|
2019-01-08 12:48:27 +01:00
|
|
|
function GetJSON: TJDOJsonObject; override;
|
|
|
|
procedure SetJSON(const JSON: TJDOJsonObject); override;
|
2018-12-17 00:39:29 +01:00
|
|
|
procedure SetID(const Value: TValue);
|
|
|
|
procedure SetResult(const Value: TValue);
|
|
|
|
procedure SetError(const Value: TJSONRPCResponseError);
|
|
|
|
function GetError: TJSONRPCResponseError;
|
|
|
|
function GetID: TValue;
|
2019-01-08 12:48:27 +01:00
|
|
|
function ResultAsJSONObject: TJDOJsonObject;
|
|
|
|
function ResultAsJSONArray: TJDOJsonArray;
|
2018-12-17 00:39:29 +01:00
|
|
|
function IsError: Boolean;
|
|
|
|
property Result: TValue read GetResult write SetResult;
|
|
|
|
property Error: TJSONRPCResponseError read GetError write SetError;
|
|
|
|
property RequestID: TValue read GetID write SetID;
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2017-09-26 01:02:09 +02:00
|
|
|
constructor Create; override;
|
2017-09-24 19:40:40 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
EMVCJSONRPCInvalidVersion = class(Exception)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
EMVCJSONRPCException = class(Exception)
|
2017-09-26 01:02:09 +02:00
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
EMVCJSONRPCErrorResponse = class abstract(Exception)
|
2017-09-24 19:40:40 +02:00
|
|
|
private
|
|
|
|
FJSONRPCErrorCode: Integer;
|
|
|
|
public
|
|
|
|
property JSONRPCErrorCode: Integer read FJSONRPCErrorCode;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCParseError = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCInvalidRequest = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor Create(const Message: String = ''); overload;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCMethodNotFound = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2018-08-08 17:11:45 +02:00
|
|
|
constructor Create(const MethodName: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCInvalidParams = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor Create(const Message: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCInternalError = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ -32000 to -32099 Server error Reserved for implementation-defined server-errors. }
|
2017-09-26 01:02:09 +02:00
|
|
|
EMVCJSONRPCServerError = class(EMVCJSONRPCErrorResponse)
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor Create(const JSONRPCError: Integer; const Message: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
TMVCJSONObject = TJDOJsonObject;
|
2017-09-24 19:40:40 +02:00
|
|
|
TMVCJSONArray = TJDOJsonArray;
|
|
|
|
|
|
|
|
TMVCJSONRPCController = class(TMVCController)
|
2017-10-10 12:19:46 +02:00
|
|
|
private
|
|
|
|
fSerializer: TMVCJsonDataObjectsSerializer;
|
2018-11-21 22:11:58 +01:00
|
|
|
fRPCInstance: TObject;
|
|
|
|
fOwsRPCInstance: Boolean;
|
2017-10-10 12:19:46 +02:00
|
|
|
function GetSerializer: TMVCJsonDataObjectsSerializer;
|
2019-01-08 12:48:27 +01:00
|
|
|
// procedure CheckInputParametersTypes(aRTTIMethod: TRTTIMethod);
|
2017-09-24 19:40:40 +02:00
|
|
|
protected
|
2019-01-08 12:48:27 +01:00
|
|
|
function CreateError(const RequestID: TValue; const ErrorCode: Integer; const Message: string): TJDOJsonObject;
|
2017-09-28 00:14:34 +02:00
|
|
|
function CreateResponse(const RequestID: TValue; const Value: TValue): TJSONRPCResponse;
|
2019-01-08 12:48:27 +01:00
|
|
|
function CreateRequest(const JSON: TJDOJsonObject): IJSONRPCRequest;
|
|
|
|
function JSONObjectAs<T: class, constructor>(const JSON: TJDOJsonObject): T;
|
2017-09-24 19:40:40 +02:00
|
|
|
public
|
|
|
|
[MVCPath]
|
|
|
|
[MVCHTTPMethods([httpPOST])]
|
|
|
|
[MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
|
|
|
|
[MVCProduces(TMVCMediaType.APPLICATION_JSON)]
|
2018-08-08 17:11:45 +02:00
|
|
|
procedure Index; virtual;
|
2018-12-17 00:39:29 +01:00
|
|
|
|
|
|
|
[MVCPath]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_PLAIN)]
|
|
|
|
procedure GetProxyCode; virtual;
|
2018-11-21 22:11:58 +01:00
|
|
|
constructor Create; overload; override;
|
2017-10-10 12:19:46 +02:00
|
|
|
destructor Destroy; override;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
TMVCJSONRPCPublisher = class(TMVCJSONRPCController)
|
|
|
|
public
|
|
|
|
constructor Create(const RPCInstance: TObject; const Owns: Boolean = True); reintroduce; overload;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
TJSONRPCProxyGenerator = class abstract
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
procedure StartGeneration; virtual;
|
|
|
|
procedure EndGeneration; virtual;
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure VisitMethod(const aRTTIMethod: TRTTIMethod); virtual; abstract;
|
2018-12-17 00:39:29 +01:00
|
|
|
function GetCode: String; virtual; abstract;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJSONRPCProxyGeneratorClass = class of TJSONRPCProxyGenerator;
|
|
|
|
|
|
|
|
procedure RegisterJSONRPCProxyGenerator(const aLanguage: String; const aClass: TJSONRPCProxyGeneratorClass);
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
implementation
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
uses
|
2018-12-09 23:03:06 +01:00
|
|
|
MVCFramework.Serializer.Intf, MVCFramework.Logger,
|
2018-12-12 11:00:41 +01:00
|
|
|
System.TypInfo, MVCFramework.DuckTyping,
|
2018-12-17 00:39:29 +01:00
|
|
|
MVCFramework.Serializer.jsondataobjects.CustomTypes;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
2018-12-12 22:29:45 +01:00
|
|
|
const
|
2018-12-17 00:39:29 +01:00
|
|
|
CALL_TYPE: array [mkProcedure .. mkFunction] of string = ('PROCEDURE', 'FUNCTION');
|
|
|
|
|
|
|
|
var
|
|
|
|
GProxyGeneratorsRegister: TDictionary<String, TJSONRPCProxyGeneratorClass>;
|
2018-12-12 22:29:45 +01:00
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure AppendTValueToJsonArray(const Value: TValue; const ParamType: TJSONRPCParamDataType;
|
|
|
|
const JSONArr: TJDOJsonArray);
|
|
|
|
var
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
lJArr: TJDOJsonArray;
|
|
|
|
LJObj: TJDOJsonObject;
|
|
|
|
lOrdinalValue: Int64;
|
|
|
|
begin
|
|
|
|
case ParamType of
|
|
|
|
pdtInteger:
|
|
|
|
begin
|
|
|
|
JSONArr.Add(Value.AsInteger);
|
|
|
|
end;
|
|
|
|
pdtFloat:
|
|
|
|
begin
|
|
|
|
JSONArr.Add(Value.AsExtended);
|
|
|
|
end;
|
|
|
|
pdtDateTime:
|
|
|
|
begin
|
2019-01-16 14:23:14 +01:00
|
|
|
JSONArr.Add(DateTimeToISOTimeStamp(FloatToDateTime(Value.AsExtended)));
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
pdtDate:
|
|
|
|
begin
|
2019-01-18 18:11:27 +01:00
|
|
|
JSONArr.Add(DateToISODate(FloatToDateTime(Value.AsExtended)));
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
pdtTime:
|
|
|
|
begin
|
2019-01-18 18:11:27 +01:00
|
|
|
JSONArr.Add(TimeToISOTime(FloatToDateTime(Value.AsExtended)));
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
pdtString:
|
|
|
|
begin
|
|
|
|
JSONArr.Add(Value.AsString);
|
|
|
|
end;
|
|
|
|
pdtLongInteger:
|
|
|
|
begin
|
|
|
|
JSONArr.Add(Value.AsInt64);
|
|
|
|
end;
|
|
|
|
pdtBoolean:
|
|
|
|
begin
|
|
|
|
if not Value.TryAsOrdinal(lOrdinalValue) then
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create('Invalid ordinal parameter');
|
|
|
|
end;
|
|
|
|
JSONArr.Add(lOrdinalValue = 1);
|
|
|
|
end;
|
|
|
|
pdTJDOJsonObject:
|
|
|
|
begin
|
|
|
|
JSONArr.Add((Value.AsObject as TJDOJsonObject).Clone as TJDOJsonObject);
|
|
|
|
end;
|
|
|
|
pdtJSONArray:
|
|
|
|
begin
|
|
|
|
JSONArr.Add((Value.AsObject as TJDOJsonArray).Clone as TJDOJsonArray);
|
|
|
|
end;
|
|
|
|
pdtObject:
|
|
|
|
begin
|
|
|
|
if Value.AsObject is TDataSet then
|
|
|
|
begin
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
|
|
|
lJArr := TJDOJsonArray.Create;
|
|
|
|
JSONArr.Add(lJArr);
|
|
|
|
lSer.DataSetToJsonArray(TDataSet(Value.AsObject), lJArr, TMVCNameCase.ncLowerCase, []);
|
|
|
|
finally
|
|
|
|
lSer.Free;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
|
|
|
LJObj := lSer.SerializeObjectToJSON(Value.AsObject, TMVCSerializationType.stProperties, [], nil);
|
|
|
|
JSONArr.Add(LJObj);
|
|
|
|
finally
|
|
|
|
lSer.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
raise EMVCException.Create('Invalid type');
|
|
|
|
end;
|
|
|
|
|
|
|
|
//
|
|
|
|
// case Value.Kind of
|
|
|
|
// tkInteger:
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(Value.AsInteger);
|
|
|
|
// end;
|
|
|
|
// tkFloat:
|
|
|
|
// begin
|
|
|
|
// if Value.IsType(TypeInfo(System.TDateTime), False) then
|
|
|
|
// begin
|
|
|
|
// lIsDate := Frac(Value.AsExtended) = 0;
|
|
|
|
// lIsTime := Trunc(Value.AsExtended) = 0;
|
|
|
|
// if lIsDate then
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(DateToISODate(Value.AsExtended));
|
|
|
|
// end
|
|
|
|
// else if lIsTime then
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(TimeToISOTime(Value.AsExtended));
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(DateTimeToISOTimeStamp(Value.AsExtended));
|
|
|
|
// end;
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(Value.AsExtended);
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// tkString, tkUString, tkWChar, tkLString, tkWString:
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(Value.AsString);
|
|
|
|
// end;
|
|
|
|
// tkInt64:
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(Value.AsInt64);
|
|
|
|
// end;
|
|
|
|
// tkEnumeration:
|
|
|
|
// begin
|
|
|
|
// if not Value.TryAsOrdinal(lOrdinalValue) then
|
|
|
|
// begin
|
|
|
|
// raise EMVCException.Create('Invalid ordinal parameter');
|
|
|
|
// end;
|
|
|
|
// if Value.IsType(TypeInfo(System.Boolean)) then
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(lOrdinalValue = 1);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// JSONArr.Add(lOrdinalValue);
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// tkClass:
|
|
|
|
// begin
|
|
|
|
// if Value.AsObject is TJDOJsonObject then
|
|
|
|
// begin
|
|
|
|
// LJObj := TJDOJsonObject.Create;
|
|
|
|
// JSONArr.Add(LJObj);
|
|
|
|
// LJObj.Assign(TJDOJsonObject(Value.AsObject));
|
|
|
|
// end
|
|
|
|
// else if Value.AsObject is TJDOJsonArray then
|
|
|
|
// begin
|
|
|
|
// lJArr := TJDOJsonArray.Create;
|
|
|
|
// JSONArr.Add(lJArr);
|
|
|
|
// lJArr.Assign(TJDOJsonArray(Value.AsObject));
|
|
|
|
// end
|
|
|
|
// else if Value.AsObject is TDataSet then
|
|
|
|
// begin
|
|
|
|
// lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
// try
|
|
|
|
// lJArr := TJDOJsonArray.Create;
|
|
|
|
// JSONArr.Add(lJArr);
|
|
|
|
// lSer.DataSetToJsonArray(TDataSet(Value.AsObject), lJArr, TMVCNameCase.ncLowerCase, []);
|
|
|
|
// finally
|
|
|
|
// lSer.Free;
|
|
|
|
// end
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
// try
|
|
|
|
// LJObj := lSer.SerializeObjectToJSON(Value.AsObject, TMVCSerializationType.stProperties, [], nil);
|
|
|
|
// JSONArr.Add(LJObj);
|
|
|
|
// finally
|
|
|
|
// lSer.Free;
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// else
|
|
|
|
// raise EMVCException.Create('Invalid type');
|
|
|
|
// end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function JSONDataValueToTValue(const JSONDataValue: TJsonDataValueHelper): TValue; overload;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
case JSONDataValue.Typ of
|
|
|
|
jdtString:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.Value;
|
|
|
|
end;
|
|
|
|
jdtFloat:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.FloatValue;
|
|
|
|
end;
|
|
|
|
jdtBool:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.BoolValue;
|
|
|
|
end;
|
|
|
|
jdtArray:
|
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Result := JSONDataValue.ArrayValue.Clone as TJDOJsonArray;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
jdtObject:
|
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Result := JSONDataValue.ObjectValue.Clone as TJDOJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
jdtInt:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.IntValue;
|
|
|
|
end;
|
|
|
|
jdtLong:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.LongValue;
|
|
|
|
end;
|
|
|
|
jdtULong:
|
|
|
|
begin
|
|
|
|
Result := JSONDataValue.ULongValue;
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
raise EMVCJSONRPCException.Create('Invalid parameter type');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function BuildDeclaration(const RTTIParameter: TRttiParameter): String;
|
|
|
|
begin
|
|
|
|
Result := RTTIParameter.Name + ': ' + RTTIParameter.ParamType.Name;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure JSONDataValueToTValueParam(const JSONDataValue: TJsonDataValueHelper; const RTTIParameter: TRttiParameter;
|
|
|
|
const JSONRPCRequestParams: TJSONRPCRequestParams);
|
|
|
|
begin
|
|
|
|
case RTTIParameter.ParamType.TypeKind of
|
|
|
|
tkString, tkUString, tkAnsiString:
|
|
|
|
begin
|
|
|
|
if JSONDataValue.Typ <> jdtString then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidParams.Create('Invalid param type for [' + BuildDeclaration(RTTIParameter) + ']');
|
|
|
|
end;
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.Value);
|
|
|
|
end;
|
|
|
|
tkFloat:
|
|
|
|
begin
|
|
|
|
if SameText(RTTIParameter.ParamType.Name, 'TDate') then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(ISODateToDate(JSONDataValue.Value), pdtDate);
|
|
|
|
end
|
|
|
|
else if SameText(RTTIParameter.ParamType.Name, 'TDateTime') then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.UtcDateTimeValue, pdtDateTime);
|
|
|
|
end
|
|
|
|
else if SameText(RTTIParameter.ParamType.Name, 'TTime') then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(ISOTimeToTime(JSONDataValue.Value), pdtTime);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if JSONDataValue.Typ <> jdtFloat then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.FloatValue, pdtFloat);
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
tkEnumeration:
|
|
|
|
begin
|
|
|
|
if JSONDataValue.Typ <> jdtBool then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.BoolValue, pdtBoolean);
|
|
|
|
end;
|
|
|
|
tkClass:
|
|
|
|
begin
|
|
|
|
if (SameText(RTTIParameter.ParamType.Name, TJDOJsonArray.ClassName)) then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.ArrayValue.Clone, pdtJSONArray);
|
|
|
|
end
|
|
|
|
else if SameText(RTTIParameter.ParamType.Name, TJDOJsonObject.ClassName) then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.ObjectValue.Clone as TJDOJsonObject, pdTJDOJsonObject);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
{ TODO -oDanieleT -cGeneral : Automatically inject the dseserialized version of arbitrary object? }
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
tkInteger:
|
|
|
|
begin
|
|
|
|
if JSONDataValue.Typ <> jdtInt then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.IntValue, pdtInteger);
|
|
|
|
end;
|
|
|
|
tkInt64:
|
|
|
|
begin
|
|
|
|
if JSONDataValue.Typ = jdtInt then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.IntValue, pdtInteger);
|
|
|
|
end
|
|
|
|
else if JSONDataValue.Typ = jdtULong then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.ULongValue, pdtLongInteger);
|
|
|
|
end
|
|
|
|
else if JSONDataValue.Typ = jdtULong then
|
|
|
|
begin
|
|
|
|
JSONRPCRequestParams.Add(JSONDataValue.ULongValue, pdtLongInteger);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create('Invalid parameter type for ' + BuildDeclaration(RTTIParameter));
|
|
|
|
end;
|
|
|
|
|
|
|
|
// case JSONDataValue.Typ of
|
|
|
|
// jdtString:
|
|
|
|
// begin
|
|
|
|
// if RTTIParameter.ParamType.TypeKind in [tkString, tkUString, tkUnicodeString, tkAnsiString] then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.Value);
|
|
|
|
// end
|
|
|
|
// else if SameText(RTTIParameter.ParamType.Name, 'TDate') then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.UtcDateTimeValue, pdtDate);
|
|
|
|
// end
|
|
|
|
// else if SameText(RTTIParameter.ParamType.Name, 'TDateTime') then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.UtcDateTimeValue, pdtDateTime);
|
|
|
|
// end
|
|
|
|
// else if SameText(RTTIParameter.ParamType.Name, 'TTime') then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.UtcDateTimeValue, pdtTime);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtFloat:
|
|
|
|
// begin
|
|
|
|
// if RTTIParameter.ParamType.TypeKind in [tkFloat] then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.FloatValue, pdtFloat);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtBool:
|
|
|
|
// begin
|
|
|
|
// if (RTTIParameter.ParamType.TypeKind in [tkEnumeration]) and
|
|
|
|
// (SameText(RTTIParameter.ParamType.QualifiedName, 'System.Boolean')) then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.BoolValue, pdtBoolean);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtArray:
|
|
|
|
// begin
|
|
|
|
// if (RTTIParameter.ParamType.TypeKind in [tkClass]) and
|
|
|
|
// (SameText(RTTIParameter.ParamType.Name, TJDOJsonArray.ClassName)) then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.ArrayValue.Clone, pdtJSONArray);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtObject:
|
|
|
|
// begin
|
|
|
|
// if (RTTIParameter.ParamType.TypeKind in [tkClass]) and
|
|
|
|
// (SameText(RTTIParameter.ParamType.Name, TJDOJsonObject.ClassName)) then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.ObjectValue.Clone as TJDOJsonObject, pdTJDOJsonObject);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtInt:
|
|
|
|
// begin
|
|
|
|
// if RTTIParameter.ParamType.TypeKind in [tkInteger] then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.IntValue, pdtInteger);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtLong:
|
|
|
|
// begin
|
|
|
|
// if RTTIParameter.ParamType.TypeKind in [tkInt64] then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.LongValue, pdtLongInteger);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// jdtULong:
|
|
|
|
// begin
|
|
|
|
// if RTTIParameter.ParamType.TypeKind in [tkInt64] then
|
|
|
|
// begin
|
|
|
|
// JSONRPCRequestParams.Add(JSONDataValue.ULongValue, pdtLongInteger);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create(BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
// else
|
|
|
|
// raise EMVCJSONRPCInvalidRequest.Create('Invalid parameter type for ' + BuildDeclaration(RTTIParameter));
|
|
|
|
// end;
|
|
|
|
end;
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
{ TMVCJSONRPCMessage }
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TMVCJSONRPCMessage.AsJSON: TJDOJsonObject;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
Result := TMVCJSONObject.Create;
|
|
|
|
Result.S[JSONRPC_HEADER] := JSONRPC_VERSION;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
function TMVCJSONRPCMessage.AsJSONRPCMessage: string;
|
|
|
|
begin
|
|
|
|
Result := fJSON.ToJSON();
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
class procedure TMVCJSONRPCMessage.CheckID(const aJSON: TMVCJSONObject; out aIsNotification: Boolean);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
{
|
|
|
|
id
|
|
|
|
An identifier established by the Client that MUST contain a String, Number, or NULL value if included.
|
|
|
|
If it is not included it is assumed to be a notification.
|
|
|
|
The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain fractional parts [2]
|
|
|
|
}
|
|
|
|
aIsNotification := not aJSON.Contains(JSONRPC_ID);
|
|
|
|
if not aIsNotification then
|
|
|
|
begin
|
|
|
|
if not(aJSON.Types[JSONRPC_ID] in [jdtString, jdtInt, jdtLong, jdtULong, jdtNone]) then
|
2017-09-26 01:02:09 +02:00
|
|
|
raise EMVCJSONRPCException.Create('Message is not a notification but its ''id'' property is not valid');
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
class procedure TMVCJSONRPCMessage.CheckMethod(const aJSON: TMVCJSONObject);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
if (aJSON.Types[JSONRPC_METHOD] <> jdtString) then
|
|
|
|
raise EMVCJSONRPCException.Create('Invalid ''method''');
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
class procedure TMVCJSONRPCMessage.CheckVersion(const aJSON: TMVCJSONObject);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
if not Assigned(aJSON) then
|
|
|
|
raise EMVCJSONRPCException.Create('JSON not assigned');
|
|
|
|
if aJSON.S[JSONRPC_HEADER] <> JSONRPC_VERSION then
|
|
|
|
raise EMVCJSONRPCInvalidVersion.Create(JSONRPC_HEADER + ' must be "2.0"');
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
constructor TMVCJSONRPCMessage.Create;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
{ TMVCJSONRPCController }
|
2017-09-24 19:40:40 +02:00
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
constructor TMVCJSONRPCPublisher.Create(const RPCInstance: TObject; const Owns: Boolean);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fRPCInstance := RPCInstance;
|
|
|
|
fOwsRPCInstance := Owns;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
// procedure TMVCJSONRPCController.CheckInputParametersTypes(aRTTIMethod: TRTTIMethod);
|
|
|
|
// var
|
|
|
|
// lParam: TRttiParameter;
|
|
|
|
// begin
|
|
|
|
// for lParam in aRTTIMethod.GetParameters do
|
|
|
|
// begin
|
|
|
|
// if lParam.ParamType.TypeKind in [tkClass] then
|
|
|
|
// begin
|
|
|
|
// if not(SameText(lParam.ParamType.QualifiedName, 'JsonDataObjects.TJDOJsonObject') or
|
|
|
|
// SameText(lParam.ParamType.QualifiedClassName, 'JsonDataObjects.TJDOJsonArray')) then
|
|
|
|
// begin
|
|
|
|
// raise EMVCJSONRPCException.Create('Parameter [' + lParam.Name + ': ' + lParam.ParamType.QualifiedName +
|
|
|
|
// '] is not allowed as input parameter');
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
//
|
|
|
|
// end;
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
constructor TMVCJSONRPCController.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fRPCInstance := Self;
|
|
|
|
fOwsRPCInstance := False;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TMVCJSONRPCController.CreateError(const RequestID: TValue; const ErrorCode: Integer; const Message: string)
|
|
|
|
: TJDOJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
var
|
2017-09-28 00:14:34 +02:00
|
|
|
lErrResp: TJSONRPCResponse;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2017-09-28 00:14:34 +02:00
|
|
|
lErrResp := TJSONRPCResponse.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
try
|
2017-10-10 12:19:46 +02:00
|
|
|
lErrResp.RequestID := RequestID;
|
2018-12-17 00:39:29 +01:00
|
|
|
lErrResp.Error := TJSONRPCResponseError.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
lErrResp.Error.Code := ErrorCode;
|
2017-09-28 00:14:34 +02:00
|
|
|
lErrResp.Error.ErrMessage := message;
|
2017-09-26 01:02:09 +02:00
|
|
|
Result := lErrResp.AsJSON;
|
|
|
|
finally
|
|
|
|
lErrResp.Free;
|
|
|
|
end;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TMVCJSONRPCController.CreateRequest(const JSON: TJDOJsonObject): IJSONRPCRequest;
|
2017-09-26 01:02:09 +02:00
|
|
|
var
|
2018-12-17 00:39:29 +01:00
|
|
|
lReqID: TValue;
|
|
|
|
lMethodName: String;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
if JSON.Types[JSONRPC_ID] = jdtString then
|
|
|
|
lReqID := JSON.S[JSONRPC_ID]
|
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtInt then
|
|
|
|
lReqID := JSON.I[JSONRPC_ID]
|
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtLong then
|
|
|
|
lReqID := JSON.L[JSONRPC_ID]
|
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtULong then
|
|
|
|
lReqID := JSON.U[JSONRPC_ID]
|
|
|
|
else
|
|
|
|
lReqID := TValue.Empty;
|
2017-09-26 01:02:09 +02:00
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
lMethodName := JSON.S[JSONRPC_METHOD];
|
|
|
|
Result := TJSONRPCRequest.Create(lReqID, lMethodName);
|
|
|
|
{
|
2017-09-28 00:14:34 +02:00
|
|
|
if JSON.Types[JSONRPC_PARAMS] = jdtArray then
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
lParams := JSON.A[JSONRPC_PARAMS];
|
|
|
|
for I := 0 to lParams.Count - 1 do
|
|
|
|
begin
|
|
|
|
Result.Params.Add(JSONDataValueToTValue(lParams[I]));
|
|
|
|
end;
|
2017-09-28 00:14:34 +02:00
|
|
|
end
|
|
|
|
else if JSON.Types[JSONRPC_PARAMS] <> jdtNone then
|
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
raise EMVCJSONRPCException.Create('Params must be a JSON array or null');
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
}
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
function TMVCJSONRPCController.CreateResponse(const RequestID: TValue; const Value: TValue): TJSONRPCResponse;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2017-09-28 00:14:34 +02:00
|
|
|
Result := TJSONRPCResponse.Create;
|
2017-10-10 12:19:46 +02:00
|
|
|
Result.RequestID := RequestID;
|
2017-09-26 01:02:09 +02:00
|
|
|
Result.Result := Value;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
|
2017-10-10 12:19:46 +02:00
|
|
|
destructor TMVCJSONRPCController.Destroy;
|
|
|
|
begin
|
2018-11-21 22:11:58 +01:00
|
|
|
if Assigned(fRPCInstance) and fOwsRPCInstance then
|
|
|
|
begin
|
|
|
|
fRPCInstance.Free;
|
|
|
|
end;
|
2017-10-10 12:19:46 +02:00
|
|
|
fSerializer.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
procedure TMVCJSONRPCController.GetProxyCode;
|
|
|
|
var
|
|
|
|
lLanguage: string;
|
|
|
|
lClass: TJSONRPCProxyGeneratorClass;
|
|
|
|
lGenerator: TJSONRPCProxyGenerator;
|
|
|
|
lRTTI: TRTTIContext;
|
|
|
|
lRTTIType: TRttiType;
|
2019-01-08 12:48:27 +01:00
|
|
|
lMethod: TRTTIMethod;
|
2018-12-17 00:39:29 +01:00
|
|
|
begin
|
|
|
|
if not Context.Request.QueryStringParamExists('language') then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCException.Create('Query string parameter "language" is required');
|
|
|
|
end;
|
|
|
|
|
|
|
|
lLanguage := Context.Request.Params['language'].ToLower;
|
|
|
|
|
|
|
|
if not Assigned(GProxyGeneratorsRegister) then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCException.Create
|
|
|
|
('No Proxy Generators have been registered. [HINT] Use RegisterJSONRPCProxyGenerator function');
|
|
|
|
end;
|
|
|
|
|
|
|
|
if not GProxyGeneratorsRegister.TryGetValue(lLanguage, lClass) then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCException.CreateFmt('Unknown language [%s]', [lLanguage]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
lGenerator := lClass.Create;
|
|
|
|
try
|
|
|
|
lRTTI := TRTTIContext.Create;
|
|
|
|
try
|
|
|
|
lRTTIType := lRTTI.GetType(fRPCInstance.ClassType);
|
|
|
|
lGenerator.StartGeneration();
|
|
|
|
for lMethod in lRTTIType.GetMethods do
|
|
|
|
begin
|
|
|
|
lGenerator.VisitMethod(lMethod);
|
|
|
|
end;
|
|
|
|
lGenerator.EndGeneration();
|
|
|
|
Context.Response.ContentType := 'text/plain';
|
|
|
|
Render(lGenerator.GetCode);
|
|
|
|
finally
|
|
|
|
lRTTI.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lGenerator.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-10-10 12:19:46 +02:00
|
|
|
function TMVCJSONRPCController.GetSerializer: TMVCJsonDataObjectsSerializer;
|
|
|
|
begin
|
|
|
|
if not Assigned(fSerializer) then
|
|
|
|
fSerializer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
Result := fSerializer;
|
|
|
|
end;
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
procedure TMVCJSONRPCController.Index;
|
2017-09-24 19:40:40 +02:00
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSONRPCReq: IJSONRPCRequest;
|
2017-09-24 19:40:40 +02:00
|
|
|
lMethod: string;
|
|
|
|
lRTTI: TRTTIContext;
|
|
|
|
lRTTIType: TRttiType;
|
2019-01-08 12:48:27 +01:00
|
|
|
lRTTIMethod: TRTTIMethod;
|
2017-09-24 19:40:40 +02:00
|
|
|
lRes: TValue;
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSONRPCResponse: IJSONRPCResponse;
|
2017-09-24 19:40:40 +02:00
|
|
|
lParamsToInject: TArray<TValue>;
|
2017-09-26 01:02:09 +02:00
|
|
|
lReqID: TValue;
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSON: TJDOJsonObject;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2017-09-26 01:02:09 +02:00
|
|
|
lReqID := TValue.Empty;
|
2017-09-24 19:40:40 +02:00
|
|
|
SetLength(lParamsToInject, 0);
|
|
|
|
try
|
2018-11-21 22:11:58 +01:00
|
|
|
lJSON := StringToJSON(Context.Request.Body);
|
2017-09-24 19:40:40 +02:00
|
|
|
try
|
2019-01-08 12:48:27 +01:00
|
|
|
if not Assigned(lJSON) then
|
|
|
|
raise EMVCJSONRPCParseError.Create;
|
|
|
|
lJSONRPCReq := CreateRequest(lJSON);
|
2017-09-26 01:02:09 +02:00
|
|
|
lMethod := lJSONRPCReq.Method;
|
2018-05-16 19:46:29 +02:00
|
|
|
|
|
|
|
if lJSONRPCReq.RequestType = TJSONRPCRequestType.Request then
|
|
|
|
begin
|
|
|
|
if lJSONRPCReq.RequestID.IsEmpty then
|
|
|
|
raise EMVCJSONRPCInvalidRequest.Create;
|
|
|
|
lReqID := lJSONRPCReq.RequestID;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
lRTTI := TRTTIContext.Create;
|
|
|
|
try
|
2018-11-21 22:11:58 +01:00
|
|
|
lRTTIType := lRTTI.GetType(fRPCInstance.ClassType);
|
2017-09-26 01:02:09 +02:00
|
|
|
lRTTIMethod := lRTTIType.GetMethod(lMethod);
|
|
|
|
if Assigned(lRTTIMethod) then
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
if (lRTTIMethod.Visibility <> mvPublic) or (not(lRTTIMethod.MethodKind in [mkProcedure, mkFunction])) then
|
2018-12-09 23:03:06 +01:00
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
LogW(Format('Method "%s" cannot be called. Only public functions or procedures can be called. ',
|
|
|
|
[lMethod]));
|
2018-12-09 23:03:06 +01:00
|
|
|
raise EMVCJSONRPCMethodNotFound.Create(lMethod);
|
|
|
|
end;
|
|
|
|
|
2018-12-12 22:29:45 +01:00
|
|
|
if (lJSONRPCReq.RequestType = TJSONRPCRequestType.Request) and (lRTTIMethod.MethodKind <> mkFunction) then
|
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
raise EMVCJSONRPCInvalidParams.Create
|
|
|
|
('Cannot call a procedure using a JSON-RPC request. [HINT] Use requests for functions and notifications for procedures');
|
2018-12-12 22:29:45 +01:00
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
if (lJSONRPCReq.RequestType = TJSONRPCRequestType.Notification) and (lRTTIMethod.MethodKind <> mkProcedure)
|
|
|
|
then
|
2018-12-12 22:29:45 +01:00
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
raise EMVCJSONRPCInvalidParams.Create
|
|
|
|
('Cannot call a function using a JSON-RPC notification. [HINT] Use requests for functions and notifications for procedures');
|
2018-12-12 22:29:45 +01:00
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSONRPCReq.FillParameters(lJSON, lRTTIMethod);
|
2018-05-16 19:46:29 +02:00
|
|
|
try
|
2018-12-12 22:29:45 +01:00
|
|
|
LogD('[JSON-RPC][CALL][' + CALL_TYPE[lRTTIMethod.MethodKind] + '] ' + lRTTIMethod.Name);
|
2019-01-08 12:48:27 +01:00
|
|
|
// CheckInputParametersTypes(lRTTIMethod);
|
2018-11-21 22:11:58 +01:00
|
|
|
lRes := lRTTIMethod.Invoke(fRPCInstance, lJSONRPCReq.Params.ToArray);
|
2018-05-16 19:46:29 +02:00
|
|
|
except
|
|
|
|
on E: EInvalidCast do
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCInvalidParams.Create('Check your input parameters types');
|
|
|
|
end;
|
2017-10-10 12:19:46 +02:00
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
case lJSONRPCReq.RequestType of
|
|
|
|
TJSONRPCRequestType.Notification:
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
// if lRes.IsObject then
|
|
|
|
// lRes.AsObject.Free;
|
2018-05-16 19:46:29 +02:00
|
|
|
ResponseStatus(HTTP_STATUS.NoContent);
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
2018-05-16 19:46:29 +02:00
|
|
|
TJSONRPCRequestType.Request:
|
|
|
|
begin
|
|
|
|
lJSONRPCResponse := CreateResponse(lJSONRPCReq.RequestID, lRes);
|
2019-01-08 12:48:27 +01:00
|
|
|
ResponseStatus(200);
|
|
|
|
Render(lJSONRPCResponse.AsJSON);
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
2018-05-16 19:46:29 +02:00
|
|
|
else
|
|
|
|
raise EMVCJSONRPCException.Create('Invalid RequestType');
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
2017-09-26 01:02:09 +02:00
|
|
|
end
|
|
|
|
else
|
2018-12-09 23:03:06 +01:00
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
LogW(Format('Method "%s" has not be found in %s. Only public methods can be invoked.',
|
|
|
|
[lMethod, fRPCInstance.QualifiedClassName]));
|
2018-05-16 19:46:29 +02:00
|
|
|
raise EMVCJSONRPCMethodNotFound.Create(lMethod);
|
2018-12-09 23:03:06 +01:00
|
|
|
end;
|
2017-09-26 01:02:09 +02:00
|
|
|
finally
|
|
|
|
lRTTI.Free;
|
|
|
|
end;
|
2017-09-24 19:40:40 +02:00
|
|
|
finally
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSON.Free;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
2017-09-26 01:02:09 +02:00
|
|
|
except
|
|
|
|
on E: EMVCJSONRPCErrorResponse do
|
|
|
|
begin
|
|
|
|
{
|
|
|
|
http://www.jsonrpc.org/historical/json-rpc-over-http.html#response-codes
|
|
|
|
HTTP Status code message
|
|
|
|
500 -32700 Parse error.
|
|
|
|
400 -32600 Invalid Request.
|
|
|
|
404 -32601 Method not found.
|
|
|
|
500 -32602 Invalid params.
|
|
|
|
500 -32603 Internal error.
|
|
|
|
500 -32099..-32000 Server error.
|
|
|
|
}
|
|
|
|
case E.JSONRPCErrorCode of
|
2018-05-16 19:46:29 +02:00
|
|
|
- 32700:
|
|
|
|
ResponseStatus(500);
|
|
|
|
-32600:
|
|
|
|
ResponseStatus(400);
|
|
|
|
-32601:
|
|
|
|
ResponseStatus(404);
|
|
|
|
-32602:
|
|
|
|
ResponseStatus(500);
|
|
|
|
-32603:
|
|
|
|
ResponseStatus(500);
|
|
|
|
-32099 .. -32000:
|
|
|
|
ResponseStatus(500);
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
2019-01-08 12:48:27 +01:00
|
|
|
Render(CreateError(lReqID, E.JSONRPCErrorCode, E.Message), True);
|
|
|
|
LogE(Format('[JSON-RPC][CLS %s][ERR %d][MSG "%s"]', [E.ClassName, E.JSONRPCErrorCode, E.Message]));
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
2018-05-16 19:46:29 +02:00
|
|
|
on E: Exception do
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Render(CreateError(lReqID, 0, E.Message), True);
|
|
|
|
LogE(Format('[JSON-RPC][CLS %s][MSG "%s"]', [E.ClassName, E.Message]));
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
2017-09-24 19:40:40 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TMVCJSONRPCController.JSONObjectAs<T>(const JSON: TJDOJsonObject): T;
|
2017-10-10 12:19:46 +02:00
|
|
|
begin
|
|
|
|
Result := T.Create;
|
|
|
|
try
|
|
|
|
GetSerializer.JsonObjectToObject(JSON, Result, TMVCSerializationType.stProperties, []);
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
{ EMVCJSONRPCParseError }
|
|
|
|
|
|
|
|
constructor EMVCJSONRPCParseError.Create;
|
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
inherited Create
|
|
|
|
('Parse error. Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text');
|
2017-09-24 19:40:40 +02:00
|
|
|
FJSONRPCErrorCode := -32700;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCJSONRPCInvalidRequest }
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor EMVCJSONRPCInvalidRequest.Create(const Message: String);
|
|
|
|
var
|
|
|
|
lMsg: string;
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
lMsg := 'Invalid Request. The JSON sent is not a valid Request object.';
|
|
|
|
if not Message.IsEmpty then
|
|
|
|
begin
|
|
|
|
lMsg := lMsg + ' [HINT] ' + Message;
|
|
|
|
end;
|
|
|
|
inherited Create(lMsg);
|
2017-09-24 19:40:40 +02:00
|
|
|
FJSONRPCErrorCode := -32600;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCJSONRPCMethodNotFound }
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
constructor EMVCJSONRPCMethodNotFound.Create(const MethodName: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
2018-11-21 22:11:58 +01:00
|
|
|
inherited CreateFmt('Method "%s" not found. The method does not exist or is not available.', [MethodName]);
|
2017-09-24 19:40:40 +02:00
|
|
|
FJSONRPCErrorCode := -32601;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCJSONRPCInvalidParams }
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor EMVCJSONRPCInvalidParams.Create(const Message: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
inherited Create('Invalid params. [hint: ' + message + ']');
|
|
|
|
FJSONRPCErrorCode := -32602;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCJSONRPCInternalError }
|
|
|
|
|
|
|
|
constructor EMVCJSONRPCInternalError.Create;
|
|
|
|
begin
|
|
|
|
inherited Create('Internal JSON-RPC error');
|
|
|
|
FJSONRPCErrorCode := -32603;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCJSONRPCServerError }
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
constructor EMVCJSONRPCServerError.Create(const JSONRPCError: Integer; const Message: string);
|
2017-09-24 19:40:40 +02:00
|
|
|
begin
|
|
|
|
inherited Create(message);
|
|
|
|
FJSONRPCErrorCode := JSONRPCError;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
{ TJSONRPCRequest }
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
constructor TJSONRPCRequest.Create(const aID: TValue; const aMethod: String);
|
|
|
|
begin
|
|
|
|
inherited Create(aMethod);
|
|
|
|
SetID(aID);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TJSONRPCRequest.Create(const aID: TValue);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
SetID(aID);
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
constructor TJSONRPCRequest.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TJSONRPCRequest.Destroy;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCRequest.GetRequestType: TJSONRPCRequestType;
|
|
|
|
begin
|
|
|
|
if FID.IsEmpty then
|
|
|
|
Result := TJSONRPCRequestType.Notification
|
|
|
|
else
|
|
|
|
Result := TJSONRPCRequestType.Request;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure TJSONRPCRequest.SetJSON(const JSON: TJDOJsonObject);
|
2017-09-28 00:14:34 +02:00
|
|
|
begin
|
|
|
|
if JSON.Types[JSONRPC_ID] = jdtString then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.S[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtInt then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.I[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtLong then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.L[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtULong then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.U[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := TValue.Empty;
|
2017-09-28 00:14:34 +02:00
|
|
|
Method := JSON.S[JSONRPC_METHOD];
|
|
|
|
Params.Clear;
|
2019-01-08 12:48:27 +01:00
|
|
|
// if JSON.Types[JSONRPC_PARAMS] = jdtArray then
|
|
|
|
// begin
|
|
|
|
// lParams := JSON.A[JSONRPC_PARAMS];
|
|
|
|
// for I := 0 to lParams.Count - 1 do
|
|
|
|
// begin
|
|
|
|
// { TODO -oDanieleT -cGeneral : Qui devo sapere cosa si aspetta la classe, altrimenti non posso castare al tipo corretto }
|
|
|
|
// AddParam(Params,lParams[I]);
|
|
|
|
// end;
|
|
|
|
// end
|
|
|
|
// else if JSON.Types[JSONRPC_PARAMS] <> jdtNone then
|
|
|
|
// begin
|
|
|
|
// raise EMVCJSONRPCException.Create('Params must be a JSON array or null');
|
|
|
|
// end;
|
2017-09-28 00:14:34 +02:00
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
constructor TJSONRPCNotification.Create(const aMethod: String);
|
|
|
|
begin
|
|
|
|
Create;
|
|
|
|
Method := aMethod;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
destructor TJSONRPCNotification.Destroy;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
|
|
|
FParams.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCNotification.FillParameters(const JSON: TJDOJsonObject; const RTTIMethod: TRTTIMethod);
|
2018-05-16 19:46:29 +02:00
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
lRTTIMethodParams: TArray<TRttiParameter>;
|
|
|
|
lRTTIMethodParam: TRttiParameter;
|
|
|
|
lJSONParams: TJDOJsonArray;
|
|
|
|
I: Integer;
|
2018-05-16 19:46:29 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSONParams := nil;
|
|
|
|
Params.Clear;
|
|
|
|
if JSON.Types[JSONRPC_PARAMS] = jdtArray then
|
2018-05-16 19:46:29 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSONParams := JSON.A[JSONRPC_PARAMS];
|
|
|
|
end
|
|
|
|
else if JSON.Types[JSONRPC_PARAMS] <> jdtNone then
|
|
|
|
begin
|
|
|
|
raise EMVCJSONRPCException.Create('Params must be a JSON array or null');
|
|
|
|
end;
|
|
|
|
|
|
|
|
lRTTIMethodParams := RTTIMethod.GetParameters;
|
|
|
|
if (Length(lRTTIMethodParams) > 0) and (not Assigned(lJSONParams)) then
|
|
|
|
raise EMVCJSONRPCInvalidParams.CreateFmt('Wrong parameters count. Expected %d got %d.',
|
|
|
|
[Length(lRTTIMethodParams), 0]);
|
|
|
|
if Assigned(lJSONParams) and (Length(lRTTIMethodParams) <> lJSONParams.Count) then
|
|
|
|
raise EMVCJSONRPCInvalidParams.CreateFmt('Wrong parameters count. Expected %d got %d.',
|
|
|
|
[Length(lRTTIMethodParams), lJSONParams.Count]);
|
|
|
|
for lRTTIMethodParam in lRTTIMethodParams do
|
|
|
|
begin
|
|
|
|
if lRTTIMethodParam.Flags * [pfVar, pfOut, pfArray, pfReference] <> [] then
|
|
|
|
raise EMVCJSONRPCInvalidParams.CreateFmt
|
|
|
|
('Parameter modifier not supported for formal parameter [%s]. Only const and value modifiers are allowed.',
|
|
|
|
[lRTTIMethodParam.Name]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// scroll json params and rttimethod params and find the best match
|
|
|
|
if Assigned(lJSONParams) then
|
|
|
|
begin
|
|
|
|
for I := 0 to lJSONParams.Count - 1 do
|
|
|
|
begin
|
|
|
|
JSONDataValueToTValueParam(lJSONParams[I], lRTTIMethodParams[I], Params);
|
|
|
|
end;
|
2018-05-16 19:46:29 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCNotification.GetJSON: TJDOJsonObject;
|
2018-05-16 19:46:29 +02:00
|
|
|
var
|
|
|
|
I: Integer;
|
|
|
|
begin
|
|
|
|
if FMethod.IsEmpty then
|
|
|
|
raise EMVCJSONRPCException.Create('JSON-RPC "Method" cannot be empty');
|
|
|
|
Result := inherited;
|
|
|
|
Result.S[JSONRPC_METHOD] := FMethod;
|
|
|
|
if FParams.Count > 0 then
|
|
|
|
begin
|
|
|
|
for I := 0 to FParams.Count - 1 do
|
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
AppendTValueToJsonArray(FParams.FParamsValue[I], FParams.FParamsType[I], Result.A[JSONRPC_PARAMS]);
|
2018-05-16 19:46:29 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
function TJSONRPCNotification.GetMethod: string;
|
|
|
|
begin
|
|
|
|
Result := FMethod;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCNotification.GetParams: TJSONRPCRequestParams;
|
|
|
|
begin
|
|
|
|
Result := FParams;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
procedure TJSONRPCNotification.SetMethod(const Value: string);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FMethod := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TJSONRCPResponse }
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
constructor TJSONRPCResponse.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
FError := nil;
|
|
|
|
end;
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
destructor TJSONRPCResponse.Destroy;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FreeAndNil(FError);
|
2018-12-09 23:03:06 +01:00
|
|
|
if FResult.IsObject then
|
2017-09-28 00:14:34 +02:00
|
|
|
FResult.AsObject.Free;
|
2017-09-26 01:02:09 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
function TJSONRPCResponse.GetError: TJSONRPCResponseError;
|
|
|
|
begin
|
|
|
|
Result := FError;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCResponse.GetID: TValue;
|
|
|
|
begin
|
|
|
|
Result := FID;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCResponse.GetJSON: TJDOJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
Result := inherited;
|
|
|
|
// Must generate something like the following:
|
|
|
|
// {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}
|
2018-05-16 19:46:29 +02:00
|
|
|
|
|
|
|
if FID.IsEmpty then
|
|
|
|
begin
|
|
|
|
Result.Values[JSONRPC_ID] := jdtNone;
|
|
|
|
end
|
|
|
|
else if FID.IsType<string> then
|
|
|
|
begin
|
|
|
|
Result.S[JSONRPC_ID] := FID.AsString;
|
|
|
|
end
|
|
|
|
else if FID.IsType<Int32> then
|
|
|
|
begin
|
|
|
|
Result.I[JSONRPC_ID] := FID.AsInteger;
|
|
|
|
end
|
|
|
|
else if FID.IsType<Int64> then
|
|
|
|
begin
|
|
|
|
Result.I[JSONRPC_ID] := FID.AsInt64;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EMVCJSONRPCException.Create('ID can be only Int32, Int64 or String');
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
try
|
|
|
|
if Assigned(FError) then
|
|
|
|
begin
|
|
|
|
Result.O[JSONRPC_ERROR].I[JSONRPC_CODE] := FError.Code;
|
2017-09-28 00:14:34 +02:00
|
|
|
Result.O[JSONRPC_ERROR].S[JSONRPC_MESSAGE] := FError.ErrMessage;
|
2017-09-26 01:02:09 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
TValueToJsonElement(Self.FResult, Result, JSONRPC_RESULT);
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
function TJSONRPCResponse.GetResult: TValue;
|
|
|
|
begin
|
|
|
|
Result := FResult;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCResponse.IsError: Boolean;
|
|
|
|
begin
|
|
|
|
Result := Assigned(FError);
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCResponse.ResultAsJSONArray: TJDOJsonArray;
|
2018-12-17 00:39:29 +01:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Result := Self.Result.AsObject as TJDOJsonArray;
|
2018-12-17 00:39:29 +01:00
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCResponse.ResultAsJSONObject: TJDOJsonObject;
|
2018-12-17 00:39:29 +01:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Result := Self.Result.AsObject as TJDOJsonObject;
|
2018-12-17 00:39:29 +01:00
|
|
|
end;
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
procedure TJSONRPCResponse.SetError(const Value: TJSONRPCResponseError);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FError := Value;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
procedure TJSONRPCResponse.SetID(const Value: TValue);
|
|
|
|
begin
|
|
|
|
FID := Value;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure TJSONRPCResponse.SetJSON(const JSON: TJDOJsonObject);
|
2017-09-28 00:14:34 +02:00
|
|
|
begin
|
|
|
|
if JSON.Types[JSONRPC_ID] = jdtString then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.S[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtInt then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.I[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtLong then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.L[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else if JSON.Types[JSONRPC_ID] = jdtULong then
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := JSON.U[JSONRPC_ID]
|
2017-09-28 00:14:34 +02:00
|
|
|
else
|
2017-10-10 12:19:46 +02:00
|
|
|
RequestID := TValue.Empty;
|
2017-09-28 00:14:34 +02:00
|
|
|
|
|
|
|
if JSON.Contains(JSONRPC_RESULT) then
|
|
|
|
begin
|
|
|
|
FreeAndNil(FError);
|
|
|
|
FResult := JSONDataValueToTValue(JSON.Values[JSONRPC_RESULT]);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
FResult := TValue.Empty;
|
|
|
|
if JSON.Contains(JSONRPC_ERROR) then
|
|
|
|
begin
|
|
|
|
FError := TJSONRPCResponseError.Create;
|
|
|
|
FError.Code := JSON.O[JSONRPC_ERROR].I[JSONRPC_CODE];
|
|
|
|
FError.ErrMessage := JSON.O[JSONRPC_ERROR].S[JSONRPC_MESSAGE];
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EMVCJSONRPCException.Create('Response message must have ''result'' or ''error''');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCResponse.SetResult(const Value: TValue);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FResult := Value;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
{ TJSONRPCNotification }
|
2017-09-26 01:02:09 +02:00
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
constructor TJSONRPCNotification.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
2018-05-16 19:46:29 +02:00
|
|
|
FParams := TJSONRPCRequestParams.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
constructor TJSONRPCObject.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCObject.GetJSON: TJDOJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
2019-01-08 12:48:27 +01:00
|
|
|
Result := TJDOJsonObject.Create;
|
2017-09-26 01:02:09 +02:00
|
|
|
Result.S[JSONRPC_HEADER] := JSONRPC_VERSION;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
function TJSONRPCObject.GetJSONString: string;
|
2017-09-26 01:02:09 +02:00
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSON: TJDOJsonObject;
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
lJSON := GetJSON;
|
|
|
|
try
|
2018-05-16 19:46:29 +02:00
|
|
|
Result := lJSON.ToJSON;
|
2017-09-26 01:02:09 +02:00
|
|
|
finally
|
|
|
|
lJSON.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
procedure TJSONRPCRequest.SetID(const Value: TValue);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FID := Value;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
procedure TJSONRPCObject.SetJSON(const Value: TJDOJsonObject);
|
2018-05-16 19:46:29 +02:00
|
|
|
begin
|
|
|
|
// not implemented
|
|
|
|
raise Exception.Create('This method must be overwritten by child');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCObject.SetJsonString(const Value: string);
|
2017-09-28 00:14:34 +02:00
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSON: TJDOJsonObject;
|
2017-09-28 00:14:34 +02:00
|
|
|
begin
|
|
|
|
try
|
2019-01-08 12:48:27 +01:00
|
|
|
lJSON := TJDOJsonObject.Parse(Value) as TJDOJsonObject;
|
2017-09-28 00:14:34 +02:00
|
|
|
except
|
|
|
|
raise EMVCJSONRPCParseError.Create;
|
|
|
|
end;
|
|
|
|
try
|
|
|
|
AsJSON := lJSON;
|
|
|
|
finally
|
|
|
|
lJSON.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
{ TJSONRPCResponseError }
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
procedure TJSONRPCResponseError.SetCode(const Value: Integer);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FCode := Value;
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
procedure TJSONRPCResponseError.SetMessage(const Value: string);
|
2017-09-26 01:02:09 +02:00
|
|
|
begin
|
|
|
|
FMessage := Value;
|
|
|
|
end;
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
{ TJSONRPCMessage }
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
function TJSONRPCRequest.GetID: TValue;
|
|
|
|
begin
|
|
|
|
Result := FID;
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
function TJSONRPCRequest.GetJSON: TJDOJsonObject;
|
2018-05-16 19:46:29 +02:00
|
|
|
begin
|
|
|
|
Result := inherited GetJSON;
|
|
|
|
if not FID.IsEmpty then
|
|
|
|
begin
|
|
|
|
if FID.IsType<string> then
|
|
|
|
begin
|
|
|
|
Result.S[JSONRPC_ID] := FID.AsString;
|
|
|
|
end
|
|
|
|
else if FID.IsType<Int32> then
|
|
|
|
begin
|
|
|
|
Result.I[JSONRPC_ID] := FID.AsInteger;
|
|
|
|
end
|
|
|
|
else if FID.IsType<Int64> then
|
|
|
|
begin
|
|
|
|
Result.I[JSONRPC_ID] := FID.AsInt64;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EMVCJSONRPCException.Create('ID can be only Int32, Int64 or String');
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
{ TJSONRPCProxyGenerator }
|
|
|
|
|
|
|
|
constructor TJSONRPCProxyGenerator.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure RegisterJSONRPCProxyGenerator(const aLanguage: String; const aClass: TJSONRPCProxyGeneratorClass);
|
|
|
|
begin
|
|
|
|
if not Assigned(GProxyGeneratorsRegister) then
|
|
|
|
begin
|
|
|
|
GProxyGeneratorsRegister := TDictionary<String, TJSONRPCProxyGeneratorClass>.Create();
|
|
|
|
end;
|
|
|
|
GProxyGeneratorsRegister.AddOrSetValue(aLanguage.ToLower, aClass);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCProxyGenerator.EndGeneration;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCProxyGenerator.StartGeneration;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
{ TJSONRPCRequestParams }
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TJDOJsonArray);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtJSONArray);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TJDOJsonObject);
|
|
|
|
begin
|
|
|
|
Add(Value, pdTJDOJsonObject);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: Integer);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtInteger);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: String);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtString);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: Boolean);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtBoolean);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: Double);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtFloat);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TDateTime);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtDateTime);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TTime);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtTime);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TDate);
|
|
|
|
begin
|
|
|
|
Add(Value, pdtDate);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Clear;
|
|
|
|
begin
|
|
|
|
FParamsValue.Clear;
|
|
|
|
FParamsType.Clear;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCRequestParams.Count: Integer;
|
|
|
|
begin
|
|
|
|
Result := FParamsValue.Count;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TJSONRPCRequestParams.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FParamsValue := TList<TValue>.Create;
|
|
|
|
FParamsType := TList<TJSONRPCParamDataType>.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TJSONRPCRequestParams.Destroy;
|
|
|
|
var
|
|
|
|
lValue: TValue;
|
|
|
|
begin
|
|
|
|
for lValue in FParamsValue do
|
|
|
|
begin
|
|
|
|
if lValue.IsObject then
|
|
|
|
lValue.AsObject.Free;
|
|
|
|
end;
|
|
|
|
FParamsValue.Free;
|
|
|
|
FParamsType.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCRequestParams.GetItem(const Index: Integer): TValue;
|
|
|
|
begin
|
|
|
|
Result := FParamsValue[Index];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCRequestParams.GetItemDataType(const Index: Integer): TJSONRPCParamDataType;
|
|
|
|
begin
|
|
|
|
Result := FParamsType[Index];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJSONRPCRequestParams.ToArray: TArray<TValue>;
|
|
|
|
begin
|
|
|
|
Result := FParamsValue.ToArray;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJSONRPCRequestParams.Add(const Value: TValue; const ParamType: TJSONRPCParamDataType);
|
|
|
|
begin
|
|
|
|
FParamsValue.Add(Value);
|
|
|
|
FParamsType.Add(ParamType);
|
|
|
|
end;
|
|
|
|
|
2018-12-17 00:39:29 +01:00
|
|
|
initialization
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
FreeAndNil(GProxyGeneratorsRegister);
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
end.
|