Advertisement
filhotecmail

JsonToDataset

Sep 13th, 2020 (edited)
1,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.06 KB | None | 0 0
  1. function DataSet.JsonToDataset(aJSON : string; Arg: TProc<TDataset>): String;
  2. var
  3.   JObj: TJSONArray;
  4.   vConv : TCustomJSONDataSetAdapter;
  5.   FmemoryDataset: TFDMemTable;
  6. begin
  7.   Result := aJSON;
  8.   if (aJSON = EmptyStr) then
  9.   begin
  10.     Exit;
  11.   end;
  12.   FmemoryDataset:= TFDMemTable.Create(nil);
  13.   JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
  14.   vConv := TCustomJSONDataSetAdapter.Create(Nil);
  15.   try
  16.     vConv.Dataset := FmemoryDataset;
  17.     vConv.UpdateDataSet(JObj);
  18.     if Assigned(Arg) then
  19.        Arg(FmemoryDataset);
  20.   finally
  21.     vConv.Free;
  22.     JObj.Free;
  23.     FreeAndNil( FmemoryDataset );
  24.   end;
  25.  end;
  26.  
  27. function DataSet.JsonToMemoryDataset<TJsonString>(AJsonString: String; Arg: TProc<TDataset>): TDataset;
  28.  
  29. begin
  30.  Result := self;
  31.  JsonToDataset(AJsonString,Arg);
  32. end;
  33.  
  34. function DataSet.TryField<T>(const aFieldName: String; Arg: T): Variant;
  35.  var
  36.    FField: TField;
  37. begin
  38.  try
  39.   FFIeld:= FindField(aFieldName);
  40.  finally
  41.  if FField = nil then
  42.     Result :=  Any.From(Arg).AsVariant
  43.     else
  44.     Result:= FField.Value;
  45.  end;
  46.  end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement