-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathSimpleEntity.pas
146 lines (124 loc) · 3.46 KB
/
SimpleEntity.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
unit SimpleEntity;
interface
uses
{$IFNDEF CONSOLE}
{$IFDEF FMX}
FMX.Forms,
{$ELSE}
Vcl.Forms,
{$ENDIF}
{$ENDIF}
Data.DB, System.Generics.Collections, System.SysUtils;
type
TSimpleEntity = class
public
{$IFNDEF CONSOLE}
function Parse(const aForm: TForm): TSimpleEntity; overload; virtual;
{$ENDIF}
function Parse(const aDataSet: TDataSet): TSimpleEntity; overload; virtual;
procedure Parse(const psJSON: string); overload;
procedure SaveToFileJSON(const poFileName: TFileName); overload; virtual;
procedure SaveToFileJSON(const poFileName: TFileName; poEncoding: TEncoding); overload; virtual;
function ToJSON: string;
function ToJSONRefletion: string;
end;
TSimpleEntityList<T: TSimpleEntity, constructor> = class(TObjectList<T>)
public
function Parse(const aDataSet: TDataSet): TSimpleEntityList<T>; virtual;
function ToJSON: string;
function ToJSONRefletion: string;
end;
implementation
uses
SimpleUtil, SimpleJSONUtil, System.Classes, System.JSON, System.StrUtils;
{ TSimpleEntity }
function TSimpleEntity.Parse(const aDataSet: TDataSet): TSimpleEntity;
begin
Result := Self;
TSimpleUtil.GetValuesFromDataset(aDataSet, Self);
end;
procedure TSimpleEntity.SaveToFileJSON(const poFileName: TFileName);
begin
SaveToFileJSON(poFileName, TEncoding.Default);
end;
procedure TSimpleEntity.Parse(const psJSON: string);
begin
TSimpleJsonUtil.JSONStringToObject(psJSON, Self);
end;
procedure TSimpleEntity.SaveToFileJSON(const poFileName: TFileName;
poEncoding: TEncoding);
var
oConteudo: TStringList;
begin
oConteudo := TStringList.Create;
try
oConteudo.Text := ToJSON;
if ForceDirectories(ExtractFilePath(poFileName)) then
oConteudo.SaveToFile(poFileName, poEncoding);
finally
FreeAndNil(oConteudo);
end;
end;
function DecodeUnicodeEscapes(psEscaped: string): string;
var
FoundPos: LongInt;
HexCode: String;
DecodedChars: String;
begin
Result := psEscaped;
FoundPos := Pos('\u', Result);
while (FoundPos <> 0) and (FoundPos < Length(Result) - 4) do
begin
HexCode := Copy(Result, FoundPos + 2, 4);
DecodedChars := WideChar(StrToInt('$' + HexCode));
Result := AnsiReplaceStr(Result, '\u' + HexCode,
Utf8ToAnsi(UTF8Encode(DecodedChars)));
FoundPos := Pos('\u', Result);
end;
Result := StringReplace(Result, '\/', '/', [rfReplaceAll])
end;
function TSimpleEntity.ToJSON: string;
begin
Result := TSimpleJsonUtil.ObjectToJSONString(Self);
end;
function TSimpleEntity.ToJSONRefletion: string;
var
oJSONValue: TJSONValue;
begin
oJSONValue := TSimpleJsonUtil.ObjectToJSON(Self);
try
Result := DecodeUnicodeEscapes(oJSONValue.ToJSON);
finally
FreeAndNil(oJSONValue);
end;
end;
{ TSimpleEntityList<T> }
function TSimpleEntityList<T>.Parse(const aDataSet: TDataSet): TSimpleEntityList<T>;
begin
Result := Self;
Self.Clear;
TSimpleUtil.DataSetToObjectList<T>(aDataSet, Self);
end;
{$IFNDEF CONSOLE}
function TSimpleEntity.Parse(const aForm: TForm): TSimpleEntity;
begin
Result := Self;
TSimpleUtil.GetObjectFromForm(aForm, Self);
end;
{$ENDIF}
function TSimpleEntityList<T>.ToJSON: string;
begin
Result := TSimpleJsonUtil.ListToJSONArrayString<T>(Self, [jfFormat]);
end;
function TSimpleEntityList<T>.ToJSONRefletion: string;
var
oJSONArray: TJSONArray;
begin
oJSONArray := TSimpleJsonUtil.ListToJSONArray<T>(Self);
try
Result := oJSONArray.ToJSON;
finally
FreeAndNil(oJSONArray);
end;
end;
end.