Skip to content

Commit

Permalink
Code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Jul 7, 2021
1 parent 3184318 commit 0293fee
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ procedure TFrmSamples.Button1Click(Sender: TObject);
begin
LJSONArray := mtUsers.ToJSONArray;
try
memoJSON.Lines.Text := {$IFDEF CompilerVersion > 32}LJSONArray.Format{$ELSE}LJSONArray.ToJSON{$ENDIF};
memoJSON.Lines.Text := {$IFDEF CompilerVersion < 33}LJSONArray.ToJSON{$ELSE}LJSONArray.Format{$ENDIF};
finally
LJSONArray.Free;
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ object FrmSamples: TFrmSamples
ReadOnly = True
ScrollBars = ssVertical
TabOrder = 1
ExplicitLeft = 3
ExplicitTop = -6
end
end
object Panel5: TPanel
Expand Down Expand Up @@ -464,7 +462,6 @@ object FrmSamples: TFrmSamples
Align = alClient
BevelOuter = bvNone
TabOrder = 1
ExplicitWidth = 722
object Panel16: TPanel
Left = 0
Top = 150
Expand All @@ -473,7 +470,6 @@ object FrmSamples: TFrmSamples
Align = alClient
BevelOuter = bvNone
TabOrder = 0
ExplicitWidth = 722
object Panel17: TPanel
Left = 0
Top = 0
Expand All @@ -492,7 +488,6 @@ object FrmSamples: TFrmSamples
ParentBackground = False
ParentFont = False
TabOrder = 0
ExplicitWidth = 722
end
object DBGrid5: TDBGrid
Left = 0
Expand Down Expand Up @@ -525,7 +520,6 @@ object FrmSamples: TFrmSamples
Align = alTop
BevelOuter = bvNone
TabOrder = 1
ExplicitWidth = 722
object Panel19: TPanel
Left = 0
Top = 0
Expand All @@ -544,7 +538,6 @@ object FrmSamples: TFrmSamples
ParentBackground = False
ParentFont = False
TabOrder = 0
ExplicitWidth = 722
end
object DBGrid6: TDBGrid
Left = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ procedure TFrmSamples.btnExportArrayValueClick(Sender: TObject);
begin
LJSONArray := mtUsers.ToJSONArray;
try
memoExportArrayValue.Lines.Text := LJSONArray.Format(2);
memoExportArrayValue.Lines.Text := {$IFDEF CompilerVersion < 33}LJSONArray.ToJSON{$ELSE}LJSONArray.Format{$ENDIF};
finally
LJSONArray.Free;
end;
Expand All @@ -109,7 +109,7 @@ procedure TFrmSamples.btnExportJSONArrayClick(Sender: TObject);
begin
LJSONArray := mtCountries.ToJSONArray;
try
memoExportedDataSetNested.Lines.Text := LJSONArray.Format(2);
memoExportedDataSetNested.Lines.Text := {$IFDEF CompilerVersion < 33}LJSONArray.ToJSON{$ELSE}LJSONArray.Format{$ENDIF};
finally
LJSONArray.Free;
end;
Expand Down
27 changes: 18 additions & 9 deletions src/DataSet.Serialize.Export.pas
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,35 @@ TDataSetSerialize = class
/// <param name="ADataSet">
/// Refers to the DataSet that you want to export the record.
/// </param>
/// <param name="AValue">
/// Only export the value when there is only 1 field in the DataSet.
/// </param>
/// <returns>
/// Returns a JSON object containing the record data.
/// </returns>
/// <remarks>
/// Invisible or null fields will not be exported.
/// </remarks>
function DataSetToJSONObject(const ADataSet: TDataSet; const AValue: Boolean = true): TJSONObject;
function DataSetToJSONObject(const ADataSet: TDataSet; const AValue: Boolean = True): TJSONObject;
/// <summary>
/// Creates an array of JSON objects with all DataSet records.
/// </summary>
/// <param name="ADataSet">
/// Refers to the DataSet that you want to export the records.
/// </param>
/// <param name="IsChild">
/// Inform if it's child records.
/// </param>
/// <param name="IsValue">
/// Inform if it's to export only field values (when there is only 1 field in the DataSet).
/// </param>
/// <returns>
/// Returns a JSONArray with all records from the DataSet.
/// </returns>
/// <remarks>
/// Invisible or null fields will not be exported.
/// </remarks>
function DataSetToJSONArray(const ADataSet: TDataSet; const IsChild: Boolean; const isValue: Boolean = true ): TJSONArray;
function DataSetToJSONArray(const ADataSet: TDataSet; const IsChild: Boolean; const IsValue: Boolean = True): TJSONArray;
/// <summary>
/// Encrypts a blob field in Base64.
/// </summary>
Expand All @@ -66,7 +75,7 @@ TDataSetSerialize = class
/// <summary>
/// Responsible for creating a new instance of TDataSetSerialize class.
/// </summary>
constructor Create(const ADataSet: TDataSet; const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; const AValueRecords: Boolean = true);
constructor Create(const ADataSet: TDataSet; const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; const AValueRecords: Boolean = True);
/// <summary>
/// Creates an array of JSON objects with all DataSet records.
/// </summary>
Expand Down Expand Up @@ -117,7 +126,7 @@ function TDataSetSerialize.ToJSONObject: TJSONObject;
Result := DataSetToJSONObject(FDataSet);
end;

