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

View File

@ -232,7 +232,6 @@ type
procedure FillJSONArray(const AJsonArray: TJsonArray); procedure FillJSONArray(const AJsonArray: TJsonArray);
end; end;
TMVCRecordUtils = record TMVCRecordUtils = record
private 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 Serialier: TMVCJsonDataObjectsSerializer): T; overload; static; inline;
@ -2732,7 +2731,13 @@ procedure TMVCJsonDataObjectsSerializer.RecordToJsonObject(
const AJsonObject: TJDOJsonObject; const AType: TMVCSerializationType; const AJsonObject: TJDOJsonObject; const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList); const AIgnoredAttributes: TMVCIgnoredList);
begin begin
raise Exception.Create('Not implemented'); InternalRecordToJsonObject(
ARecord,
ARecordTypeInfo,
AJsonObject,
AType,
AIgnoredAttributes,
nil, nil,nil);
end; end;
function TMVCJsonDataObjectsSerializer.SerializeCollection(const AList: TObject; const AType: TMVCSerializationType; 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 ARecordTypeInfo: PTypeInfo; const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList; const AIgnoredAttributes: TMVCIgnoredList;
const ASerializationAction: TMVCSerializationAction): string; const ASerializationAction: TMVCSerializationAction): string;
var
lJSON: TJDOJsonObject;
begin 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; end;
function TMVCJsonDataObjectsSerializer.TryMapNullableFloat(var Value: TValue; const JSONDataObject: TJsonObject; function TMVCJsonDataObjectsSerializer.TryMapNullableFloat(var Value: TValue; const JSONDataObject: TJsonObject;

View File

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