Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melhoria no metodo DateTimeToIso8601 da unit jsonbr.utils #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Examples/delphi/JSONBr_Exemplo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ uses
Vcl.Forms,
PUnit in 'PUnit.pas' {Form1},
model.person in 'model.person.pas',
jsonbr.builders in '..\Source\Core\jsonbr.builders.pas',
jsonbr in '..\Source\Core\jsonbr.pas',
jsonbr.writer in '..\Source\Core\jsonbr.writer.pas';
jsonbr in '..\..\Source\jsonbr.pas',
jsonbr.builders in '..\..\Source\Core\jsonbr.builders.pas',
jsonbr.reader in '..\..\Source\Core\jsonbr.reader.pas',
jsonbr.utils in '..\..\Source\Core\jsonbr.utils.pas',
jsonbr.writer in '..\..\Source\Core\jsonbr.writer.pas';

{$R *.res}

Expand Down
557 changes: 440 additions & 117 deletions Examples/delphi/JSONBr_Exemplo.dproj

Large diffs are not rendered by default.

Binary file modified Examples/delphi/JSONBr_Exemplo.res
Binary file not shown.
31 changes: 19 additions & 12 deletions Examples/delphi/PUnit.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,50 @@ object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 299
ClientWidth = 635
ClientHeight = 506
ClientWidth = 705
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 66
Top = 30
Width = 499
Height = 166
Left = 8
Top = 8
Width = 681
Height = 459
Lines.Strings = (
'Memo1')
TabOrder = 0
end
object Button1: TButton
Left = 66
Top = 222
Left = 8
Top = 473
Width = 118
Height = 25
Caption = 'Funcional'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 462
Top = 222
Left = 190
Top = 473
Width = 99
Height = 25
Caption = 'ObjectToJSON'
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 352
Top = 473
Width = 121
Height = 25
Caption = 'JsonToObject'
TabOrder = 3
OnClick = Button3Click
end
end
34 changes: 32 additions & 2 deletions Examples/delphi/PUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
Expand All @@ -36,6 +38,9 @@ TForm1 = class(TForm)
implementation

uses
Rest.Json,
System.JSON,
System.DateUtils,
model.person;

{$R *.dfm}
Expand Down Expand Up @@ -78,7 +83,8 @@ procedure TForm1.Button2Click(Sender: TObject);
Person.LastName := 'Json';
Person.Age := 10;
Person.Salary := 100.10;
Person.Date := Now;
Person.Date := Now.GetDate;
Person.DateTime := Now;

Person.Pessoa.Id := 2;
Person.Pessoa.FirstName := 'Json 2';
Expand Down Expand Up @@ -106,8 +112,32 @@ procedure TForm1.Button2Click(Sender: TObject);

TJSONBr.OnSetValue := nil; // Criando seu proprio tratamento
TJSONBr.OnGetValue := nil; // Criando seu proprio tratamento
Memo1.Lines.Text := TJSONBr.ObjectToJsonString(Person);

var JV: TJSONValue; // not TJSONObject
JV := nil;
try
try
JV := TJSONObject.ParseJSONValue(TJSONBr.ObjectToJsonString(Person));
Memo1.Lines.Text := JV.Format(4);
except on E: Exception do
ShowMessage(E.Message);
end;
finally
FreeAndNil(JV);
end;

finally
Person.Free;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Person: TPerson;
begin
Person := TJsonBr.JsonToObject<TPerson>(Memo1.Lines.Text);
try
Memo1.Lines.Add(Person.FirstName);
finally
Person.Free;
end;
Expand Down
12 changes: 9 additions & 3 deletions Examples/delphi/model.person.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ TPerson = class
FLastName: string;
FAge: Integer;
FSalary: Double;
FDate: TDateTime;
FDate: TDate;
FPessoa: TPersonSub;
FPessoas: TObjectList<TPersonSub>;
FBlob: String;
FDateTime: TDateTime;
public
{ Public declarations }
constructor Create;
Expand Down Expand Up @@ -78,9 +79,14 @@ TPerson = class
property Salary: Double read FSalary write FSalary;

[Restrictions([NotNull])]
[Column('Date', ftDateTime)]
[Column('Date', ftDate)]
[Dictionary('Nivel','Data de anivers�rio','Date','','',taRightJustify)]
property Date: TDateTime read FDate write FDate;
property Date: TDate read FDate write FDate;

[Restrictions([NotNull])]
[Column('DateTime', ftDateTime)]
[Dictionary('Nivel','Data de anivers�rio','Date','','',taRightJustify)]
property DateTime: TDateTime read FDateTime write FDateTime;

[Column('Imagem', ftBlob)]
property Imagem: string read FBlob write FBlob;
Expand Down
12 changes: 9 additions & 3 deletions Source/Core/jsonbr.utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ interface

uses
StrUtils,
SysUtils;
SysUtils,
System.DateUtils;

function DateTimeToIso8601(const AValue: TDateTime;
const AUseISO8601DateFormat: Boolean): string;
Expand Down Expand Up @@ -43,8 +44,13 @@ function Iso8601ToDateTime(const AValue: string;
LYYYY, LMM, LDD, LHH, LMI, LSS: Cardinal;
begin
if AUseISO8601DateFormat then
Result := StrToDateTimeDef(AValue, 0)
else
begin
try
Result := System.DateUtils.ISO8601ToDate(AValue);
except
Result := 0;
end;
end else
Result := StrToDateTimeDef(AValue, 0, JsonBrFormatSettings);

if Length(AValue) = 19 then
Expand Down