+ Added example for serialize single record in controller

This commit is contained in:
Daniele Teti 2022-07-18 18:04:36 +02:00
parent 08bf440713
commit 8ed42ee797
3 changed files with 72 additions and 30 deletions

View File

@ -233,14 +233,20 @@ type
[MVCPath('/arrays')]
procedure GetClassWithArrays;
// Enums
//Records
[MVCHTTPMethod([httpGET])]
[MVCPath('/enums')]
procedure GetClassWithEnums;
[MVCPath('/record')]
procedure GetSingleRecord;
[MVCHTTPMethod([httpPOST])]
[MVCPath('/enums')]
procedure EchoClassWithEnums;
// Enums
// [MVCHTTPMethod([httpGET])]
// [MVCPath('/enums')]
// procedure GetClassWithEnums;
//
// [MVCHTTPMethod([httpPOST])]
// [MVCPath('/enums')]
// procedure EchoClassWithEnums;
end;
implementation
@ -390,15 +396,15 @@ begin
Render(Person, False);
end;
procedure TRenderSampleController.EchoClassWithEnums;
var
lObj: TClassWithEnums;
begin
lObj := Context.Request.BodyAs<TClassWithEnums>;
lObj.RGBSet := [ctBlue, ctGreen, ctRed];
lObj.EnumWithName := ctBlue;
Render(lObj, True);
end;
//procedure TRenderSampleController.EchoClassWithEnums;
//var
// lObj: TClassWithEnums;
//begin
// lObj := Context.Request.BodyAs<TClassWithEnums>;
// lObj.RGBSet := [ctBlue, ctGreen, ctRed];
// lObj.EnumWithName := ctBlue;
// Render(lObj, True);
//end;
procedure TRenderSampleController.GetBinaryData(const filename: string);
var
@ -430,18 +436,18 @@ begin
Render(lClass);
end;
procedure TRenderSampleController.GetClassWithEnums;
var
lObj: TClassWithEnums;
begin
lObj := TClassWithEnums.Create;
lObj.RGBSet := [ctGreen, ctBlue];
lObj.EnumSimple := ctGreen;
lObj.EnumWithName := ctGreen;
lObj.EnumWithOrdValue := ctGreen;
lObj.EnumWithMappedValues := ctGreen;
Render(lObj);
end;
//procedure TRenderSampleController.GetClassWithEnums;
//var
// lObj: TClassWithEnums;
//begin
// lObj := TClassWithEnums.Create;
// lObj.RGBSet := [ctGreen, ctBlue];
// lObj.EnumSimple := ctGreen;
// lObj.EnumWithName := ctGreen;
// lObj.EnumWithOrdValue := ctGreen;
// lObj.EnumWithMappedValues := ctGreen;
// Render(lObj);
//end;
procedure TRenderSampleController.GetCustomerByID_AsTObject(const ID: Integer);
var
@ -828,6 +834,14 @@ begin
Render(TSimpleListTest.Create);
end;
procedure TRenderSampleController.GetSingleRecord;
var
lSR: TSimpleRecord;
begin
lSR := TSimpleRecord.Create;
Render<TSimpleRecord>(200, lSR);
end;
procedure TRenderSampleController.GetPeopleAsCSV;
begin
ResponseStream.AppendLine('first_name;last_name;age');

View File

@ -232,7 +232,6 @@ type
procedure FillJSONArray(const AJsonArray: TJsonArray);
end;
TMVCRecordUtils = record
private
class function JSONObjectToRecord<T: record>(const JSONObject: TJSONObject; const Serialier: TMVCJsonDataObjectsSerializer): T; overload; static; inline;
@ -2732,7 +2731,13 @@ procedure TMVCJsonDataObjectsSerializer.RecordToJsonObject(
const AJsonObject: TJDOJsonObject; const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList);
begin
raise Exception.Create('Not implemented');
InternalRecordToJsonObject(
ARecord,
ARecordTypeInfo,
AJsonObject,
AType,
AIgnoredAttributes,
nil, nil,nil);
end;
function TMVCJsonDataObjectsSerializer.SerializeCollection(const AList: TObject; const AType: TMVCSerializationType;
@ -2975,8 +2980,22 @@ function TMVCJsonDataObjectsSerializer.SerializeRecord(const ARecord: Pointer;
const ARecordTypeInfo: PTypeInfo; const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList;
const ASerializationAction: TMVCSerializationAction): string;
var
lJSON: TJDOJsonObject;
begin
raise Exception.Create('not implemented');
lJSON := TJDOJsonObject.Create;
try
RecordToJsonObject(
ARecord,
ARecordTypeInfo,
lJSON,
TMVCSerializationType.stFields,
nil);
Result := lJSON.ToJSON(True);
finally
lJSON.Free;
end;
end;
function TMVCJsonDataObjectsSerializer.TryMapNullableFloat(var Value: TValue; const JSONDataObject: TJsonObject;

View File

@ -723,6 +723,7 @@ type
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AStatusCode: Integer; const AObject: IInterface;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render<T: record>(const AStatusCode: Integer; var ARecord: T); overload;
// PODOs Collection render
procedure Render<T: class>(const ACollection: TObjectList<T>;
const ASerializationAction: TMVCSerializationAction<T> = nil); overload;
@ -3824,6 +3825,14 @@ begin
raise EMVCException.Create('Can not render an empty collection.');
end;
procedure TMVCRenderer.Render<T>(const AStatusCode: Integer; var ARecord: T);
begin
SetStatusCode(AStatusCode);
Render(
Serializer(GetContentType)
.SerializeRecord(@ARecord, TypeInfo(T), TMVCSerializationType.stFields,nil,nil));
end;
procedure TMVCRenderer.Render<T>(const AStatusCode: Integer; const ACollection: TObjectList<T>;
const AOwns: Boolean; const ASerializationAction: TMVCSerializationAction<T>);
begin