class function TUtilJSON.CheckXDataRef( AJSONObject: TJSONObject; AName:
String): TJSONObject;
begin
// Wenn Entity und nicht Entity@ref
if HasValue( AJSONObject, AName)
and not HasValue( AJSONObject, AName + cXDataRef)
then
// dann neuer Eintrag mit ref anhängen
AJSONObject.AddPair( AName + cXDataRef, AJSONObject.Values[ AName]);
// falls noch vorhanden, Entity (ohne ref) löschen
if HasValue( AJSONObject, AName)
then
AJSONObject.RemovePair( AName).Free;
// <=== hier nach ist der Zugriff auf AJSONObject nicht mehr möglich, da gelöscht.
// und zurückgeben
Result := AJSONObject;
end;
class function TUtilJSON.CheckIDValue( AJSONObject: TJSONObject; AName:
String): TJSONObject;
var
LIDValue:
String;
begin
if HasValue( AJSONObject, AName)
then begin
// Eintrag löschen und mit bereinigtem ID-Value neu erzeugen
LIDValue := AJSONObject.Values[ AName].AsType<
String>;
AJSONObject.RemovePair( AName).Free;
// und mit bereinigtem ID-Value neu erzeugen
AJSONObject.AddPair( AName, DirtyStringToGuidStr( LIDValue));
end;
Result := AJSONObject;
end;
class function TUtilJSON.CheckJSONEntity( AObj: TJSONObject; AForInsert: Boolean; AEntity, AIDField:
String;
AInsertArr, AFieldArr: TStringDynArray): TJSONObject;
var
i: Integer;
LFeldname:
String;
begin
// *** Falls Objekt im Feld liegt ***
if TUtilJSON.HasObject( AObj, AEntity)
then
AObj := AObj.GetValue<TJSONObject>( AEntity);
// *** ID-neu Prüfung ***
AObj := TUtilJSON.CheckNewID( AObj, AIDField, AForInsert);
if AForInsert
then begin
// *** alle Insert-Felder prüfen ***
for i := Low( AInsertArr)
to High( AInsertArr)
do begin
LFeldname := AInsertArr[i];
// falls Referenz
if copy( LFeldname, 1, 1) = '
@'
then begin
delete( LFeldname, 1, 1);
// Feldname
AObj := TUtilJSON.CheckXDataRef( AObj, LFeldname);
LFeldname := LFeldname + cXDataRef;
// Value (ID bereinigen)
AObj := TUtilJSON.CheckIDValue( AObj, LFeldname);
end;
// Prüfung
if not TUtilJSON.HasValue( AObj, LFeldname)
then
raise Exception.Create('
Feld ' + LFeldname + _nichtVorhanden);
end;
end else begin
// *** alle Felder prüfen ***
for i := Low( AFieldArr)
to High( AFieldArr)
do begin
LFeldname := AFieldArr[i];
// falls Referenz
if copy( LFeldname, 1, 1) = '
@'
then begin
delete( LFeldname, 1, 1);
AObj := TUtilJSON.CheckXDataRef( AObj, LFeldname);
LFeldname := LFeldname + cXDataRef;
end;
// Prüfung
if not TUtilJSON.HasValue( AObj, LFeldname)
then
raise Exception.Create('
Feld ' + LFeldname + _nichtVorhanden);
end;
end;
Result := AObj;
end;