dualarrow

Example Serializer

Apr 25th, 2022 (edited)
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.17 KB | None | 0 0
  1. unit Unit6;
  2.  
  3. interface
  4.  
  5. uses
  6.   MVCFramework.Serializer.Commons, Rtti,
  7.   MVCFramework.Serializer.Intf,
  8.   MVCFramework, MVCFramework.RestClient, MVCFramework.RESTClient.Intf, MVCFramework.RestAdapter, MVCFramework.Commons, MVCFramework.Nullables, MVCFramework.Serializer.JsonDataObjects.NullableTypes,
  9.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  10.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
  11.  
  12. type
  13.   TForm6 = class(TForm)
  14.     procedure FormCreate(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   Form6: TForm6;
  23.  
  24. type
  25.   TSkipableString = class
  26.   end;
  27.  
  28.   TCouchDb = class
  29.   private
  30.     f_Id: string;
  31.     f_Rev: string;
  32.   public
  33.     property _id: string read f_Id write f_Id;
  34.     property _rev: string read f_Rev write f_Rev;
  35.   end;
  36.  
  37.   TTest = class(TCouchDb)
  38.   private
  39.     fName: TSkipableString;
  40.     fAge: NullableInt32;
  41.   public
  42.     constructor create;
  43.     property name: TSkipableString read fName write fName;
  44.     property age: NullableInt32 read fAge write fAge;
  45.   end;
  46.  
  47.   ICouchDb = interface(IInvokable)
  48.     ['{4605A9AA-04C8-4309-9A69-B7890920DAA4}']
  49.  
  50.     [RestResource(HttpGet, '/{database}/{id}')]
  51. //    [MVCProduces('application/json')]
  52.     [MVCConsumes('application/json')]
  53. //    [Mapping(TTest)]
  54.     function Get([Param('database')]database: string; [Param('id')]id: string): TTest;
  55.  
  56.     [RestResource(HttpPut, '/{database}/{id}')]
  57.     [MVCProduces('application/json')]
  58.     [MVCConsumes('application/json')]
  59. //    [Mapping(TTest)]
  60.     function Update([Param('database')]database: string; [Param('id')]id: string; [Body]body: TTest): TTest;
  61.   end;
  62.  
  63.   TSkipableStringSerializer = class(TInterfacedObject, IMVCTypeSerializer)
  64.   public
  65.     procedure SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  66.     procedure SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction = nil);
  67.     procedure DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  68.     procedure DeserializeRoot(const ASerializerObject: TObject; const AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  69.   end;
  70.  
  71. implementation
  72.  
  73. {$R *.dfm}
  74.  
  75. var
  76.   gRestClient: IMVCRESTClient;
  77.   gCouchDb: ICouchDb;
  78.  
  79. procedure TForm6.FormCreate(Sender: TObject);
  80. begin
  81.   var lRestAdapter := TRestAdapter<ICouchDb>.Create;
  82.  
  83.   gRestClient := TMVCRESTClient.Create.BaseURL('http://192.168.1.4', 5984);
  84.  
  85.  
  86.   var ss := TSkipableStringSerializer.Create;
  87.   gRestClient.Serializer.RegisterTypeSerializer(TypeInfo(TSkipableString), ss);
  88. //  gRestClient.RegisterTypeSerializer(TypeInfo(TSkipableString), ss);
  89.  
  90. //  gRestClient.Serializer
  91.  
  92.   gRestClient.SetBasicAuthorization('admin','b19246');
  93.  
  94.   gCouchDb := lRestAdapter.Build(gRestClient);
  95.  
  96.   var ans := gCouchDb.Get('mytest', 'id');
  97.  
  98.   ans.age.value := ans.age.value + 1;
  99.  
  100. //  ans.name := '';
  101.  
  102.   var repl := gCouchDb.Update('mytest', 'id', ans);
  103.  
  104.   if ans._id = repl._id then
  105.     now;
  106.  
  107. end;
  108.  
  109. { TSkipableStringSerializer }
  110.  
  111. procedure TSkipableStringSerializer.DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  112. begin
  113.  
  114. end;
  115.  
  116. procedure TSkipableStringSerializer.DeserializeRoot(const ASerializerObject, AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  117. begin
  118.  
  119. end;
  120.  
  121. procedure TSkipableStringSerializer.SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  122. begin
  123.  
  124. end;
  125.  
  126. procedure TSkipableStringSerializer.SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction);
  127. begin
  128.  
  129. end;
  130.  
  131. { TTest }
  132.  
  133. constructor TTest.create;
  134. begin
  135.   fName := TSkipableString.Create;
  136.  
  137. end;
  138.  
  139. end.
Add Comment
Please, Sign In to add comment