Skip to content

Commit

Permalink
Now the ManageInstance routine gets called before the custom serializ…
Browse files Browse the repository at this point in the history
…er starts
  • Loading branch information
paolo-rossi committed Feb 19, 2023
1 parent 48037e5 commit 856c0d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
33 changes: 24 additions & 9 deletions Source/Neon.Core.Persistence.JSON.pas
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ TNeonDeserializerJSON = class(TNeonBase, IDeserializerContext)
/// <summary>
/// Decides to whether or not to create the object and assigning it to the reference
/// </summary>
function ManageReference(const AParam: TNeonDeserializerParam; const AData: TValue): TValue;
function ReadReference(const AParam: TNeonDeserializerParam; const AData: TValue): TValue;

/// <summary>
/// Decides to whether or not to create the object and assigning it to the reference
/// </summary>
function ManageInstance(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject): TValue;
private
/// <summary>
/// reader for string types
Expand Down Expand Up @@ -1312,14 +1317,16 @@ function TNeonDeserializerJSON.ReadDataMember(const AParam: TNeonDeserializerPar
const AData: TValue; ACustomProcess: Boolean): TValue;
var
LCustom: TCustomSerializer;
LValue: TValue;
begin
if ACustomProcess then
begin
// if there is a custom serializer
LCustom := FConfig.Serializers.GetSerializer(AParam.RttiType.Handle);
if Assigned(LCustom) then
begin
Result := LCustom.Deserialize(AParam.JSONValue, AData, AParam.NeonObject, Self);
LValue := ManageInstance(AParam.JSONValue, AData, AParam.NeonObject);
Result := LCustom.Deserialize(AParam.JSONValue, LValue, AParam.NeonObject, Self);
Exit(Result);
end;
end;
Expand Down Expand Up @@ -1348,7 +1355,7 @@ function TNeonDeserializerJSON.ReadDataMember(const AParam: TNeonDeserializerPar
tkClass:
begin
if TJSONUtils.HasValues(AParam.JSONValue) then
Result := ManageReference(AParam, AData);
Result := ReadReference(AParam, AData);
end;

tkInterface: Result := ReadInterface(AParam, AData);
Expand Down Expand Up @@ -1873,18 +1880,26 @@ function TNeonDeserializerJSON.JSONToTValue(AJSON: TJSONValue; AType: TRttiType;
Result := ReadDataMember(AJSON, AType, AData);
end;

function TNeonDeserializerJSON.ManageReference(const AParam: TNeonDeserializerParam; const AData: TValue): TValue;
function TNeonDeserializerJSON.ManageInstance(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject): TValue;
var
LType: TRttiType;
LValue: TValue;
begin
LValue := AData;
if (AData.AsObject = nil) and
(FConfig.AutoCreate or AParam.NeonObject.NeonAutoCreate) then
Result := AData;
if (AData.IsObject) and (AData.AsObject = nil) and
TJSONUtils.HasValues(AValue) and
(FConfig.AutoCreate or ANeonObject.NeonAutoCreate) then
begin
LType := TRttiUtils.Context.GetType(AData.TypeInfo);
LValue := TRttiUtils.CreateInstance(LType);
Result := TRttiUtils.CreateInstance(LType);
end;
end;

function TNeonDeserializerJSON.ReadReference(const AParam:
TNeonDeserializerParam; const AData: TValue): TValue;
var
LValue: TValue;
begin
LValue := ManageInstance(AParam.JSONValue, AData, AParam.NeonObject);

if ReadEnumerableMap(AParam, LValue) then
Result := LValue
Expand Down
2 changes: 1 addition & 1 deletion Source/Neon.Core.Persistence.pas
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ function TNeonRttiMembers.NewMember(AMember: TRttiMember): TNeonRttiMember;

function TNeonRttiObject.AsRttiType: TRttiType;
begin
// Trhows an exception if not (is better than returnig nil and have to check outside)
// Throws an exception if not (is better than returnig nil and have to check outside)
Result := FRttiObject as TRttiType;
end;

Expand Down
15 changes: 14 additions & 1 deletion Tests/Neon.Tests.Framework.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Cfg_2)'=='true') or '$(Cfg_2_Win32)'!=''">
<Cfg_2_Win32>true</Cfg_2_Win32>
<CfgParent>Cfg_2</CfgParent>
<Cfg_2>true</Cfg_2>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''">
<DCC_DcuOutput>.\$(Platform)\$(Config)</DCC_DcuOutput>
<DCC_ExeOutput>.\Bin</DCC_ExeOutput>
Expand All @@ -61,8 +67,9 @@
<DCC_UsePackage>DBXSqliteDriver;fmxase;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;tethering;svnui;JvGlobus;FireDACADSDriver;SMCmpntRX102;uniGUI25Chart;frxe25;JvPluginSystem;DBXMSSQLDriver;JvMM;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;JvBands;vcldb;bindcompfmx;svn;JvJans;DBXOracleDriver;unidac250;JvNet;inetdb;JvAppFrm;RaizeComponentsVcl;FmxTeeUI;emsedge;JvDotNetCtrls;fmx;fmxdae;frxDB25;RaizeComponentsVclDb;JvWizards;frxTee25;FireDACDBXDriver;dbexpress;IndyCore;vclx;InspexVclDb;JvPageComps;dsnap;DataSnapCommon;WiRLRunTime;JvDB;DataSnapConnectors;uniGUI25m;VCLRESTComponents;JclDeveloperTools;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;FireDACMySQLDriver;DBXFirebirdDriver;vquery250;JvCmp;JvHMI;FireDACCommonODBC;DataSnapClient;IndyIPCommon;bindcompdbx;JvCustom;vcl;IndyIPServer;DBXSybaseASEDriver;JvXPCtrls;dac250;IndySystem;PngComponents;FireDACDb2Driver;dsnapcon;madExcept_;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;madBasic_;TeeDB;Jcl;JvCore;emshosting;JvCrypt;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;DbxCommonDriver;JvDlgs;JvRuntimeDesign;JvManagedThreads;Tee;DataSnapServer;xmlrtl;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;unidacfmx250;DbxClientDriver;dacvcl250;DBXSybaseASADriver;JvTimeFramework;CustomIPTransport;vcldsnap;TechnoFramework;JvSystem;JvStdCtrls;frx25;CodeSiteExpressPkg;madDisAsm_;bindcomp;appanalytics;DBXInformixDriver;NativeExcelTokyo;IndyIPClient;bindcompvcl;SynEdit_R;TeeUI;JvDocking;dbxcds;VclSmp;unidacvcl250;adortl;FireDACODBCDriver;JvPascalInterpreter;JclVcl;DataSnapIndy10ServerTransport;dacfmx250;InspexVcl;uniGUI25VCL;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;inetdbxpress;FireDACMongoDBDriver;JvControls;JvPrintPreview;JclContainers;uSynEdit_R2021;DataSnapServerMidas;$(DCC_UsePackage)</DCC_UsePackage>
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
<BT_BuildType>Debug</BT_BuildType>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<Manifest_File>(None)</Manifest_File>
<VerInfo_Locale>1033</VerInfo_Locale>
<AppDPIAwarenessMode>none</AppDPIAwarenessMode>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win64)'!=''">
<DCC_UsePackage>DBXSqliteDriver;fmxase;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;tethering;FireDACADSDriver;uniGUI25Chart;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;DBXOracleDriver;inetdb;RaizeComponentsVcl;FmxTeeUI;emsedge;fmx;fmxdae;RaizeComponentsVclDb;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;DataSnapConnectors;uniGUI25m;VCLRESTComponents;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;DataSnapClient;IndyIPCommon;bindcompdbx;vcl;IndyIPServer;DBXSybaseASEDriver;IndySystem;PngComponents;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;emshosting;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;DbxCommonDriver;Tee;DataSnapServer;xmlrtl;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;SynEdit_R;TeeUI;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;uniGUI25VCL;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;inetdbxpress;FireDACMongoDBDriver;uSynEdit_R2021;DataSnapServerMidas;$(DCC_UsePackage)</DCC_UsePackage>
Expand All @@ -87,6 +94,11 @@
<DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
<DCC_DebugInformation>0</DCC_DebugInformation>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2_Win32)'!=''">
<VerInfo_Locale>1033</VerInfo_Locale>
<Manifest_File>(None)</Manifest_File>
<AppDPIAwarenessMode>none</AppDPIAwarenessMode>
</PropertyGroup>
<ItemGroup>
<DelphiCompile Include="$(MainSource)">
<MainSource>MainSource</MainSource>
Expand Down Expand Up @@ -132,6 +144,7 @@
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k280.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclofficexp280.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="C:\WINDOWS\system32\dclSvComV8D28.bpl">File C:\WINDOWS\system32\dclSvComV8D28.bpl not found</Excluded_Packages>
</Excluded_Packages>
</Delphi.Personality>
<Deployment Version="4">
Expand Down

0 comments on commit 856c0d1

Please sign in to comment.