Advertisement
dualarrow

Custom Serialize

Nov 12th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.55 KB | None | 0 0
  1. unit TestSerialiser;
  2.  
  3. interface
  4.  
  5. uses
  6.   MVCFramework.Serializer.Intf,
  7.   MVCFramework.Serializer.Commons,
  8.   System.Rtti;
  9.  
  10. type
  11.   TTestRecord = class
  12.  
  13.   end;
  14.  
  15.   TTestString = class
  16.   private
  17.     FValue: string;
  18.     FIsNull: boolean;
  19.     procedure SetValue(const Value: string);
  20.   public
  21.     constructor Create;
  22.  
  23.     property Value: string Read FValue write SetValue;
  24.     property IsNull: boolean read FIsNull write FIsNull;
  25.   end;
  26.  
  27.   TTestStringSerializer = class(TInterfacedObject, IMVCTypeSerializer)
  28.   public
  29.     procedure SerializeAttribute(
  30.       const AElementValue: TValue;
  31.       const APropertyName: string;
  32.       const ASerializerObject: TObject;
  33.       const AAttributes: TArray<TCustomAttribute>
  34.       );
  35.  
  36.     procedure SerializeRoot(
  37.       const AObject: TObject;
  38.       out ASerializerObject: TObject;
  39.       const AAttributes: TArray<TCustomAttribute>;
  40.       const ASerializationAction: TMVCSerializationAction = nil
  41.       );
  42.  
  43.     procedure DeserializeAttribute(
  44.       var AElementValue: TValue;
  45.       const APropertyName: string;
  46.       const ASerializerObject: TObject;
  47.       const AAttributes: TArray<TCustomAttribute>
  48.       );
  49.  
  50.     procedure DeserializeRoot(
  51.       const ASerializerObject: TObject;
  52.       const AObject: TObject;
  53.       const AAttributes: TArray<TCustomAttribute>
  54.       );
  55.   end;
  56.  
  57.   TTestRecordSerializer = class(TInterfacedObject, IMVCTypeSerializer)
  58.   public
  59.     procedure SerializeAttribute(
  60.       const AElementValue: TValue;
  61.       const APropertyName: string;
  62.       const ASerializerObject: TObject;
  63.       const AAttributes: TArray<TCustomAttribute>
  64.       );
  65.  
  66.     procedure SerializeRoot(
  67.       const AObject: TObject;
  68.       out ASerializerObject: TObject;
  69.       const AAttributes: TArray<TCustomAttribute>;
  70.       const ASerializationAction: TMVCSerializationAction = nil
  71.       );
  72.  
  73.     procedure DeserializeAttribute(
  74.       var AElementValue: TValue;
  75.       const APropertyName: string;
  76.       const ASerializerObject: TObject;
  77.       const AAttributes: TArray<TCustomAttribute>
  78.       );
  79.  
  80.     procedure DeserializeRoot(
  81.       const ASerializerObject: TObject;
  82.       const AObject: TObject;
  83.       const AAttributes: TArray<TCustomAttribute>
  84.       );
  85.   end;
  86.  
  87.   TComplex1 = class(TTestRecord)
  88.   private
  89.     FFld3: TTestString;
  90.     FFld4: TTestString;
  91.   public
  92.     constructor Create;
  93.     destructor Destroy; override;
  94.  
  95.     property Fld3: TTestString read FFld3 write FFld3;
  96.     property Fld4: TTestString read FFld4 write FFld4;
  97.   end;
  98.  
  99.   TComplex = class(TTestRecord)
  100.   private
  101.     FFld2: TTestString;
  102.     FFld1: TTestString;
  103.     FC1: TComplex1;
  104.   public
  105.     constructor Create;
  106.     destructor Destroy; override;
  107.  
  108.     property C1: TComplex1 read FC1 write FC1;
  109.     property Fld1: TTestString read FFld1 write FFld1;
  110.     property Fld2: TTestString read FFld2 write FFld2;
  111.   end;
  112.  
  113. implementation
  114.  
  115. uses
  116.   JsonDataObjects;
  117.  
  118. { TTestStringSerializer }
  119.  
  120. procedure TTestStringSerializer.DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  121. var
  122.   lJson: TJsonObject;
  123. begin
  124.   lJson := aSerializerObject as TJsonObject;
  125.   var lValue := lJson.S[aPropertyName];
  126.   var lTestString := aElementValue.AsObject as TTestString;
  127.   lTestString.Value := lValue;
  128. end;
  129.  
  130. procedure TTestStringSerializer.DeserializeRoot(const ASerializerObject, AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  131. begin
  132.  
  133. end;
  134.  
  135. procedure TTestStringSerializer.SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  136. begin
  137.   var aTest := aElementValue.AsObject as TTestString;
  138.  
  139.   if aTest.IsNull then
  140.   begin
  141.     // assign nil to get a value of null, or dont assign it at all and it wont be output
  142.     (aSerializerObject as TJDOJSONObject).O['Zap'] := nil;
  143.   end
  144.   else
  145.   begin
  146.     //aSerializerObject := TJDOJSONObject.Create;
  147.     var lms := aElementValue.AsObject as TTestString;
  148.     (aSerializerObject as TJDOJSONObject).S[aPropertyName] := lms.Value;
  149.  
  150.   end;
  151. end;
  152.  
  153. procedure TTestStringSerializer.SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction);
  154. begin
  155.   var aTest := aObject as TTestString;
  156.  
  157.   if aTest.IsNull then
  158.   begin
  159.  
  160.   end
  161.   else
  162.   begin
  163.     aSerializerObject := TJDOJSONObject.Create;
  164.     (aSerializerObject as TJDOJSONObject).S['Zap'] := 'Geeee';
  165.   end;
  166. end;
  167.  
  168. { TTestString }
  169.  
  170. constructor TTestString.Create;
  171. begin
  172.   FIsNull := true;
  173. end;
  174.  
  175. procedure TTestString.SetValue(const Value: string);
  176. begin
  177.   FIsNull := false;
  178.   FValue := Value;
  179. end;
  180.  
  181. { TComplex }
  182.  
  183. constructor TComplex.Create;
  184. begin
  185.     FFld2 := TTestString.Create;
  186.     FFld1 := TTestString.Create;
  187.     FC1 := TComplex1.Create;
  188. end;
  189.  
  190. destructor TComplex.Destroy;
  191. begin
  192.   FFld1.Free;
  193.   FFld2.Free;
  194.   FC1.Free;
  195.   inherited;
  196. end;
  197.  
  198. { TTestRecordSerializer }
  199.  
  200. procedure TTestRecordSerializer.DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  201. begin
  202.  
  203. end;
  204.  
  205. procedure TTestRecordSerializer.DeserializeRoot(const ASerializerObject, AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  206. begin
  207.  
  208. end;
  209.  
  210. procedure TTestRecordSerializer.SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
  211. begin
  212.   var aTest := aElementValue.AsObject as TComplex;
  213.  
  214.   //aSerializerObject := TJDOJSONObject.Create;
  215.   var lTestString := aElementValue.AsObject as TTestString;
  216.   (aSerializerObject as TJDOJSONObject).S[aPropertyName] := lTestString.Value;
  217.  
  218. end;
  219.  
  220. procedure TTestRecordSerializer.SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction);
  221. begin
  222.   aSerializerObject := TJDOJSONObject.Create;
  223.  
  224.   var c := aObject as TComplex;
  225.  
  226.   SerializeAttribute(AObject, 'C1', ASerializerObject, AAttributes);
  227.  
  228.   (aSerializerObject as TJDOJSONObject).O['C1'];
  229.   (aSerializerObject as TJDOJSONObject).O['Fld1'];
  230.   (aSerializerObject as TJDOJSONObject).O['Fld2'];
  231. end;
  232.  
  233. { TComplex1 }
  234.  
  235. constructor TComplex1.Create;
  236. begin
  237.     FFld4 := TTestString.Create;
  238.     FFld3 := TTestString.Create;
  239. end;
  240.  
  241. destructor TComplex1.Destroy;
  242. begin
  243.   FFld3.Free;
  244.   FFld4.Free;
  245.   inherited;
  246. end;
  247.  
  248. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement