Quick.JSONRecord.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.JSONRecord
  4. Description : Serializable class
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 05/05/2018
  8. Modified : 28/08/2018
  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.JSONRecord;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Quick.Json.Serializer,
  26. Quick.AutoMapper;
  27. type
  28. IJsonable = interface
  29. ['{AF71F59C-89A5-4BFB-8227-0CC3068B7671}']
  30. procedure FromJson(const aJson : string);
  31. function ToJson : string;
  32. procedure MapTo(aTgtObj : TObject);
  33. procedure MapFrom(aSrcObj : TObject);
  34. end;
  35. TJsonRecord = class(TInterfacedObject,IJsonable)
  36. public
  37. constructor CreateFromJson(const aJson : string);
  38. procedure FromJson(const aJson : string);
  39. function ToJson : string;
  40. function Map<T : class, constructor> : T;
  41. procedure MapTo(aTgtObj : TObject);
  42. procedure MapFrom(aSrcObj : TObject);
  43. function Clone : TObject; virtual;
  44. end;
  45. implementation
  46. { TJsonRecord }
  47. constructor TJsonRecord.CreateFromJson(const aJson: string);
  48. var
  49. serializer : TJsonSerializer;
  50. begin
  51. //inherited Create;
  52. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  53. try
  54. serializer.JsonToObject(Self,aJson);
  55. finally
  56. serializer.Free;
  57. end;
  58. end;
  59. procedure TJsonRecord.FromJson(const aJson: string);
  60. var
  61. serializer : TJsonSerializer;
  62. begin
  63. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  64. try
  65. serializer.JsonToObject(Self,aJson);
  66. finally
  67. serializer.Free;
  68. end;
  69. end;
  70. function TJsonRecord.Map<T> : T;
  71. begin
  72. Result := TMapper<T>.Map(Self);
  73. end;
  74. procedure TJsonRecord.MapFrom(aSrcObj: TObject);
  75. begin
  76. TObjMapper.Map(aSrcObj,Self);
  77. end;
  78. procedure TJsonRecord.MapTo(aTgtObj: TObject);
  79. begin
  80. TObjMapper.Map(Self,aTgtObj);
  81. end;
  82. function TJsonRecord.ToJson: string;
  83. var
  84. serializer : TJsonSerializer;
  85. begin
  86. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  87. try
  88. Result := serializer.ObjectToJson(Self);
  89. finally
  90. serializer.Free;
  91. end;
  92. end;
  93. function TJsonRecord.Clone : TObject;
  94. begin
  95. Result := Self.ClassType.Create;
  96. TObjMapper.Map(Self,Result);
  97. end;
  98. end.