+ added feature in jsonrpc sample about record parameters for jsonrpc

This commit is contained in:
Daniele Teti 2022-07-18 19:04:15 +02:00
parent 8ed42ee797
commit efadb389c7
4 changed files with 114 additions and 30 deletions

View File

@ -544,9 +544,43 @@ object MainForm: TMainForm
Width = 601
Height = 545
Anchors = [akLeft, akTop, akRight, akBottom]
ItemHeight = 13
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Consolas'
Font.Style = []
ItemHeight = 15
ParentFont = False
ScrollWidth = 5000
TabOrder = 1
end
object btnGetArrayOfRecords: TButton
Left = 16
Top = 63
Width = 185
Height = 40
Caption = 'Returning Array of Records'
TabOrder = 2
OnClick = btnGetArrayOfRecordsClick
end
object btnGetDynArray: TButton
Left = 16
Top = 109
Width = 185
Height = 40
Caption = 'Returning DynArray of Records'
TabOrder = 3
OnClick = btnGetDynArrayClick
end
object btnPassAndGetRecord: TButton
Left = 16
Top = 155
Width = 185
Height = 40
Caption = 'Using record parameters'
TabOrder = 4
OnClick = btnPassAndGetRecordClick
end
end
end
object DataSource1: TDataSource

View File

@ -95,6 +95,9 @@ type
TabSheet6: TTabSheet;
btnSingleRec: TButton;
lbLogRec: TListBox;
btnGetArrayOfRecords: TButton;
btnGetDynArray: TButton;
btnPassAndGetRecord: TButton;
procedure btnSubstractClick(Sender: TObject);
procedure btnReverseStringClick(Sender: TObject);
procedure edtGetCustomersClick(Sender: TObject);
@ -120,6 +123,9 @@ type
procedure btnGenericExcWithCustomHAndling2Click(Sender: TObject);
procedure btnGenericExcWithoutCustomHandlingClick(Sender: TObject);
procedure btnSingleRecClick(Sender: TObject);
procedure btnGetArrayOfRecordsClick(Sender: TObject);
procedure btnGetDynArrayClick(Sender: TObject);
procedure btnPassAndGetRecordClick(Sender: TObject);
private
FExecutor: IMVCJSONRPCExecutor;
// FExecutor2: IMVCJSONRPCExecutor;
@ -387,7 +393,8 @@ begin
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
lPersonRec := TMVCRecordUtils.JSONObjectToRecord<TPersonRec>(lResp.ResultAsJSONObject);
lbLogRec.Items.Add('** TPersonRec **');
lbLogRec.Items.Add(lResp.ResultAsJSONObject.ToJSON());
lbLogRec.Items.Add('JSON: ' + lResp.ResultAsJSONObject.ToJSON());
lbLogRec.Items.Add('-- record --');
lbLogRec.Items.Add(lPersonRec.ToString);
end;
@ -438,6 +445,30 @@ begin
ShowMessage(lPerson.ToJSON(False));
end;
procedure TMainForm.btnPassAndGetRecordClick(Sender: TObject);
var
lReq: IJSONRPCRequest;
lResp: IJSONRPCResponse;
lPersonRec: TPersonRec;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'SavePersonRec';
lReq.RequestID := Random(1000);
lPersonRec.Name := 'Daniele';
lPersonRec.Surname := 'Teti';
lPersonRec.Age := 42;
lPersonRec.Child.ChildName := 'Mattia';
lPersonRec.Child.ChildSurname := 'Teti';
lPersonRec.Child.PersonType := ptFamily;
lPersonRec.PersonType := ptMySelf;
lReq.Params.Add(TValue.From<TPersonRec>(lPersonRec), pdtRecord);
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
lPersonRec := TMVCRecordUtils.JSONObjectToRecord<TPersonRec>(lResp.ResultAsJSONObject);
lbLogRec.Items.Add('** TPersonRec **');
lbLogRec.Items.Add('JSON: ' + lResp.ResultAsJSONObject.ToJSON());
lbLogRec.Items.Add(lPersonRec.ToString);
end;
procedure TMainForm.btnGenericExceptionClick(Sender: TObject);
var
lReq: IJSONRPCNotification;
@ -510,6 +541,47 @@ begin
end;
end;
procedure TMainForm.btnGetArrayOfRecordsClick(Sender: TObject);
var
lReq: IJSONRPCRequest;
lResp: IJSONRPCResponse;
lPeopleRec: TArray<TPersonRec>; //server serializes a static array, we read it as dynarray
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'GetPeopleRecStaticArray';
lReq.RequestID := Random(1000);
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
lPeopleRec := TMVCRecordUtils.JSONArrayToArrayOfRecord<TPersonRec>(lResp.ResultAsJSONArray);
lbLogRec.Items.Add('** TArray<TPersonRec> **');
lbLogRec.Items.Add('JSON: ' + lResp.ResultAsJSONArray.ToJSON());
lbLogRec.Items.Add('-- elements --');
for var lPRec in lPeopleRec do
begin
lbLogRec.Items.Add(' ' + lPRec.ToString);
end;
end;
procedure TMainForm.btnGetDynArrayClick(Sender: TObject);
var
lReq: IJSONRPCRequest;
lResp: IJSONRPCResponse;
lPeopleRec: TArray<TPersonRec>;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'GetPeopleRecDynArray';
lReq.RequestID := Random(1000);
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
lPeopleRec := TMVCRecordUtils.JSONArrayToArrayOfRecord<TPersonRec>(lResp.ResultAsJSONArray);
lbLogRec.Items.Add('** TArray<TPersonRec> **');
lbLogRec.Items.Add('JSON: ' + lResp.ResultAsJSONArray.ToJSON());
lbLogRec.Items.Add('-- elements --');
for var lPRec in lPeopleRec do
begin
lbLogRec.Items.Add(' ' + lPRec.ToString);
end;
end;
procedure TMainForm.btnGetListOfDatasetClick(Sender: TObject);
var
lReq: IJSONRPCRequest;

