+ Added TMVCRecordUtils.JSONArrayToArrayOfRecord<T> with relative TESTS

This commit is contained in:
Daniele Teti 2022-07-05 16:51:31 +02:00
parent 08c3fca0b3
commit 686c23cb33
2 changed files with 36 additions and 13 deletions

View File

@ -223,7 +223,10 @@ type
TMVCRecordUtils = record
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject): T; static;
private
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject; const Serialier: 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;
end;
@ -3474,16 +3477,41 @@ end;
{ TMVCRecordUtils }
class function TMVCRecordUtils.JSONArrayToArrayOfRecord<T>(
const JSONArray: TJSONArray): TArray<T>;
var
I: Integer;
lSer: TMVCJsonDataObjectsSerializer;
begin
lSer := TMVCJsonDataObjectsSerializer.Create(nil);
try
SetLength(Result, JSONArray.Count);
for I := Low(Result) to High(Result) do
begin
Result[I] := JSONObjectToRecord<T>(JSONArray.Items[I].ObjectValue, lSer);
end;
finally
lSer.Free;
end;
end;
class function TMVCRecordUtils.JSONObjectToRecord<T>(const JSONObject: TJSONObject): T;
var
lSer: TMVCJsonDataObjectsSerializer;
begin
lSer := TMVCJsonDataObjectsSerializer.Create(nil);
try
Result := lSer.JSONObjectToRecord<T>(JSONObject);
Result := JSONObjectToRecord<T>(JSONObject, lSer);
finally
lSer.Free;
end;
end;
class function TMVCRecordUtils.JSONObjectToRecord<T>(
const JSONObject: TJSONObject;
const Serialier: TMVCJsonDataObjectsSerializer): T;
begin
Serialier.JSONObjectToRecord<T>(JSONObject);
end;
end.

View File

@ -3006,22 +3006,17 @@ procedure TJSONRPCServerTest.TestRequest_NoParams_DynamicArrayOfRecordAsResult;
var
lReq: IJSONRPCRequest;
lRPCResp: IJSONRPCResponse;
lSimpleRec: TArray<TSimpleRecord>;
lSimpleRecArray: TArray<TSimpleRecord>;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'GetArrayOfRecords';
lReq.RequestID := 1234;
lRPCResp := FExecutor2.ExecuteRequest(lReq);
lSimpleRec := TMVCRecordUtils.JSONArrayToArrayOfRecord<TSimple Record>(lRPCResp.ResultAsJSONArray);
// Assert.AreEqual('the string property', lSimpleRec.StringProperty);
// Assert.AreEqual(1234, lSimpleRec.IntegerProperty);
// Assert.AreEqual(EncodeDate(2022,7,5), lSimpleRec.DateProperty);
// Assert.AreEqual(EncodeTime(12,13,14,0), lSimpleRec.TimeProperty);
// Assert.AreEqual(EncodeDate(2022,7,5) + EncodeTime(12,13,14,0), lSimpleRec.DateTimeProperty, 0.000001);
// Assert.AreEqual(True, lSimpleRec.BooleanProperty);
// Assert.AreEqual(EnumItem2, lSimpleRec.EnumProperty);
// Assert.IsTrue([EnumItem1, EnumItem3] * lSimpleRec.SetProperty = [EnumItem1, EnumItem3]);
// Assert.IsTrue(lSimpleRec.SetProperty - [EnumItem1, EnumItem3] = []);
lSimpleRecArray := TMVCRecordUtils.JSONArrayToArrayOfRecord<TSimpleRecord>(lRPCResp.ResultAsJSONArray);
Assert.AreEqual(3, Length(lSimpleRecArray));
Assert.AreEqual(0, lSimpleRecArray[0].IntegerProperty);
Assert.AreEqual(1, lSimpleRecArray[1].IntegerProperty);
Assert.AreEqual(2, lSimpleRecArray[2].IntegerProperty);
end;
procedure TJSONRPCServerTest.TestRequest_NoParams_SingleRecordAsResult;