Neon.Core.Serializers.VCL.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {******************************************************************************}
  2. { }
  3. { Neon: Serialization Library for Delphi }
  4. { Copyright (c) 2018-2019 Paolo Rossi }
  5. { https://github.com/paolo-rossi/neon-library }
  6. { }
  7. {******************************************************************************}
  8. { }
  9. { Licensed under the Apache License, Version 2.0 (the "License"); }
  10. { you may not use this file except in compliance with the License. }
  11. { You may obtain a copy of the License at }
  12. { }
  13. { http://www.apache.org/licenses/LICENSE-2.0 }
  14. { }
  15. { Unless required by applicable law or agreed to in writing, software }
  16. { distributed under the License is distributed on an "AS IS" BASIS, }
  17. { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
  18. { See the License for the specific language governing permissions and }
  19. { limitations under the License. }
  20. { }
  21. {******************************************************************************}
  22. unit Neon.Core.Serializers.VCL;
  23. interface
  24. uses
  25. System.SysUtils, System.Classes, System.Rtti, System.TypInfo,
  26. System.Generics.Collections, System.JSON,
  27. Neon.Core.Types,
  28. Neon.Core.Attributes,
  29. Neon.Core.Persistence;
  30. type
  31. TImageSerializer = class(TCustomSerializer)
  32. protected
  33. class function GetTargetInfo: PTypeInfo; override;
  34. class function CanHandle(AType: PTypeInfo): Boolean; override;
  35. public
  36. function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
  37. function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
  38. end;
  39. implementation
  40. uses
  41. Vcl.ExtCtrls,
  42. Neon.Core.Utils;
  43. { TImageSerializer }
  44. class function TImageSerializer.CanHandle(AType: PTypeInfo): Boolean;
  45. begin
  46. Result := TypeInfoIs(AType);
  47. end;
  48. class function TImageSerializer.GetTargetInfo: PTypeInfo;
  49. begin
  50. Result := TImage.ClassInfo;
  51. end;
  52. function TImageSerializer.Serialize(const AValue: TValue;
  53. ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
  54. var
  55. LImage: TImage;
  56. LStream: TMemoryStream;
  57. LBase64: string;
  58. begin
  59. LImage := AValue.AsObject as TImage;
  60. if LImage.Picture = nil then
  61. begin
  62. case ANeonObject.NeonInclude.Value of
  63. IncludeIf.NotEmpty, IncludeIf.NotDefault: Exit(nil);
  64. else
  65. Exit(TJSONString.Create(''));
  66. end;
  67. end;
  68. LStream := TMemoryStream.Create;
  69. try
  70. LImage.Picture.SaveToStream(LStream);
  71. LStream.Position := soFromBeginning;
  72. LBase64 := TBase64.Encode(LStream);
  73. finally
  74. LStream.Free;
  75. end;
  76. Result := TJSONString.Create(LBase64);
  77. end;
  78. function TImageSerializer.Deserialize(AValue: TJSONValue; const AData: TValue;
  79. ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
  80. var
  81. LImage: TImage;
  82. LStream: TMemoryStream;
  83. begin
  84. Result := AData;
  85. LImage := AData.AsObject as TImage;
  86. if AValue.Value.IsEmpty then
  87. begin
  88. LImage.Picture := nil;
  89. Exit(AData);
  90. end;
  91. LStream := TMemoryStream.Create;
  92. try
  93. TBase64.Decode(AValue.Value, LStream);
  94. LStream.Position := soFromBeginning;
  95. LImage.Picture.LoadFromStream(LStream);
  96. finally
  97. LStream.Free;
  98. end;
  99. end;
  100. end.