Skip to content

Commit

Permalink
Bugfix UUID export and Root Element added on import
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Dec 14, 2023
1 parent bfa7746 commit 54aeba4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/DataSet.Serialize.Export.pas
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const A
else
Result.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}(LKey, TJSONString.Create(FormatFloat(TDataSetSerializeConfig.GetInstance.Export.FormatFloat, LField.AsFloat)));
end;
TFieldType.ftString, TFieldType.ftWideString, TFieldType.ftMemo, TFieldType.ftWideMemo, TFieldType.ftGuid, TFieldType.ftFixedChar, TFieldType.ftFixedWideChar:
TFieldType.ftGuid:
Result.{$IF DEFINED(FPC)}Add{$ELSE}AddPair{$ENDIF}(LKey, TJSONString.Create(LField.AsWideString));
TFieldType.ftString, TFieldType.ftWideString, TFieldType.ftMemo, TFieldType.ftWideMemo, TFieldType.ftFixedChar, TFieldType.ftFixedWideChar:
begin
LStringValue := Trim(LField.AsWideString);
if TDataSetSerializeConfig.GetInstance.Export.TryConvertStringToJson then
Expand Down
50 changes: 46 additions & 4 deletions src/DataSet.Serialize.pas
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ TDataSetSerializeHelper = class Helper for TDataSet
/// </remarks>
procedure LoadFromJSON(const AJSONObject: TJSONObject; const AOwns: Boolean = True); overload;
/// <summary>
/// Loads the DataSet with data from a JSON object.
/// </summary>
/// <param name="AJSONObject">
/// Refers to JSON that you want to load.
/// </param>
/// <param name="ARootElement">
/// Elemento raiz do JSON
/// </param>
/// <param name="AOwns">
/// Destroy JSON in the end.
/// </param>
/// <remarks>
/// Only the keys that make up the DataSet field list will be loaded. The JSON keys must have the same name as the
/// DataSet fields. It's not case-sensitive.
/// </remarks>
procedure LoadFromJSON(const AJSONObject: TJSONObject; const ARootElement: string = ''; const AOwns: Boolean = True); overload;
/// <summary>
/// Loads the DataSet with data from a JSON array.
/// </summary>
/// <param name="AJSONArray">
Expand All @@ -170,11 +187,14 @@ TDataSetSerializeHelper = class Helper for TDataSet
/// <param name="AJSONString">
/// Refers to JSON that you want to load.
/// </param>
/// <param name="ARootElement">
/// Elemento raiz do JSON
/// </param>
/// <remarks>
/// Only the keys that make up the DataSet field list will be loaded. The JSON keys must have the same name as the
/// DataSet fields. It's not case-sensitive.
/// </remarks>
procedure LoadFromJSON(const AJSONString: string); overload;
procedure LoadFromJSON(const AJSONString: string; const ARootElement: string = ''); overload;
/// <summary>
/// Updates the DataSet data with JSON object data.
/// </summary>
Expand Down Expand Up @@ -326,10 +346,32 @@ procedure TDataSetSerializeHelper.LoadFromJSON(const AJSONArray: TJSONArray; con
end;

procedure TDataSetSerializeHelper.LoadFromJSON(const AJSONObject: TJSONObject; const AOwns: Boolean = True);
begin
LoadFromJSON(AJSONObject, EmptyStr, AOwns);
end;

procedure TDataSetSerializeHelper.LoadFromJSON(const AJSONObject: TJSONObject; const ARootElement: string = ''; const AOwns: Boolean = True);
var
LJSON: TJSONValue;
LJSONSerialize: TJSONSerialize;
begin
LJSONSerialize := TJSONSerialize.Create(AJSONObject, AOwns);
if ARootElement.Trim.IsEmpty then
LJSONSerialize := TJSONSerialize.Create(AJSONObject, AOwns)
else
begin
try
LJSON := AJSONObject.FindValue(ARootElement);
if not Assigned(LJSON) then
raise Exception.Create('Root element not found!');
if LJSON.InheritsFrom(TJSONArray) then
LJSONSerialize := TJSONSerialize.Create(LJSON.Clone as TJSONArray, True)
else
LJSONSerialize := TJSONSerialize.Create(LJSON.Clone as TJSONObject, True);
finally
if AOwns then
AJSONObject.Free;
end;
end;
try
LJSONSerialize.ToDataSet(Self);
finally
Expand Down Expand Up @@ -369,10 +411,10 @@ function TDataSetSerializeHelper.ValidateJSON(const AJSONString: string; const A
Result := TJSONArray.Create();
end;

procedure TDataSetSerializeHelper.LoadFromJSON(const AJSONString: string);
procedure TDataSetSerializeHelper.LoadFromJSON(const AJSONString: string; const ARootElement: string = '');
begin
if Trim(AJSONString).StartsWith('{') then
LoadFromJSON({$IF DEFINED(FPC)}GetJSON(AJSONString){$ELSE}TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(AJSONString), 0){$ENDIF} as TJSONObject)
LoadFromJSON({$IF DEFINED(FPC)}GetJSON(AJSONString){$ELSE}TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(AJSONString), 0){$ENDIF} as TJSONObject, ARootElement)
else if Trim(AJSONString).StartsWith('[') then
LoadFromJSON({$IF DEFINED(FPC)}GetJSON(AJSONString){$ELSE}TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(AJSONString), 0){$ENDIF} as TJSONArray);
end;
Expand Down

0 comments on commit 54aeba4

Please sign in to comment.