-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathOpenAI.Utils.JSON.Cleaner.pas
263 lines (233 loc) · 7.57 KB
/
OpenAI.Utils.JSON.Cleaner.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
unit OpenAI.Utils.JSON.Cleaner;
interface
uses
System.Rtti,
System.JSON,
System.TypInfo,
System.SysUtils,
System.Classes,
System.Generics.Collections;
type
IJSONCleaner<T: class> = interface
['{DB75F095-ADE7-4ABF-8986-F9CCB0FD578D}']
/// <summary>
/// Cleans a JSON string by removing fields that are not present in the corresponding class.
/// </summary>
/// <param name="AJSON">The JSON string to be cleaned.</param>
/// <returns>A cleaned JSON string.</returns>
function CleanJSON(const AJSON: string): string;
end;
TJSONCleaner<T: class> = class(TInterfacedObject, IJSONCleaner<T>)
private
/// <summary>
/// Cleans a JSON object by removing fields that are not present in the corresponding class.
/// </summary>
/// <param name="AJSONObject">The JSON object to be cleaned.</param>
/// <param name="AType">The RTTI type of the corresponding Delphi class.</param>
procedure CleanJSONObject(AJSONObject: TJSONObject; AType: TRttiType);
/// <summary>
/// Cleans a JSON array by applying the cleaning process to each item in the array.
/// </summary>
/// <param name="AJSONArray">The JSON array to be cleaned.</param>
/// <param name="AType">The RTTI type of the items in the array.</param>
procedure CleanJSONArray(AJSONArray: TJSONArray; AType: TRttiType);
/// <summary>
/// Obtains the generic type of a list from the RTTI type.
/// </summary>
/// <param name="AType">The RTTI type of the TObjectList.</param>
/// <param name="ATypeListName">The name of the list type.</param>
/// <returns>The RTTI type of the generic item in the list.</returns>
function GetGenericClassType(AType: TRttiType; const ATypeListName: String): TRttiType;
/// <summary>
/// Removes the prefix 'F' (uppercase or lowercase) from the beginning of a string, if present.
/// </summary>
/// <param name="AValue">The string from which the prefix 'F' should be removed.</param>
/// <returns>
/// The string without the 'F' prefix at the beginning, if present; otherwise, returns the original string.
/// </returns>
function RemovePrefixF(const AValue: string): string;
public
/// <summary>
/// Creates an instance of the class.
/// </summary>
/// <returns>
/// Returns the instance of the class.
/// </returns>
class function New: IJSONCleaner<T>;
/// <summary>
/// Cleans a JSON string by removing fields that are not present in the corresponding class.
/// </summary>
/// <param name="AJSON">The JSON string to be cleaned.</param>
/// <returns>A cleaned JSON string.</returns>
function CleanJSON(const AJSON: string): string;
end;
implementation
uses
REST.JSON.Types;
procedure TJSONCleaner<T>.CleanJSONObject(AJSONObject: TJSONObject; AType: TRttiType);
var
LField: TRttiField;
LContext: TRttiContext;
LFields: TDictionary<string, TRttiField>;
LPair: TJSONPair;
LListToRemove: TList<string>;
LPairValue: string;
LJSONPair: TJSONPair;
LPropType, LPropTypeAux: TRttiType;
LAttribute: TCustomAttribute;
LFieldName: String;
begin
if not Assigned(AJSONObject) or (AJSONObject.toJSON.ToLower = 'null') then
Exit;
LContext := TRttiContext.Create;
try
LFields := TDictionary<string, TRttiField>.Create;
try
for LField in AType.GetFields do
begin
LFieldName := EmptyStr;
for LAttribute in LField.GetAttributes do
begin
if LAttribute is JSONNameAttribute then
begin
{$IF CompilerVersion >= 36}
LFieldName := JSONNameAttribute(LAttribute).Value;
{$ELSE}
LFieldName := JSONNameAttribute(LAttribute).Name;
{$ENDIF}
break;
end;
end;
if LFieldName.Trim.IsEmpty then
LFieldName := RemovePrefixF(LField.Name).ToLower;
LFields.TryAdd(LFieldName.ToLower, LField);
end;
LListToRemove := TList<string>.Create;
try
for LPair in AJSONObject do
begin
if not LFields.ContainsKey(LPair.JsonString.Value.ToLower) then
LListToRemove.Add(LPair.JsonString.Value)
else
begin
LPropType := LFields[LPair.JsonString.Value.ToLower].FieldType;
if LPropType.TypeKind = tkClass then
begin
if (LPropType.Name.Contains('TList<') or LPropType.Name.Contains('TObjectList<')) and LPropType.IsInstance
then
begin
LPropTypeAux := GetGenericClassType(LPropType, 'System.Generics.Collections.TObjectList<');
CleanJSONArray(TJSONArray(LPair.JsonValue), LPropTypeAux);
end
else
CleanJSONObject(TJSONObject(LPair.JsonValue), LPropType);
end
else if LPropType.TypeKind = tkDynArray then
begin
LPropTypeAux := GetGenericClassType(LPropType, 'System.TArray<');
CleanJSONArray(TJSONArray(LPair.JsonValue), LPropTypeAux);
end;
end;
end;
for LPairValue in LListToRemove do
begin
LJSONPair := AJSONObject.RemovePair(LPairValue);
if Assigned(LJSONPair) then
LJSONPair.Free;
end;
finally
LListToRemove.Free;
end;
finally
LFields.Free;
end;
finally
LContext.Free;
end;
end;
function TJSONCleaner<T>.GetGenericClassType(AType: TRttiType; const ATypeListName: String): TRttiType;
var
LContext: TRttiContext;
LRttiType: TRttiType;
LBaseType: TRttiType;
LTypeName: string;
LGenTypeName: string;
begin
Result := nil;
LContext := TRttiContext.Create;
try
LBaseType := LContext.GetType(AType.Handle);
if Assigned(LBaseType) then
begin
LTypeName := LBaseType.QualifiedName;
if LTypeName.StartsWith(ATypeListName) then
begin
LGenTypeName := Copy(LTypeName, Pos('<', LTypeName) + 1, Length(LTypeName) - Pos('<', LTypeName) - 1);
LRttiType := LContext.FindType(LGenTypeName);
Result := LRttiType;
end;
end;
finally
LContext.Free;
end;
end;
class function TJSONCleaner<T>.New: IJSONCleaner<T>;
begin
Result := Self.Create;
end;
function TJSONCleaner<T>.RemovePrefixF(const AValue: string): string;
begin
if (Length(AValue) > 0) and SameText(AValue[1], 'F') then
Result := Copy(AValue, 2, Length(AValue) - 1)
else
Result := AValue;
end;
procedure TJSONCleaner<T>.CleanJSONArray(AJSONArray: TJSONArray; AType: TRttiType);
var
LItem: TJSONValue;
LItemType: TRttiType;
LContext: TRttiContext;
begin
if not Assigned(AJSONArray) or (AJSONArray.toJSON.ToLower = 'null') or (AJSONArray.Count <= 0) then
Exit;
LContext := TRttiContext.Create;
try
for LItem in AJSONArray do
begin
if LItem is TJSONObject then
begin
LItemType := LContext.GetType(AType.AsInstance.MetaclassType);
CleanJSONObject(TJSONObject(LItem), LItemType);
end
else if LItem is TJSONArray then
begin
CleanJSONArray(TJSONArray(LItem), AType);
end;
end;
finally
LContext.Free;
end;
end;
function TJSONCleaner<T>.CleanJSON(const AJSON: string): string;
var
LJSONObject: TJSONObject;
LContext: TRttiContext;
LType: TRttiType;
begin
LJSONObject := TJSONObject.ParseJSONValue(AJSON) as TJSONObject;
if not Assigned(LJSONObject) then
raise Exception.Create('Invalid JSON format');
try
LContext := TRttiContext.Create;
try
LType := LContext.GetType(TClass(T));
CleanJSONObject(LJSONObject, LType);
Result := LJSONObject.toJSON;
finally
LContext.Free;
end;
finally
LJSONObject.Free;
end;
end;
end.