Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
- type
- TForm2 = class(TForm)
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form2: TForm2;
- implementation
- uses
- Json,
- Rtti,
- TypInfo,
- DateUtils,
- TimeSpan,
- Neon.Core.Nullables,
- Neon.Core.Utils,
- Neon.Core.Persistence.JSON,
- Neon.Core.Persistence,
- Generics.Collections;
- {$R *.dfm}
- var
- gTimeZone: string;
- type
- TJunk = class
- private
- FAnInt: integer;
- public
- property AnInt: integer read FAnInt write FAnInt;
- end;
- TTest = class
- private
- FSomeDate: TDate;
- FSomeTime: TTime;
- FSomeDateTime: TDateTime;
- FJunk: TJunk;
- public
- constructor create;
- property someDate: TDate read FSomeDate write FSomeDate;
- property someTime: TTime read FSomeTime write FSomeTime;
- property someDateTime: TDateTime read FSomeDateTime write FSomeDateTime;
- property Junk: TJunk read FJunk write FJunk;
- end;
- TTimeSerializer = class(TCustomSerializer)
- protected
- class function GetTargetInfo: PTypeInfo; override;
- class function CanHandle(AType: PTypeInfo): Boolean; override;
- public
- function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
- function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
- end;
- TDateSerializer = class(TCustomSerializer)
- protected
- class function GetTargetInfo: PTypeInfo; override;
- class function CanHandle(AType: PTypeInfo): Boolean; override;
- public
- function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
- function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
- end;
- TDateTimeSerializer = class(TCustomSerializer)
- protected
- class function GetTargetInfo: PTypeInfo; override;
- class function CanHandle(AType: PTypeInfo): Boolean; override;
- public
- function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
- function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
- end;
- { TTimeSerializer }
- class function TTimeSerializer.GetTargetInfo: PTypeInfo;
- begin
- Result := TypeInfo(TTime);
- end;
- function TTimeSerializer.Serialize(const AValue: TValue; ANeonObject:
- TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
- var
- LTime: TTime;
- LHours, LMinutes, LSeconds, LMilli: Word;
- begin
- LTime := AValue.AsType<TTime>;
- DecodeTime(LTime, LHours, LMinutes, LSeconds, LMilli);
- Result := TJSONString.Create(Format('%.2d:%.2d:%.2d%s', [LHours, LMinutes, LSeconds, gTimeZone]));
- end;
- class function TTimeSerializer.CanHandle(AType: PTypeInfo): Boolean;
- begin
- if AType = GetTargetInfo then
- Result := True
- else
- Result := False;
- end;
- function TTimeSerializer.Deserialize(AValue: TJSONValue; const AData: TValue;
- ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
- var
- LTime: TTime;
- s: string;
- begin
- s := AValue.Value;
- s := copy(s, 1, 8);
- LTime := StrToTime(s);
- Result := TValue.From<TTime>(LTime);
- end;
- { TDateSerializer }
- class function TDateSerializer.GetTargetInfo: PTypeInfo;
- begin
- Result := TypeInfo(TDate);
- end;
- function TDateSerializer.Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
- var
- LDate: TDate;
- lDay, lMonth, lYear: Word;
- begin
- LDate := AValue.AsType<TDate>;
- DecodeDate(LDate, lYear, lMonth, lDay);
- Result := TJSONString.Create(Format('%4.4d-%2.2d-%2.2d', [lYear, lMonth, lDay]));
- end;
- class function TDateSerializer.CanHandle(AType: PTypeInfo): Boolean;
- begin
- if AType = GetTargetInfo then
- Result := True
- else
- Result := False;
- end;
- function TDateSerializer.Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
- var
- LDate: TDate;
- s: string;
- begin
- s := AValue.Value;
- s := copy(s, 9, 2) + '/' + copy(s, 6, 2) + '/' + copy(s,1,4);
- LDate := StrToDate(s);
- Result := TValue.From<TDate>(LDate);
- end;
- { TDateTimeSerializer }
- class function TDateTimeSerializer.GetTargetInfo: PTypeInfo;
- begin
- Result := TypeInfo(TDateTime);
- end;
- function TDateTimeSerializer.Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
- var
- LDateTime: TDateTime;
- lDay, lMonth, lYear, lHour, lMinute, lSecond, lDummy: Word;
- begin
- LDateTime := AValue.AsType<TDateTime>;
- DecodeDate(LDateTime, lYear, lMonth, lDay);
- DecodeTime(lDateTime, lHour, lMinute, lSecond, lDummy);
- Result := TJSONString.Create(Format('%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d%s', [lYear, lMonth, lDay, lHour, lMinute, lSecond, gTimezone]));
- end;
- class function TDateTimeSerializer.CanHandle(AType: PTypeInfo): Boolean;
- begin
- if AType = GetTargetInfo then
- Result := True
- else
- Result := False;
- end;
- function TDateTimeSerializer.Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
- var
- LDateTime: TDateTime;
- s: string;
- begin
- s := aValue.Value;
- s := copy(s, 9, 2) + '/' + copy(s, 6, 2) + '/' + copy(s,1,4) + ' ' + copy(s, 12, 2) + ':' + copy(s, 15, 2) + ':' + copy(s, 18, 2);
- LDateTime := StrToDateTime(s);
- Result := TValue.From<TDateTime>(LDateTime);
- end;
- procedure Test;
- var
- lJson: TJSONValue;
- lWriter: TNeonSerializerJson;
- lReader: TNeonDeserializerJson;
- lConfig: INeonConfiguration;
- o, oo: TTest;
- s: string;
- begin
- o := TTest.Create;
- o.someDateTime := now;
- o.someDate := o.someDateTime;
- o.someTime := o.someDateTime;
- o.Junk := nil;
- lConfig := TNeonConfiguration.Default;
- lConfig.GetSerializers.RegisterSerializer(TTimeSerializer).RegisterSerializer(TDateSerializer).RegisterSerializer(TDateTimeSerializer);
- lWriter := TNeonSerializerJson.Create(lConfig);
- lJson := lWriter.ObjectToJSON(o);
- s := TNeon.Print(lJson, true);
- showmessage(s);
- o.Free;
- lJson := TJsonObject.ParseJSONValue(s);
- lReader := TNeonDeserializerJson.Create(lConfig);
- o := TTest.Create;
- lReader.JSONToObject(o, lJson);
- if o = nil then;
- end;
- procedure TForm2.FormCreate(Sender: TObject);
- var
- lTZ: TTimeZone;
- tz: TTimeSpan;
- begin
- lTZ := TTimeZone.Create;
- tz := ltz.Local.GetUtcOffset(now);
- if tz.hours = 0 then;
- if tz.minutes = 0 then;
- gTimeZone := Format('+%d:%2.2d', [tz.hours, tz.minutes]);
- lTZ.Free;
- test;
- halt;
- end;
- { TTest }
- constructor TTest.create;
- begin
- FJunk := TJunk.Create;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement