Daniele Teti 2018-07-16 12:34:07 +02:00
parent a4381ec719
commit c305aec5fc
14 changed files with 271 additions and 262 deletions

BIN
7z.exe Normal file

Binary file not shown.

View File

@ -103,7 +103,7 @@ begin
raise; raise;
end; end;
Render<TPerson>(lPeople); Render<TPerson>(lPeople);
SetCache(20); SetCache(60);
end; end;
procedure TPeopleController.CreateBulkData(CTX: TWebContext); procedure TPeopleController.CreateBulkData(CTX: TWebContext);

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //
@ -39,8 +39,7 @@ type
[MVCPath('/')] [MVCPath('/')]
TRenderSampleController = class(TMVCController) TRenderSampleController = class(TMVCController)
protected protected
procedure OnBeforeAction(AContext: TWebContext; const AActionName: string; procedure OnBeforeAction(AContext: TWebContext; const AActionName: string; var AHandled: Boolean); override;
var AHandled: Boolean); override;
public public
[MVCHTTPMethod([httpGET])] [MVCHTTPMethod([httpGET])]
[MVCPath('/customers/($id)')] [MVCPath('/customers/($id)')]
@ -52,6 +51,11 @@ type
[MVCProduces('application/json')] [MVCProduces('application/json')]
procedure GetCustomers_AsDataSet(CTX: TWebContext); procedure GetCustomers_AsDataSet(CTX: TWebContext);
[MVCHTTPMethod([httpGET])]
[MVCPath('/customers/metadata')]
[MVCProduces('application/json')]
procedure GetDataSetWithMetadata;
[MVCHTTPMethod([httpGET])] [MVCHTTPMethod([httpGET])]
[MVCPath('/multi')] [MVCPath('/multi')]
[MVCProduces('application/json')] [MVCProduces('application/json')]
@ -71,7 +75,7 @@ type
[MVCPath('/lotofobjects')] [MVCPath('/lotofobjects')]
procedure GetLotOfPeople; procedure GetLotOfPeople;
//this action is polymorphic // this action is polymorphic
[MVCHTTPMethod([httpGET])] [MVCHTTPMethod([httpGET])]
[MVCPath('/skilledpeople')] [MVCPath('/skilledpeople')]
[MVCProduces('application/json')] [MVCProduces('application/json')]
@ -91,7 +95,6 @@ type
[MVCPath('/customers.csv')] [MVCPath('/customers.csv')]
procedure GetPeopleAsCSV; procedure GetPeopleAsCSV;
[MVCHTTPMethod([httpGET])] [MVCHTTPMethod([httpGET])]
[MVCPath('/customers/unicode/($id).html')] [MVCPath('/customers/unicode/($id).html')]
[MVCProduces('text/html', 'UTF-8')] [MVCProduces('text/html', 'UTF-8')]
@ -157,8 +160,8 @@ begin
Render(s); Render(s);
end; end;
procedure TRenderSampleController.OnBeforeAction(AContext: TWebContext; procedure TRenderSampleController.OnBeforeAction(AContext: TWebContext; const AActionName: string;
const AActionName: string; var AHandled: Boolean); var AHandled: Boolean);
begin begin
inherited; inherited;
@ -207,8 +210,8 @@ begin
// We need a non standard representation, let's create a specific serializer. // We need a non standard representation, let's create a specific serializer.
lSer := TMVCJsonDataObjectsSerializer.Create; lSer := TMVCJsonDataObjectsSerializer.Create;
try try
lSer.DataSetToJsonArray(lDM.qryCustomers, lJObj.A['customers'], TMVCNameCase.ncLowerCase, []); lSer.DataSetToJsonArray(lDM.qryCustomers, lJObj.a['customers'], TMVCNameCase.ncLowerCase, []);
lSer.DataSetToJsonArray(lDM.qryCountry, lJObj.A['countries'], TMVCNameCase.ncLowerCase, []); lSer.DataSetToJsonArray(lDM.qryCountry, lJObj.a['countries'], TMVCNameCase.ncLowerCase, []);
finally finally
lSer.Free; lSer.Free;
end; end;
@ -241,6 +244,24 @@ begin
Render(TSysUser.Create('daniele', ['poweruser', 'role1', 'role2']), True); Render(TSysUser.Create('daniele', ['poweruser', 'role1', 'role2']), True);
end; end;
procedure TRenderSampleController.GetDataSetWithMetadata;
var
lDM: TMyDataModule;
lHolder: TDataSetHolder;
begin
lDM := TMyDataModule.Create(nil);
try
lDM.qryCustomers.Open;
lHolder := TDataSetHolder.Create(lDM.qryCustomers);
lHolder.Metadata.AddProperty('page', '1');
lHolder.Metadata.AddProperty('count', lDM.qryCustomers.RecordCount.ToString);
Render(lHolder);
finally
lDM.Free;
end;
end;
procedure TRenderSampleController.GetLotOfPeople; procedure TRenderSampleController.GetLotOfPeople;
begin begin
Render<TPerson>(GetPeopleList, False); Render<TPerson>(GetPeopleList, False);
@ -248,12 +269,8 @@ end;
procedure TRenderSampleController.GetPerson_AsHTML(CTX: TWebContext); procedure TRenderSampleController.GetPerson_AsHTML(CTX: TWebContext);
begin begin
ResponseStream ResponseStream.Append('<html><body><ul>').Append('<li>FirstName: Daniele</li>').Append('<li>LastName: Teti')
.Append('<html><body><ul>') .AppendFormat('<li>DOB: %s</li>', [DateToISODate(EncodeDate(1975, 5, 2))]).Append('<li>Married: yes</li>')
.Append('<li>FirstName: Daniele</li>')
.Append('<li>LastName: Teti')
.AppendFormat('<li>DOB: %s</li>', [DateToISODate(EncodeDate(1975, 5, 2))])
.Append('<li>Married: yes</li>')
.Append('</ul></body></html>'); .Append('</ul></body></html>');
RenderResponseStream; RenderResponseStream;
end; end;
@ -283,11 +300,8 @@ end;
procedure TRenderSampleController.GetPerson_AsText(const id: Integer); procedure TRenderSampleController.GetPerson_AsText(const id: Integer);
begin begin
ResponseStream ResponseStream.AppendLine('ID : ' + id.ToString).AppendLine('FirstName : Daniele')
.AppendLine('ID : ' + id.ToString) .AppendLine('LastName : Teti').AppendLine('DOB : ' + DateToStr(EncodeDate(1979, 5, 2)))
.AppendLine('FirstName : Daniele')
.AppendLine('LastName : Teti')
.AppendLine('DOB : ' + DateToStr(EncodeDate(1979, 5, 2)))
.AppendLine('Married : yes'); .AppendLine('Married : yes');
RenderResponseStream; RenderResponseStream;
end; end;
@ -339,8 +353,8 @@ begin
try try
People.Metadata.StartProcessing := Now; People.Metadata.StartProcessing := Now;
{$REGION 'Fake data'} {$REGION 'Fake data'}
Sleep(1000); //processing... Sleep(1000); // processing...
p := TPerson.Create; p := TPerson.Create;
p.FirstName := 'Daniele'; p.FirstName := 'Daniele';
@ -363,8 +377,7 @@ begin
p.Married := True; p.Married := True;
People.Items.Add(p); People.Items.Add(p);
{$ENDREGION} {$ENDREGION}
People.Metadata.CustomData := Format('There are %d people in the list', [People.Items.Count]); People.Metadata.CustomData := Format('There are %d people in the list', [People.Items.Count]);
People.Metadata.StopProcessing := Now; People.Metadata.StopProcessing := Now;
Render(People, False); Render(People, False);
@ -380,8 +393,7 @@ var
begin begin
People := TObjectList<TPerson>.Create(True); People := TObjectList<TPerson>.Create(True);
{$REGION 'Fake data'} {$REGION 'Fake data'}
p := TPerson.Create; p := TPerson.Create;
p.FirstName := 'Daniele'; p.FirstName := 'Daniele';
p.LastName := 'Teti'; p.LastName := 'Teti';
@ -403,8 +415,7 @@ begin
p.Married := True; p.Married := True;
People.Add(p); People.Add(p);
{$ENDREGION} {$ENDREGION}
Render<TPerson>(People); Render<TPerson>(People);
end; end;
@ -413,9 +424,9 @@ var
p: TJSONObject; p: TJSONObject;
begin begin
p := TJSONObject.Create; p := TJSONObject.Create;
p.S['FirstName'] := 'Daniele'; p.s['FirstName'] := 'Daniele';
p.S['LastName'] := 'Teti'; p.s['LastName'] := 'Teti';
p.S['DOB'] := DateToISODate(EncodeDate(1975, 5, 2)); p.s['DOB'] := DateToISODate(EncodeDate(1975, 5, 2));
p.B['Married'] := True; p.B['Married'] := True;
Render(p); Render(p);
end; end;
@ -430,8 +441,7 @@ procedure TRenderSampleController.GetPersonPhotoAsStream(CTX: TWebContext);
var var
LPhoto: TFileStream; LPhoto: TFileStream;
begin begin
LPhoto := TFileStream.Create('..\..\_\customer.png', LPhoto := TFileStream.Create('..\..\_\customer.png', fmOpenRead or fmShareDenyWrite);
fmOpenRead or fmShareDenyWrite);
ContentType := 'image/png'; // you can also use MVCProduces attribute ContentType := 'image/png'; // you can also use MVCProduces attribute
// LPhoto is a plain TStream descendant, so it can be rendered as usual // LPhoto is a plain TStream descendant, so it can be rendered as usual

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //
@ -41,7 +41,8 @@ uses
MyDataModuleU in 'MyDataModuleU.pas' {MyDataModule: TDataModule}, MyDataModuleU in 'MyDataModuleU.pas' {MyDataModule: TDataModule},
CustomTypesU in 'CustomTypesU.pas', CustomTypesU in 'CustomTypesU.pas',
CustomTypesSerializersU in 'CustomTypesSerializersU.pas', CustomTypesSerializersU in 'CustomTypesSerializersU.pas',
InMemoryDataU in 'InMemoryDataU.pas'; InMemoryDataU in 'InMemoryDataU.pas',
MVCFramework.DataSet.Utils in '..\..\sources\MVCFramework.DataSet.Utils.pas';
{$R *.res} {$R *.res}

View File

@ -219,6 +219,7 @@
<DCCReference Include="CustomTypesU.pas"/> <DCCReference Include="CustomTypesU.pas"/>
<DCCReference Include="CustomTypesSerializersU.pas"/> <DCCReference Include="CustomTypesSerializersU.pas"/>
<DCCReference Include="InMemoryDataU.pas"/> <DCCReference Include="InMemoryDataU.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.DataSet.Utils.pas"/>
<None Include="ModelSupport_renders\default.txaPackage"/> <None Include="ModelSupport_renders\default.txaPackage"/>
<None Include="ModelSupport_renders\default.txvpck"/> <None Include="ModelSupport_renders\default.txvpck"/>
<None Include="ModelSupport_renders\WebModuleU\default.txaPackage"/> <None Include="ModelSupport_renders\WebModuleU\default.txaPackage"/>
@ -392,6 +393,12 @@
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployFile LocalName="ModelSupport_renders\renders\default.txvpck" Configuration="Release" Class="ProjectFile">
<Platform Name="Win32">
<RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="ModelSupport_renders\RenderSampleControllerU\default.txaPackage" Configuration="Debug" Class="ProjectFile"> <DeployFile LocalName="ModelSupport_renders\RenderSampleControllerU\default.txaPackage" Configuration="Debug" Class="ProjectFile">
<Platform Name="Linux64"> <Platform Name="Linux64">
<RemoteDir>.\</RemoteDir> <RemoteDir>.\</RemoteDir>
@ -404,19 +411,13 @@
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployFile LocalName="ModelSupport_renders\renders\default.txaPackage" Configuration="Debug" Class="ProjectFile"> <DeployFile LocalName="ModelSupport_renders\renders1\default.txvpck" Configuration="Release" Class="ProjectFile">
<Platform Name="Win32"> <Platform Name="Win32">
<RemoteDir>.\</RemoteDir> <RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployFile LocalName="ModelSupport_renders\renders\default.txvpck" Configuration="Debug" Class="ProjectFile"> <DeployFile LocalName="ModelSupport_renders\renders\default.txaPackage" Configuration="Debug" Class="ProjectFile">
<Platform Name="Linux64">
<RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="ModelSupport_renders\RenderSampleControllerU\default.txaPackage" Configuration="Release" Class="ProjectFile">
<Platform Name="Win32"> <Platform Name="Win32">
<RemoteDir>.\</RemoteDir> <RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
@ -428,13 +429,13 @@
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployFile LocalName="ModelSupport_renders\renders1\default.txvpck" Configuration="Release" Class="ProjectFile"> <DeployFile LocalName="ModelSupport_renders\renders\default.txvpck" Configuration="Debug" Class="ProjectFile">
<Platform Name="Win32"> <Platform Name="Linux64">
<RemoteDir>.\</RemoteDir> <RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployFile LocalName="ModelSupport_renders\renders\default.txvpck" Configuration="Release" Class="ProjectFile"> <DeployFile LocalName="ModelSupport_renders\RenderSampleControllerU\default.txaPackage" Configuration="Release" Class="ProjectFile">
<Platform Name="Win32"> <Platform Name="Win32">
<RemoteDir>.\</RemoteDir> <RemoteDir>.\</RemoteDir>
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>

View File

@ -28,44 +28,44 @@ unit MVCFramework.DataSet.Utils;
interface interface
uses System.SysUtils, Data.DB, System.Generics.Collections, System.JSON, uses
System.Rtti, JsonDataObjects, MVCFramework.Serializer.Commons; System.SysUtils,
Data.DB,
System.Generics.Collections,
System.JSON,
System.Rtti,
JsonDataObjects,
MVCFramework.Commons,
MVCFramework.Serializer.Commons;
type type
TFieldNamePolicy = (fpLowerCase, fpUpperCase, fpAsIs); TFieldNamePolicy = (fpLowerCase, fpUpperCase, fpAsIs);
TDataSetHelper = class helper for TDataSet TDataSetHelper = class helper for TDataSet
public public
procedure LoadFromTValue(const Value: TValue; procedure LoadFromTValue(const Value: TValue; const aNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase);
const aNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase);
function AsJSONArray: string; function AsJSONArray: string;
function AsJSONArrayString: string; deprecated 'Use AsJSONArray'; function AsJSONArrayString: string; deprecated 'Use AsJSONArray';
function AsJSONObject(AFieldNamePolicy: TFieldNamePolicy = fpLowerCase): string; function AsJSONObject(AFieldNamePolicy: TFieldNamePolicy = fpLowerCase): string;
function AsJSONObjectString: string; deprecated 'Use AsJSONObject'; function AsJSONObjectString: string; deprecated 'Use AsJSONObject';
procedure LoadFromJSONObject(AJSONObject: TJSONObject; procedure LoadFromJSONObject(AJSONObject: TJSONObject; AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload;
AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload; procedure LoadFromJSONObject(AJSONObject: TJSONObject; AIgnoredFields: TArray<string>;
procedure LoadFromJSONObject(AJSONObject: TJSONObject;
AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload; AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: string; procedure LoadFromJSONArray(AJSONArray: string;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy. AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
fpLowerCase); overload; procedure LoadFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
procedure LoadFromJSONArrayString(AJSONArrayString: string; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string; procedure LoadFromJSONArrayString(AJSONArrayString: string;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: TJSONArray; procedure LoadFromJSONArray(AJSONArray: TJSONArray; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
procedure LoadFromJSONObjectString(AJSONObjectString: string); overload; procedure LoadFromJSONObjectString(AJSONObjectString: string); overload;
procedure LoadFromJSONObjectString(AJSONObjectString: string; procedure LoadFromJSONObjectString(AJSONObjectString: string; AIgnoredFields: TArray<string>); overload;
AIgnoredFields: TArray<string>); overload;
procedure AppendFromJSONArrayString(AJSONArrayString: string); overload; procedure AppendFromJSONArrayString(AJSONArrayString: string); overload;
procedure AppendFromJSONArrayString(AJSONArrayString: string; procedure AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload; AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
function AsObjectList<T: class, constructor>(CloseAfterScroll function AsObjectList<T: class, constructor>(CloseAfterScroll: boolean = false): TObjectList<T>;
: boolean = false): TObjectList<T>; function AsObject<T: class, constructor>(CloseAfterScroll: boolean = false): T;
function AsObject<T: class, constructor>(CloseAfterScroll
: boolean = false): T;
end; end;
TDataSetUtils = class sealed TDataSetUtils = class sealed
@ -75,15 +75,27 @@ type
class constructor Create; class constructor Create;
class destructor Destroy; class destructor Destroy;
class procedure DataSetToObject(ADataSet: TDataSet; AObject: TObject); class procedure DataSetToObject(ADataSet: TDataSet; AObject: TObject);
class procedure DataSetToObjectList<T: class, constructor> class procedure DataSetToObjectList<T: class, constructor>(ADataSet: TDataSet; AObjectList: TObjectList<T>;
(ADataSet: TDataSet; AObjectList: TObjectList<T>;
ACloseDataSetAfterScroll: boolean = True); ACloseDataSetAfterScroll: boolean = True);
end; end;
[MVCNameCase(ncLowerCase)]
TDataSetHolder = class
private
FDataSet: TDataSet;
FMetadata: TMVCStringDictionary;
public
constructor Create(ADataSet: TDataSet); virtual;
destructor Destroy; override;
property Items: TDataSet read FDataSet;
[MVCNameAs('meta')]
property Metadata: TMVCStringDictionary read FMetadata;
end;
implementation implementation
uses uses
MVCFramework.Serializer.JSONDataObjects, MVCFramework.Serializer.JsonDataObjects,
MVCFramework.Serializer.Intf; MVCFramework.Serializer.Intf;
{ TDataSetHelper } { TDataSetHelper }
@ -92,12 +104,12 @@ procedure TDataSetHelper.LoadFromTValue(const Value: TValue; const aNameCase: TM
var var
lSer: TMVCJsonDataObjectsSerializer; lSer: TMVCJsonDataObjectsSerializer;
begin begin
if not({$IFDEF TOKYOORBETTER}Value.IsObjectInstance and {$ENDIF} (Value.AsObject is TJsonArray)) then if not({$IFDEF TOKYOORBETTER}Value.IsObjectInstance and {$ENDIF} (Value.AsObject is TJSONArray)) then
raise Exception.Create('LoadFromTValue requires a TValue containing a TJDOJsonArray'); raise Exception.Create('LoadFromTValue requires a TValue containing a TJDOJsonArray');
lSer := TMVCJsonDataObjectsSerializer.Create; lSer := TMVCJsonDataObjectsSerializer.Create;
try try
lSer.JsonArrayToDataSet(TJsonArray(Value.AsObject), Self, [], TMVCNameCase.ncLowerCase); lSer.JsonArrayToDataSet(TJSONArray(Value.AsObject), Self, [], TMVCNameCase.ncLowerCase);
finally finally
lSer.Free; lSer.Free;
end; end;
@ -169,8 +181,7 @@ begin
end; end;
end; end;
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; AFieldNamePolicy: TFieldNamePolicy);
AFieldNamePolicy: TFieldNamePolicy);
var var
lSerializer: IMVCSerializer; lSerializer: IMVCSerializer;
begin begin
@ -185,8 +196,8 @@ begin
end; end;
end; end;
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy); AFieldNamePolicy: TFieldNamePolicy);
begin begin
Self.DisableControls; Self.DisableControls;
try try
@ -196,8 +207,8 @@ begin
end; end;
end; end;
procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy); AFieldNamePolicy: TFieldNamePolicy);
begin begin
AppendFromJSONArrayString(AJSONArrayString, AIgnoredFields, AFieldNamePolicy); AppendFromJSONArrayString(AJSONArrayString, AIgnoredFields, AFieldNamePolicy);
end; end;
@ -207,8 +218,8 @@ begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), AFieldNamePolicy); AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), AFieldNamePolicy);
end; end;
procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string; procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy); AFieldNamePolicy: TFieldNamePolicy);
begin begin
LoadFromJSONArray(AJSONArrayString, AFieldNamePolicy); LoadFromJSONArray(AJSONArrayString, AFieldNamePolicy);
end; end;
@ -218,16 +229,15 @@ begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create()); AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create());
end; end;
procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; AIgnoredFields: TArray<string>;
AIgnoredFields: TArray<string>; AFieldNamePolicy: TFieldNamePolicy); AFieldNamePolicy: TFieldNamePolicy);
begin begin
raise Exception.Create('Not Implemented'); raise Exception.Create('Not Implemented');
// Mapper.JSONObjectToDataSet(AJSONObject, Self, AIgnoredFields, false, // Mapper.JSONObjectToDataSet(AJSONObject, Self, AIgnoredFields, false,
// AFieldNamePolicy); // AFieldNamePolicy);
end; end;
procedure TDataSetHelper.LoadFromJSONObjectString(AJSONObjectString: string; procedure TDataSetHelper.LoadFromJSONObjectString(AJSONObjectString: string; AIgnoredFields: TArray<string>);
AIgnoredFields: TArray<string>);
var var
lSerializer: IMVCSerializer; lSerializer: IMVCSerializer;
begin begin
@ -246,8 +256,7 @@ begin
// end; // end;
end; end;
procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; AFieldNamePolicy: TFieldNamePolicy);
AFieldNamePolicy: TFieldNamePolicy);
begin begin
LoadFromJSONObject(AJSONObject, TArray<string>.Create()); LoadFromJSONObject(AJSONObject, TArray<string>.Create());
end; end;
@ -264,8 +273,7 @@ begin
TDataSetUtils.CTX := TRttiContext.Create; TDataSetUtils.CTX := TRttiContext.Create;
end; end;
class procedure TDataSetUtils.DataSetToObject(ADataSet: TDataSet; class procedure TDataSetUtils.DataSetToObject(ADataSet: TDataSet; AObject: TObject);
AObject: TObject);
var var
_type: TRttiType; _type: TRttiType;
_fields: TArray<TRttiProperty>; _fields: TArray<TRttiProperty>;
@ -355,8 +363,8 @@ begin
_keys.Free; _keys.Free;
end; end;
class procedure TDataSetUtils.DataSetToObjectList<T>(ADataSet: TDataSet; class procedure TDataSetUtils.DataSetToObjectList<T>(ADataSet: TDataSet; AObjectList: TObjectList<T>;
AObjectList: TObjectList<T>; ACloseDataSetAfterScroll: boolean); ACloseDataSetAfterScroll: boolean);
var var
Obj: T; Obj: T;
SavedPosition: TArray<Byte>; SavedPosition: TArray<Byte>;
@ -385,4 +393,19 @@ begin
CTX.Free; CTX.Free;
end; end;
{ TDataSetHolder }
constructor TDataSetHolder.Create(ADataSet: TDataSet);
begin
inherited Create;
FDataSet := ADataSet;
FMetadata := TMVCStringDictionary.Create;
end;
destructor TDataSetHolder.Destroy;
begin
inherited;
FMetadata.Free;
end;
end. end.