View File

@ -277,33 +277,11 @@ begin
Result.Age := 42;
Result.Child.ChildName := 'Mattia';
Result.Child.ChildSurname := 'Teti';
Result.PersonType := ptFriend;
Result.InitialTypes := [ptFriend, ptColleague];
Result.Child.PersonType := ptFamily;
Result.PersonType := ptMySelf;
Result.InitialTypes := [ptMySelf];
end;
// function TMyObject.GetPeopleDataset: TFDMemTable;
// var
// lMT: TFDMemTable;
// begin
// lMT := TFDMemTable.Create(nil);
// try
// lMT.FieldDefs.Clear;
// lMT.FieldDefs.Add('FirstName', ftString, 20);
// lMT.FieldDefs.Add('LastName', ftString, 20);
// lMT.Active := True;
// lMT.AppendRecord(['Daniele', 'Teti']);
// lMT.AppendRecord(['Peter', 'Parker']);
// lMT.AppendRecord(['Bruce', 'Banner']);
// lMT.AppendRecord(['Scott', 'Summers']);
// lMT.AppendRecord(['Sue', 'Storm']);
// lMT.First;
// Result := lMT;
// except
// lMT.Free;
// raise;
// end;
// end;
function TMyObject.GetStringDictionary: TMVCStringDictionary;
begin
Result := TMVCStringDictionary.Create;

View File

@ -234,7 +234,7 @@ type
TMVCRecordUtils = record
private
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject; const Serialier: TMVCJsonDataObjectsSerializer): T; overload; static; inline;
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject; const Serializer: TMVCJsonDataObjectsSerializer): T; overload; static; inline;
public
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject): T; overload; static;
class function JSONArrayToArrayOfRecord<T: record>(const JSONArray: TJSONArray): TArray<T>; static;
@ -3774,9 +3774,9 @@ end;
class function TMVCRecordUtils.JSONObjectToRecord<T>(
const JSONObject: TJSONObject;
const Serialier: TMVCJsonDataObjectsSerializer): T;
const Serializer: TMVCJsonDataObjectsSerializer): T;
begin
Result := Serialier.JSONObjectToRecord<T>(JSONObject);
Result := Serializer.JSONObjectToRecord<T>(JSONObject);
end;
end.