Skip to content

Commit

Permalink
LoadFromJSON procedure overload
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Jul 25, 2019
1 parent b9b9ea7 commit 0245a3b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion samples/src/DataSet.Serialize.Samples.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object FrmSamples: TFrmSamples
Top = 0
Width = 994
Height = 571
ActivePage = tabJSONNested
ActivePage = tabJSON
Align = alClient
TabOrder = 0
object tabDataSet: TTabSheet
Expand Down
12 changes: 6 additions & 6 deletions samples/src/DataSet.Serialize.Samples.pas
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ procedure TFrmSamples.Append;

procedure TFrmSamples.Button10Click(Sender: TObject);
begin
mtJSONNested.LoadFromJSON(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mmJSONNested.Lines.Text),0) as TJSONArray);
mtJSONNested.LoadFromJSON(mmJSONNested.Lines.Text);
end;

procedure TFrmSamples.Button1Click(Sender: TObject);
Expand Down Expand Up @@ -147,13 +147,13 @@ procedure TFrmSamples.Button5Click(Sender: TObject);
procedure TFrmSamples.Button6Click(Sender: TObject);
begin
if ValidateStructure then
mtJSON.LoadFromJSON(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mmJSONObject.Lines.Text),0) as TJSONObject);
mtJSON.LoadFromJSON(mmJSONObject.Lines.Text);
end;

procedure TFrmSamples.Button7Click(Sender: TObject);
begin
if ValidateStructure then
mtJSON.LoadFromJSON(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mmJSONArray.Lines.Text),0) as TJSONArray);
mtJSON.LoadFromJSON(mmJSONArray.Lines.Text);
end;

procedure TFrmSamples.Button8Click(Sender: TObject);
Expand All @@ -165,10 +165,10 @@ procedure TFrmSamples.Button8Click(Sender: TObject);

procedure TFrmSamples.Button9Click(Sender: TObject);
var
JSON: TJSONObject;
LJSONObject: TJSONObject;
begin
JSON := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mmValidateJSON.Lines.Text),0) as TJSONObject;
mmJSONArrayValidate.Lines.Text := mtJSON.ValidateJSON(JSON, TLanguageType.ptBR).ToString;
LJSONObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mmValidateJSON.Lines.Text),0) as TJSONObject;
mmJSONArrayValidate.Lines.Text := mtJSON.ValidateJSON(LJSONObject, TLanguageType.ptBR).ToString;
end;

procedure TFrmSamples.ClearFields;
Expand Down
4 changes: 2 additions & 2 deletions src/core/DataSet.Serialize.DS.Impl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ TDataSetSerialize = class(TInterfacedObject, IDataSetSerialize)

implementation

uses BooleanField.Types, System.DateUtils, Data.FmtBcd, System.SysUtils, Providers.DataSet.Serialize,
Providers.DataSet.Serialize.Constants, System.Classes, System.NetEncoding, System.TypInfo;
uses BooleanField.Types, System.DateUtils, Data.FmtBcd, System.SysUtils, Providers.DataSet.Serialize, System.TypInfo,
Providers.DataSet.Serialize.Constants, System.Classes, System.NetEncoding;

{ TDataSetSerialize }

Expand Down
3 changes: 1 addition & 2 deletions src/core/DataSet.Serialize.JSON.Impl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ TJSONSerialize = class(TInterfacedObject, IJSONSerialize)

implementation

uses System.Classes, System.SysUtils, System.NetEncoding, System.TypInfo,
System.DateUtils, Providers.DataSet.Serialize.Constants;
uses System.Classes, System.SysUtils, System.NetEncoding, System.TypInfo, System.DateUtils, Providers.DataSet.Serialize.Constants;

{ TJSONSerialize }

Expand Down
21 changes: 20 additions & 1 deletion src/helpers/DataSet.Serialize.Helper.pas
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ TDataSetSerializeHelper = class Helper for TDataSet
/// </remarks>
procedure LoadFromJSON(const JSONArray: TJSONArray); overload;
/// <summary>
/// Loads the DataSet with data from a JSON (string format).
/// </summary>
/// <param name="JSONString">
/// Refers to JSON that you want to load.
/// </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 JSONString: string); overload;
/// <summary>
/// Updates the DataSet data with JSON object data.
/// </summary>
/// <param name="JSONObject">
Expand Down Expand Up @@ -94,7 +105,7 @@ TDataSetSerializeHelper = class Helper for TDataSet

implementation

uses DataSet.Serialize.Impl;
uses DataSet.Serialize.Impl, System.SysUtils;

function TDataSetSerializeHelper.ToJSONArray: TJSONArray;
begin
Expand Down Expand Up @@ -126,6 +137,14 @@ procedure TDataSetSerializeHelper.LoadFromJSON(const JSONObject: TJSONObject);
TSerialize.New.SetJSONObject(JSONObject).ToDataSet(Self);
end;

procedure TDataSetSerializeHelper.LoadFromJSON(const JSONString: string);
begin
if Trim(JSONString).StartsWith('{') then
LoadFromJSON(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSONString),0) as TJSONObject)
else if Trim(JSONString).StartsWith('[') then
LoadFromJSON(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSONString),0) as TJSONArray);
end;

procedure TDataSetSerializeHelper.LoadStructure(const JSONArray: TJSONArray);
begin
TSerialize.New.SetJSONArray(JSONArray).LoadStructure(Self);
Expand Down

0 comments on commit 0245a3b

Please sign in to comment.