View File

@ -76,7 +76,7 @@ implementation
uses uses
System.NetEncoding, System.NetEncoding,
System.DateUtils, System.DateUtils,
System.Math; System.Math, MVCFramework.Logger;
{ TMVCJWTAuthenticationMiddleware } { TMVCJWTAuthenticationMiddleware }

View File

@ -43,21 +43,34 @@ type
private private
{ private declarations } { private declarations }
protected protected
procedure Serialize(const AElementValue: TValue; var ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>); procedure Serialize(const AElementValue: TValue; var ASerializerObject: TObject;
procedure Deserialize(const ASerializedObject: TObject; var AElementValue: TValue; const AAttributes: TArray<TCustomAttribute>); const AAttributes: TArray<TCustomAttribute>);
procedure Deserialize(const ASerializedObject: TObject; var AElementValue: TValue;
const AAttributes: TArray<TCustomAttribute>);
public public
{ public declarations } { public declarations }
end; end;
TMVCStringDictionarySerializer = class(TInterfacedObject, IMVCTypeSerializer)
public
procedure Serialize(const AElementValue: TValue; var ASerializerObject: TObject;
const AAttributes: System.TArray<System.TCustomAttribute>);
procedure Deserialize(const ASerializedObject: TObject; var AElementValue: TValue;
const AAttributes: System.TArray<System.TCustomAttribute>);
end;
implementation implementation
uses uses
MVCFramework.Serializer.JsonDataObjects; MVCFramework.Serializer.JsonDataObjects,
Data.DB,
MVCFramework.Commons,
System.Generics.Collections,
JsonDataObjects;
{ TStreamSerializerJsonDataObject } { TStreamSerializerJsonDataObject }
procedure TStreamSerializerJsonDataObject.Deserialize( procedure TStreamSerializerJsonDataObject.Deserialize(const ASerializedObject: TObject; var AElementValue: TValue;
const ASerializedObject: TObject; var AElementValue: TValue;
const AAttributes: TArray<TCustomAttribute>); const AAttributes: TArray<TCustomAttribute>);
var var
JsonValue: TJsonValue; JsonValue: TJsonValue;
@ -94,8 +107,7 @@ begin
end; end;
end; end;
procedure TStreamSerializerJsonDataObject.Serialize( procedure TStreamSerializerJsonDataObject.Serialize(const AElementValue: TValue; var ASerializerObject: TObject;
const AElementValue: TValue; var ASerializerObject: TObject;
const AAttributes: TArray<TCustomAttribute>); const AAttributes: TArray<TCustomAttribute>);
var var
Stream: TStream; Stream: TStream;
@ -131,4 +143,31 @@ begin
end; end;
end; end;
{ TMVCStringDictionarySerializer }
procedure TMVCStringDictionarySerializer.Deserialize(const ASerializedObject: TObject; var AElementValue: TValue;
const AAttributes: System.TArray<System.TCustomAttribute>);
begin
raise EMVCDeserializationException.Create('Not Implemented');
end;
procedure TMVCStringDictionarySerializer.Serialize(const AElementValue: TValue; var ASerializerObject: TObject;
const AAttributes: System.TArray<System.TCustomAttribute>);
var
lStringDict: TMVCStringDictionary;
lPair: TPair<string, string>;
lJSONObject: TJsonObject;
begin
lStringDict := AElementValue.AsObject as TMVCStringDictionary;
if Assigned(lStringDict) then
begin
lJSONObject := TJsonObject.Create;
for lPair in lStringDict do
begin
lJSONObject.S[lPair.Key] := lPair.Value;
end;
ASerializerObject := lJSONObject;
end;
end;
end. end.

View File

@ -68,119 +68,51 @@ type
// const AType: TMVCSerializationType; // const AType: TMVCSerializationType;
// const AIgnoredAttributes: TMVCIgnoredList // const AIgnoredAttributes: TMVCIgnoredList
// ); // );
procedure ObjectToJsonObject( procedure ObjectToJsonObject(const AObject: TObject; const AJsonObject: TJsonObject;
const AObject: TObject; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const AJsonObject: TJsonObject; procedure AttributeToJsonDataValue(const AJsonObject: TJsonObject; const AName: string; const AValue: TValue;
const AType: TMVCSerializationType; const AType: TMVCSerializationType; const AIgnored: TMVCIgnoredList;
const AIgnoredAttributes: TMVCIgnoredList const ACustomAttributes: TArray<TCustomAttribute>);
); procedure JsonObjectToObject(const AJsonObject: TJsonObject; const AObject: TObject;
procedure AttributeToJsonDataValue( const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const AJsonObject: TJsonObject; procedure JsonDataValueToAttribute(const AJsonObject: TJsonObject; const AName: string; var AValue: TValue;
const AName: string; const AType: TMVCSerializationType; const AIgnored: TMVCIgnoredList;
const AValue: TValue; const ACustomAttributes: TArray<TCustomAttribute>);
const AType: TMVCSerializationType; procedure JsonArrayToList(const AJsonArray: TJsonArray; const AList: IMVCList; const AClazz: TClass;
const AIgnored: TMVCIgnoredList; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const ACustomAttributes: TArray<TCustomAttribute> procedure DataSetToJsonObject(const ADataSet: TDataSet; const AJsonObject: TJsonObject;
); const ANameCase: TMVCNameCase; const AIgnoredFields: TMVCIgnoredList);
procedure JsonObjectToObject( procedure DataSetToJsonArray(const ADataSet: TDataSet; const AJsonArray: TJsonArray; const ANameCase: TMVCNameCase;
const AJsonObject: TJsonObject; const AIgnoredFields: TMVCIgnoredList);
const AObject: TObject; procedure JsonObjectToDataSet(const AJsonObject: TJsonObject; const ADataSet: TDataSet;
const AType: TMVCSerializationType; const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
const AIgnoredAttributes: TMVCIgnoredList procedure JsonArrayToDataSet(const AJsonArray: TJsonArray; const ADataSet: TDataSet;
); const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
procedure JsonDataValueToAttribute(
const AJsonObject: TJsonObject;
const AName: string;
var AValue: TValue;
const AType: TMVCSerializationType;
const AIgnored: TMVCIgnoredList;
const ACustomAttributes: TArray<TCustomAttribute>
);
procedure JsonArrayToList(
const AJsonArray: TJsonArray;
const AList: IMVCList;
const AClazz: TClass;
const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList
);
procedure DataSetToJsonObject(
const ADataSet: TDataSet;
const AJsonObject: TJsonObject;
const ANameCase: TMVCNameCase;
const AIgnoredFields: TMVCIgnoredList
);
procedure DataSetToJsonArray(
const ADataSet: TDataSet;
const AJsonArray: TJsonArray;
const ANameCase: TMVCNameCase;
const AIgnoredFields: TMVCIgnoredList
);
procedure JsonObjectToDataSet(
const AJsonObject: TJsonObject;
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList;
const ANameCase: TMVCNameCase
);
procedure JsonArrayToDataSet(
const AJsonArray: TJsonArray;
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList;
const ANameCase: TMVCNameCase
);
{ IMVCSerializer } { IMVCSerializer }
function SerializeObject( function SerializeObject(const AObject: TObject; const AType: TMVCSerializationType = stDefault;
const AObject: TObject;
const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = []; const AIgnoredAttributes: TMVCIgnoredList = [];
const ASerializationAction: TMVCSerializationAction = nil const ASerializationAction: TMVCSerializationAction = nil): string;
): string;
function SerializeCollection( function SerializeCollection(const AList: TObject; const AType: TMVCSerializationType = stDefault;
const AList: TObject; const AIgnoredAttributes: TMVCIgnoredList = []): string;
const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = []
): string;
function SerializeDataSet( function SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList = [];
const ADataSet: TDataSet; const ANameCase: TMVCNameCase = ncAsIs): string;
const AIgnoredFields: TMVCIgnoredList = [];
const ANameCase: TMVCNameCase = ncAsIs
): string;
function SerializeDataSetRecord( function SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList = [];
const ADataSet: TDataSet; const ANameCase: TMVCNameCase = ncAsIs): string;
const AIgnoredFields: TMVCIgnoredList = [];
const ANameCase: TMVCNameCase = ncAsIs
): string;
procedure DeserializeObject( procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject;
const ASerializedObject: string; const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []);
const AObject: TObject;
const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = []
);
procedure DeserializeCollection( procedure DeserializeCollection(const ASerializedList: string; const AList: TObject; const AClazz: TClass;
const ASerializedList: string; const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []);
const AList: TObject;
const AClazz: TClass;
const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = []
);
procedure DeserializeDataSet( procedure DeserializeDataSet(const ASerializedDataSet: string; const ADataSet: TDataSet;
const ASerializedDataSet: string; const AIgnoredFields: TMVCIgnoredList = []; const ANameCase: TMVCNameCase = ncAsIs);
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList = [];
const ANameCase: TMVCNameCase = ncAsIs
);
procedure DeserializeDataSetRecord( procedure DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet;
const ASerializedDataSetRecord: string; const AIgnoredFields: TMVCIgnoredList = []; const ANameCase: TMVCNameCase = ncAsIs);
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList = [];
const ANameCase: TMVCNameCase = ncAsIs
);
public public
procedure AfterConstruction; override; procedure AfterConstruction; override;
end; end;
@ -188,7 +120,7 @@ type
implementation implementation
uses uses
MVCFramework.Serializer.JsonDataObjects.CustomTypes; MVCFramework.Serializer.JsonDataObjects.CustomTypes, MVCFramework.Commons;
{ TMVCJsonDataObjectsSerializer } { TMVCJsonDataObjectsSerializer }
@ -198,6 +130,7 @@ begin
GetTypeSerializers.Add(System.TypeInfo(TStream), TStreamSerializerJsonDataObject.Create); GetTypeSerializers.Add(System.TypeInfo(TStream), TStreamSerializerJsonDataObject.Create);
GetTypeSerializers.Add(System.TypeInfo(TStringStream), TStreamSerializerJsonDataObject.Create); GetTypeSerializers.Add(System.TypeInfo(TStringStream), TStreamSerializerJsonDataObject.Create);
GetTypeSerializers.Add(System.TypeInfo(TMemoryStream), TStreamSerializerJsonDataObject.Create); GetTypeSerializers.Add(System.TypeInfo(TMemoryStream), TStreamSerializerJsonDataObject.Create);
GetTypeSerializers.Add(System.TypeInfo(TMVCStringDictionary), TMVCStringDictionarySerializer.Create);
end; end;
procedure TMVCJsonDataObjectsSerializer.AttributeToJsonDataValue( procedure TMVCJsonDataObjectsSerializer.AttributeToJsonDataValue(
@ -306,18 +239,26 @@ begin
ChildObject := AValue.AsObject; ChildObject := AValue.AsObject;
if Assigned(ChildObject) then if Assigned(ChildObject) then
begin begin
ChildList := TDuckTypedList.Wrap(ChildObject); if ChildObject is TDataSet then
if Assigned(ChildList) then
begin begin
ChildJsonArray := AJsonObject.A[AName]; ChildJsonArray := AJsonObject.A[AName];
for Obj in ChildList do DataSetToJsonArray(TDataSet(ChildObject), ChildJsonArray, TMVCNameCase.ncLowerCase, []);
if Assigned(Obj) then
ObjectToJsonObject(Obj, ChildJsonArray.AddObject, GetSerializationType(Obj, AType), AIgnored);
end end
else else
begin begin
ChildJsonObject := AJsonObject.O[AName]; ChildList := TDuckTypedList.Wrap(ChildObject);
ObjectToJsonObject(ChildObject, ChildJsonObject, GetSerializationType(ChildObject, AType), AIgnored); if Assigned(ChildList) then
begin
ChildJsonArray := AJsonObject.A[AName];
for Obj in ChildList do
if Assigned(Obj) then
ObjectToJsonObject(Obj, ChildJsonArray.AddObject, GetSerializationType(Obj, AType), AIgnored);
end
else
begin
ChildJsonObject := AJsonObject.O[AName];
ObjectToJsonObject(ChildObject, ChildJsonObject, GetSerializationType(ChildObject, AType), AIgnored);
end;
end; end;
end end
else else
@ -780,7 +721,6 @@ begin
TFieldType.ftGuid: TFieldType.ftGuid:
Field.AsGuid := StringToGUID(AJsonObject.S[name]); Field.AsGuid := StringToGUID(AJsonObject.S[name]);
{$ENDIF} {$ENDIF}
TFieldType.ftGraphic, TFieldType.ftBlob, TFieldType.ftStream: TFieldType.ftGraphic, TFieldType.ftBlob, TFieldType.ftStream:
begin begin
SS := TStringStream.Create(AJsonObject.S[name]); SS := TStringStream.Create(AJsonObject.S[name]);
@ -844,17 +784,18 @@ begin
for Prop in ObjType.GetProperties do for Prop in ObjType.GetProperties do
begin begin
{$IFDEF AUTOREFCOUNT} {$IFDEF AUTOREFCOUNT}
if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then
Continue; Continue;
{$ENDIF} {$ENDIF}
if (Prop.IsWritable or Prop.GetValue(AObject).IsObject) and
if (Prop.IsWritable or Prop.GetValue(AObject).IsObject) and (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and (not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and
(not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then
begin begin
AttributeValue := Prop.GetValue(AObject); AttributeValue := Prop.GetValue(AObject);
JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType), AttributeValue, AType, AIgnoredAttributes, Prop.GetAttributes); JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType), AttributeValue,
AType, AIgnoredAttributes, Prop.GetAttributes);
if (not AttributeValue.IsEmpty) and Prop.IsWritable then if (not AttributeValue.IsEmpty) and Prop.IsWritable then
Prop.SetValue(AObject, AttributeValue); Prop.SetValue(AObject, AttributeValue);
end; end;
@ -863,10 +804,12 @@ begin
stFields: stFields:
begin begin
for Fld in ObjType.GetFields do for Fld in ObjType.GetFields do
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and (not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and
(not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then
begin begin
AttributeValue := Fld.GetValue(AObject); AttributeValue := Fld.GetValue(AObject);
JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), AttributeValue, AType, AIgnoredAttributes, Fld.GetAttributes); JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), AttributeValue, AType,
AIgnoredAttributes, Fld.GetAttributes);
if not AttributeValue.IsEmpty then if not AttributeValue.IsEmpty then
Fld.SetValue(AObject, AttributeValue); Fld.SetValue(AObject, AttributeValue);
end; end;
@ -874,11 +817,8 @@ begin
end; end;
end; end;
procedure TMVCJsonDataObjectsSerializer.ObjectToJsonObject( procedure TMVCJsonDataObjectsSerializer.ObjectToJsonObject(const AObject: TObject; const AJsonObject: TJsonObject;
const AObject: TObject; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const AJsonObject: TJsonObject;
const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList);
var var
ObjType: TRttiType; ObjType: TRttiType;
Prop: TRttiProperty; Prop: TRttiProperty;
@ -891,28 +831,29 @@ begin
for Prop in ObjType.GetProperties do for Prop in ObjType.GetProperties do
begin begin
{$IFDEF AUTOREFCOUNT} {$IFDEF AUTOREFCOUNT}
if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then
Continue; Continue;
{$ENDIF} {$ENDIF}
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and (not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then (not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then
AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType), Prop.GetValue(AObject), AType, AIgnoredAttributes, Prop.GetAttributes); AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType),
Prop.GetValue(AObject), AType, AIgnoredAttributes, Prop.GetAttributes);
end; end;
end; end;
stFields: stFields:
begin begin
for Fld in ObjType.GetFields do for Fld in ObjType.GetFields do
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and (not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and
AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), Fld.GetValue(AObject), AType, AIgnoredAttributes, Fld.GetAttributes); (not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then
AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), Fld.GetValue(AObject),
AType, AIgnoredAttributes, Fld.GetAttributes);
end; end;
end; end;
end; end;
function TMVCJsonDataObjectsSerializer.SerializeCollection( function TMVCJsonDataObjectsSerializer.SerializeCollection(const AList: TObject; const AType: TMVCSerializationType;
const AList: TObject; const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList): string; const AIgnoredAttributes: TMVCIgnoredList): string;
var var
JsonArray: TJsonArray; JsonArray: TJsonArray;
@ -942,9 +883,7 @@ begin
end; end;
end; end;
function TMVCJsonDataObjectsSerializer.SerializeDataSet( function TMVCJsonDataObjectsSerializer.SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList;
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList;
const ANameCase: TMVCNameCase): string; const ANameCase: TMVCNameCase): string;
var var
JsonArray: TJsonArray; JsonArray: TJsonArray;
@ -957,7 +896,7 @@ begin
JsonArray := TJsonArray.Create; JsonArray := TJsonArray.Create;
try try
BookMark := ADataSet.Bookmark; BookMark := ADataSet.BookMark;
ADataSet.First; ADataSet.First;
while not ADataSet.Eof do while not ADataSet.Eof do
begin begin
@ -973,10 +912,8 @@ begin
end; end;
end; end;
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord( function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(const ADataSet: TDataSet;
const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase): string;
const AIgnoredFields: TMVCIgnoredList;
const ANameCase: TMVCNameCase): string;
var var
JsonObject: TJsonObject; JsonObject: TJsonObject;
begin begin
@ -1015,12 +952,8 @@ end;
// end; // end;
// end; // end;
function TMVCJsonDataObjectsSerializer.SerializeObject( function TMVCJsonDataObjectsSerializer.SerializeObject(const AObject: TObject; const AType: TMVCSerializationType;
const AObject: TObject; const AIgnoredAttributes: TMVCIgnoredList; const ASerializationAction: TMVCSerializationAction): string;
const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList;
const ASerializationAction: TMVCSerializationAction
): string;
var var
JsonObject: TJsonObject; JsonObject: TJsonObject;
ChildJsonValue: TJsonBaseObject; ChildJsonValue: TJsonBaseObject;
@ -1034,8 +967,11 @@ begin
if AObject is TJsonBaseObject then if AObject is TJsonBaseObject then
Exit(TJsonBaseObject(AObject).ToJSON(True)); Exit(TJsonBaseObject(AObject).ToJSON(True));
if AObject is MVCFramework.TypesAliases.TJSONValue then if AObject is TDataSet then
Exit(MVCFramework.TypesAliases.TJSONValue(AObject).ToJSON); Exit(self.SerializeDataSet(TDataSet(AObject)));
if AObject is MVCFramework.TypesAliases.TJsonValue then
Exit(MVCFramework.TypesAliases.TJsonValue(AObject).ToJSON);
ObjType := GetRttiContext.GetType(AObject.ClassType); ObjType := GetRttiContext.GetType(AObject.ClassType);
if GetTypeSerializers.ContainsKey(ObjType.Handle) then if GetTypeSerializers.ContainsKey(ObjType.Handle) then
@ -1048,7 +984,8 @@ begin
if ChildJsonValue is TJsonBaseObject then if ChildJsonValue is TJsonBaseObject then
Result := ChildJsonValue.ToJSON(True) Result := ChildJsonValue.ToJSON(True)
else else
raise EMVCSerializationException.Create('Cannot serialize, the serializer does not have a valid TJsonBaseObject type.'); raise EMVCSerializationException.Create
('Cannot serialize, the serializer does not have a valid TJsonBaseObject type.');
finally finally
ChildJsonValue.Free; ChildJsonValue.Free;
end; end;
@ -1065,11 +1002,8 @@ begin
end; end;
end; end;
procedure TMVCJsonDataObjectsSerializer.DeserializeObject( procedure TMVCJsonDataObjectsSerializer.DeserializeObject(const ASerializedObject: string; const AObject: TObject;
const ASerializedObject: string; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const AObject: TObject;
const AType: TMVCSerializationType;
const AIgnoredAttributes: TMVCIgnoredList);
var var
JsonObject: TJsonObject; JsonObject: TJsonObject;
ObjType: TRttiType; ObjType: TRttiType;

View File

@ -2,7 +2,7 @@
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team // Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
// //
// https://github.com/danieleteti/delphimvcframework // https://github.com/danieleteti/delphimvcframework
// //

View File

@ -1224,6 +1224,7 @@ begin
try try
lReq.Method := 'mynotify'; lReq.Method := 'mynotify';
FExecutor.ExecuteNotification(lReq); FExecutor.ExecuteNotification(lReq);
Assert.Pass();
finally finally
lReq.Free; lReq.Free;
end; end;