function TDataSetSerialize.DataSetToJSONArray(const ADataSet: TDataSet; const IsChild: Boolean; const isValue: Boolean = true): TJSONArray;
function TDataSetSerialize.DataSetToJSONArray(const ADataSet: TDataSet; const IsChild: Boolean; const IsValue: Boolean = True): TJSONArray;
var
LBookMark: TBookmark;
begin
Expand All @@ -138,7 +147,7 @@ function TDataSetSerialize.DataSetToJSONArray(const ADataSet: TDataSet; const Is
ADataSet.Next;
Continue;
end;
if (ADataSet.FieldCount = 1) and (isValue) then
if (ADataSet.FieldCount = 1) and (IsValue) then
begin
case ADataSet.Fields[0].DataType of
TFieldType.ftBoolean:
Expand Down Expand Up @@ -175,7 +184,7 @@ function TDataSetSerialize.DataSetToJSONArray(const ADataSet: TDataSet; const Is
end;
end
else
Result.AddElement(DataSetToJSONObject(ADataSet));
Result.AddElement(DataSetToJSONObject(ADataSet, IsValue));
{$ENDIF}
ADataSet.Next;
end;
Expand All @@ -186,7 +195,7 @@ function TDataSetSerialize.DataSetToJSONArray(const ADataSet: TDataSet; const Is
end;
end;

function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const AValue: Boolean = true): TJSONObject;
function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const AValue: Boolean = True): TJSONObject;
var
LKey: string;
{$IF NOT DEFINED(FPC)}
Expand Down Expand Up @@ -247,7 +256,7 @@ function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const A
TFieldType.ftDataSet:
begin
LNestedDataSet := TDataSetField(LField).NestedDataSet;
Result.AddPair(LKey, DataSetToJSONArray(LNestedDataSet, AValue));
Result.AddPair(LKey, DataSetToJSONArray(LNestedDataSet, False, AValue));
end;
{$ENDIF}
TFieldType.ftGraphic, TFieldType.ftBlob, TFieldType.ftOraBlob{$IF NOT DEFINED(FPC)}, TFieldType.ftStream{$ENDIF}:
Expand Down Expand Up @@ -397,7 +406,7 @@ function TDataSetSerialize.SaveStructure: TJSONArray;
end;
end;

constructor TDataSetSerialize.Create(const ADataSet: TDataSet; const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; const AValueRecords: Boolean = true);
constructor TDataSetSerialize.Create(const ADataSet: TDataSet; const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; const AValueRecords: Boolean = True);
begin
FDataSet := ADataSet;
FOnlyUpdatedRecords := AOnlyUpdatedRecords;
Expand Down
7 changes: 5 additions & 2 deletions src/DataSet.Serialize.pas
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ TDataSetSerializeHelper = class Helper for TDataSet
/// <param name="AChildRecords">
/// Exports only childs records from child datasets.
/// </param>
/// <param name="AValueRecords">
/// Inform if it's to export only field values (when there is only 1 field in the DataSet)
/// </param>
/// <returns>
/// Returns a JSONArray with all records from the DataSet.
/// </returns>
/// <remarks>
/// Invisible fields will not be generated.
/// </remarks>
function ToJSONArray(const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; AValueRecords: Boolean = true): TJSONArray;
function ToJSONArray(const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; AValueRecords: Boolean = True): TJSONArray;
/// <summary>
/// Responsible for exporting the structure of a DataSet in JSON Array format.
/// </summary>
Expand Down Expand Up @@ -217,7 +220,7 @@ implementation
{$ENDIF}
DataSet.Serialize.Export, DataSet.Serialize.Import;

function TDataSetSerializeHelper.ToJSONArray(const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; AValueRecords: Boolean = true): TJSONArray;
function TDataSetSerializeHelper.ToJSONArray(const AOnlyUpdatedRecords: Boolean = False; const AChildRecords: Boolean = True; AValueRecords: Boolean = True): TJSONArray;
var
LDataSetSerialize: TDataSetSerialize;
begin
Expand Down

0 comments on commit 0293fee

Please sign in to comment.