Created
August 30, 2022 07:49
-
-
Save delphiapp/0e05d129c717f5af3acb84e0f2697c17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ModelListSV": | |
[ | |
{ | |
"SubData": { | |
"FileName": "example" | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program SerializeErrormORMot4; | |
{$APPTYPE CONSOLE} | |
{$DEFINE VER2} | |
{$IFDEF FPC} | |
{$MODE Delphi} | |
{$ENDIF} | |
uses | |
// FastMM4, | |
// FastMM4Messages, | |
{$IFDEF FPC} | |
Interfaces, | |
{$ENDIF} | |
SysUtils, Dialogs, Forms | |
{$IFNDEF VER2} | |
,SynCommons, mORMot | |
{$ELSE} | |
,mormot.core.base, mormot.core.data, mormot.core.rtti, mormot.rest.core, mormot.core.json, mormot.core.text | |
, mormot.core.os, mormot.core.variants, mormot.core.datetime | |
{$ENDIF} | |
; | |
type | |
TSubData = class(TSynPersistent) | |
private | |
fFileName:string; | |
published | |
property FileName:string read ffilename; | |
end; | |
TModelSV = class(TSynAutoCreateFields) | |
private | |
fSubData:TSubData; | |
published | |
property SubData:TSubData read fSubData; | |
end; | |
TModelSVObjArray = array of TModelSV; | |
TModelApp = class(TSynAutoCreateFields) | |
private | |
fModelListSV : TModelSVObjArray; | |
public | |
function LoadConfiguration(AFileName:string):Boolean; | |
procedure SaveConfiguration(AFileName:string); | |
procedure AddModelSV; | |
published | |
property ModelListSV:TModelSVObjArray read fModelListSV; | |
end; | |
{ TModelApp } | |
procedure TModelApp.AddModelSV; | |
var fModelSV:TModelSV; | |
begin | |
fModelSV :=TModelSV.create; | |
fModelSV.SubData.ffilename:='example'; | |
ObjArrayAdd(fModelListSV,fModelSV); | |
end; | |
function TModelApp.LoadConfiguration(AFileName: string): Boolean; | |
begin | |
result:=JsonFileToObject(AFileName,Self,nil,{$IFDEF VER2}JSONPARSER_TOLERANTOPTIONS+[jpoClearValues]{$ELSE}JSONTOOBJECT_TOLERANTOPTIONS{$ENDIF});// | |
end; | |
var ModelApp:TModelApp; | |
procedure TModelApp.SaveConfiguration(AFileName: string); | |
begin | |
ObjectToJSONFile(Self,AFileName); | |
end; | |
begin | |
try | |
{$IFDEF VER2} | |
Rtti.RegisterObjArray(TypeInfo(TModelSVObjArray),TModelSV); | |
{$ELSE} | |
TJSONSerializer.RegisterObjArrayForJSON(TypeInfo(TModelSVObjArray),TModelSV); | |
{$ENDIF} | |
except | |
on E:ESynException do ShowMessage(E.Message); | |
end; | |
ModelApp:=TModelApp.Create; | |
try | |
// ModelApp.AddModelSV; | |
// ModelApp.SaveConfiguration('SaveAll.json'); | |
ModelApp.LoadConfiguration(ExtractFilePath(Application.ExeName)+'SaveAll.json'); | |
// ObjArrayClear(modelApp.fModelListSV); | |
ModelApp.LoadConfiguration(ExtractFilePath(Application.ExeName)+'SaveAll.json'); | |
finally | |
ModelApp.Free; | |
end; | |
Readln; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment