Quick.MemoryCache.Serializer.Json.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.MemoryCache.Serializer.Json
  4. Description : Cache Json serializer
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 14/07/2019
  8. Modified : 02/11/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  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. unit Quick.MemoryCache.Serializer.Json;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. RTTI,
  26. //System.JSON.Serializers,
  27. Quick.Json.Serializer,
  28. Quick.MemoryCache.Types;
  29. type
  30. TCacheJsonSerializer = class(TInterfacedObject,ICacheSerializer)
  31. private
  32. fJsonSerializer : TJsonSerializer;
  33. public
  34. constructor Create;
  35. destructor Destroy; override;
  36. function Serialize(aObject : TObject) : string; overload;
  37. function Serialize(aArray : TArray<string>) : string; overload;
  38. function Serialize(aArray: TArray<TObject>): string; overload;
  39. procedure Deserialize(const aValue : string; aObject : TObject); overload;
  40. procedure Deserialize(const aValue : string; var aArray : TArray<string>); overload;
  41. procedure Deserialize(const aValue : string; var aArray: TArray<TObject>); overload;
  42. end;
  43. implementation
  44. { TCacheJsonSerializer }
  45. constructor TCacheJsonSerializer.Create;
  46. begin
  47. fJsonSerializer := TJsonSerializer.Create(TSerializeLevel.slPublicProperty,False);
  48. //fJsonSerializer := TJsonSerializer.Create;
  49. end;
  50. destructor TCacheJsonSerializer.Destroy;
  51. begin
  52. fJsonSerializer.Free;
  53. inherited;
  54. end;
  55. function TCacheJsonSerializer.Serialize(aObject: TObject): string;
  56. begin
  57. Result := fJsonSerializer.ObjectToJson(aObject,False);
  58. //Result := fJsonSerializer.Serialize<TObject>(aObject);
  59. end;
  60. function TCacheJsonSerializer.Serialize(aArray: TArray<string>): string;
  61. begin
  62. Result := fJsonSerializer.ArrayToJson<string>(aArray);
  63. end;
  64. function TCacheJsonSerializer.Serialize(aArray: TArray<TObject>): string;
  65. begin
  66. Result := fJsonSerializer.ArrayToJson<TObject>(aArray);
  67. end;
  68. procedure TCacheJsonSerializer.Deserialize(const aValue: string; aObject: TObject);
  69. begin
  70. fJsonSerializer.JsonToObject(aObject,aValue);
  71. //aObject := fJsonSerializer.Deserialize<TObject>(aValue);
  72. end;
  73. procedure TCacheJsonSerializer.Deserialize(const aValue: string; var aArray: TArray<string>);
  74. begin
  75. aArray := fJsonSerializer.JsonToArray<string>(aValue);
  76. end;
  77. procedure TCacheJsonSerializer.Deserialize(const aValue: string; var aArray: TArray<TObject>);
  78. begin
  79. aArray := fJsonSerializer.JsonToArray<TObject>(aValue);
  80. end;
  81. end.