Advertisement
dualarrow

Neon example

Feb 10th, 2021
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.01 KB | None | 0 0
  1. unit Unit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
  8.  
  9. type
  10.   TForm2 = class(TForm)
  11.     procedure FormCreate(Sender: TObject);
  12.   private
  13.     { Private declarations }
  14.   public
  15.     { Public declarations }
  16.   end;
  17.  
  18. var
  19.   Form2: TForm2;
  20.  
  21. implementation
  22.  
  23. uses
  24.   Json,
  25.   Rtti,
  26.   TypInfo,
  27.   DateUtils,
  28.   TimeSpan,
  29.   Neon.Core.Nullables,
  30.   Neon.Core.Utils,
  31.   Neon.Core.Persistence.JSON,
  32.   Neon.Core.Persistence,
  33.   Generics.Collections;
  34.  
  35. {$R *.dfm}
  36.  
  37. var
  38.   gTimeZone: string;
  39.  
  40. type
  41.   TJunk = class
  42.   private
  43.     FAnInt: integer;
  44.   public
  45.     property AnInt: integer read FAnInt write FAnInt;
  46.   end;
  47.  
  48.   TTest = class
  49.   private
  50.     FSomeDate: TDate;
  51.     FSomeTime: TTime;
  52.     FSomeDateTime: TDateTime;
  53.     FJunk: TJunk;
  54.   public
  55.     constructor create;
  56.  
  57.     property someDate: TDate read FSomeDate write FSomeDate;
  58.     property someTime: TTime read FSomeTime write FSomeTime;
  59.     property someDateTime: TDateTime read FSomeDateTime write FSomeDateTime;
  60.     property Junk: TJunk read FJunk write FJunk;
  61.   end;
  62.  
  63.   TTimeSerializer = class(TCustomSerializer)
  64.   protected
  65.     class function GetTargetInfo: PTypeInfo; override;
  66.     class function CanHandle(AType: PTypeInfo): Boolean; override;
  67.   public
  68.     function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
  69.     function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
  70.   end;
  71.  
  72.   TDateSerializer = class(TCustomSerializer)
  73.   protected
  74.     class function GetTargetInfo: PTypeInfo; override;
  75.     class function CanHandle(AType: PTypeInfo): Boolean; override;
  76.   public
  77.     function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
  78.     function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
  79.   end;
  80.  
  81.   TDateTimeSerializer = class(TCustomSerializer)
  82.   protected
  83.     class function GetTargetInfo: PTypeInfo; override;
  84.     class function CanHandle(AType: PTypeInfo): Boolean; override;
  85.   public
  86.     function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
  87.     function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
  88.   end;
  89.  
  90. { TTimeSerializer }
  91.  
  92. class function TTimeSerializer.GetTargetInfo: PTypeInfo;
  93. begin
  94.   Result := TypeInfo(TTime);
  95. end;
  96.  
  97. function TTimeSerializer.Serialize(const AValue: TValue; ANeonObject:
  98.     TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
  99. var
  100.   LTime: TTime;
  101.   LHours, LMinutes, LSeconds, LMilli: Word;
  102. begin
  103.   LTime := AValue.AsType<TTime>;
  104.   DecodeTime(LTime, LHours, LMinutes, LSeconds, LMilli);
  105.   Result := TJSONString.Create(Format('%.2d:%.2d:%.2d%s', [LHours, LMinutes, LSeconds, gTimeZone]));
  106. end;
  107.  
  108. class function TTimeSerializer.CanHandle(AType: PTypeInfo): Boolean;
  109. begin
  110.   if AType = GetTargetInfo then
  111.     Result := True
  112.   else
  113.     Result := False;
  114. end;
  115.  
  116. function TTimeSerializer.Deserialize(AValue: TJSONValue; const AData: TValue;
  117.     ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
  118. var
  119.   LTime: TTime;
  120.   s: string;
  121. begin
  122.   s := AValue.Value;
  123.   s := copy(s, 1, 8);
  124.   LTime := StrToTime(s);
  125.   Result := TValue.From<TTime>(LTime);
  126. end;
  127.  
  128. { TDateSerializer }
  129.  
  130. class function TDateSerializer.GetTargetInfo: PTypeInfo;
  131. begin
  132.   Result := TypeInfo(TDate);
  133. end;
  134.  
  135. function TDateSerializer.Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
  136. var
  137.   LDate: TDate;
  138.   lDay, lMonth, lYear: Word;
  139. begin
  140.   LDate := AValue.AsType<TDate>;
  141.   DecodeDate(LDate, lYear, lMonth, lDay);
  142.   Result := TJSONString.Create(Format('%4.4d-%2.2d-%2.2d', [lYear, lMonth, lDay]));
  143. end;
  144.  
  145. class function TDateSerializer.CanHandle(AType: PTypeInfo): Boolean;
  146. begin
  147.   if AType = GetTargetInfo then
  148.     Result := True
  149.   else
  150.     Result := False;
  151. end;
  152.  
  153. function TDateSerializer.Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
  154. var
  155.   LDate: TDate;
  156.   s: string;
  157. begin
  158.   s := AValue.Value;
  159.   s := copy(s, 9, 2) + '/' + copy(s, 6, 2) + '/' + copy(s,1,4);
  160.   LDate := StrToDate(s);
  161.   Result := TValue.From<TDate>(LDate);
  162. end;
  163.  
  164. { TDateTimeSerializer }
  165.  
  166. class function TDateTimeSerializer.GetTargetInfo: PTypeInfo;
  167. begin
  168.   Result := TypeInfo(TDateTime);
  169. end;
  170.  
  171. function TDateTimeSerializer.Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
  172. var
  173.   LDateTime: TDateTime;
  174.   lDay, lMonth, lYear, lHour, lMinute, lSecond, lDummy: Word;
  175. begin
  176.   LDateTime := AValue.AsType<TDateTime>;
  177.   DecodeDate(LDateTime, lYear, lMonth, lDay);
  178.   DecodeTime(lDateTime, lHour, lMinute, lSecond, lDummy);
  179.   Result := TJSONString.Create(Format('%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d%s', [lYear, lMonth, lDay, lHour, lMinute, lSecond, gTimezone]));
  180. end;
  181.  
  182. class function TDateTimeSerializer.CanHandle(AType: PTypeInfo): Boolean;
  183. begin
  184.   if AType = GetTargetInfo then
  185.     Result := True
  186.   else
  187.     Result := False;
  188. end;
  189.  
  190. function TDateTimeSerializer.Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
  191. var
  192.   LDateTime: TDateTime;
  193.   s: string;
  194. begin
  195.   s := aValue.Value;
  196.   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);
  197.   LDateTime := StrToDateTime(s);
  198.   Result := TValue.From<TDateTime>(LDateTime);
  199. end;
  200.  
  201.  
  202. procedure Test;
  203. var
  204.   lJson: TJSONValue;
  205.   lWriter: TNeonSerializerJson;
  206.   lReader: TNeonDeserializerJson;
  207.   lConfig: INeonConfiguration;
  208.   o, oo: TTest;
  209.   s: string;
  210. begin
  211.   o := TTest.Create;
  212.   o.someDateTime := now;
  213.   o.someDate := o.someDateTime;
  214.   o.someTime := o.someDateTime;
  215.   o.Junk := nil;
  216.  
  217.   lConfig := TNeonConfiguration.Default;
  218.   lConfig.GetSerializers.RegisterSerializer(TTimeSerializer).RegisterSerializer(TDateSerializer).RegisterSerializer(TDateTimeSerializer);
  219.  
  220.   lWriter := TNeonSerializerJson.Create(lConfig);
  221.   lJson := lWriter.ObjectToJSON(o);
  222.   s := TNeon.Print(lJson, true);
  223.   showmessage(s);
  224.   o.Free;
  225.  
  226.   lJson := TJsonObject.ParseJSONValue(s);
  227.   lReader := TNeonDeserializerJson.Create(lConfig);
  228.   o := TTest.Create;
  229.   lReader.JSONToObject(o, lJson);
  230.   if o = nil then;
  231. end;
  232.  
  233. procedure TForm2.FormCreate(Sender: TObject);
  234. var
  235.   lTZ: TTimeZone;
  236.   tz: TTimeSpan;
  237. begin
  238.   lTZ := TTimeZone.Create;
  239.   tz := ltz.Local.GetUtcOffset(now);
  240.   if tz.hours = 0 then;
  241.   if tz.minutes = 0 then;
  242.   gTimeZone := Format('+%d:%2.2d', [tz.hours, tz.minutes]);
  243.   lTZ.Free;
  244.  
  245.   test;
  246.   halt;
  247. end;
  248.  
  249. { TTest }
  250.  
  251. constructor TTest.create;
  252. begin
  253.   FJunk := TJunk.Create;
  254. end;
  255.  
  256. end.
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement