Quick.JSONRecord.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.2
  7. Created : 05/05/2018
  8. Modified : 06/11/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. Classes,
  26. Quick.Json.Serializer,
  27. Quick.AutoMapper;
  28. type
  29. IJsonable = interface
  30. ['{AF71F59C-89A5-4BFB-8227-0CC3068B7671}']
  31. procedure FromJson(const aJson : string);
  32. procedure LoadFromFile(const aJsonFilename : string);
  33. function ToJson(aIdent : Boolean = False) : string;
  34. procedure SaveToFile(const aJsonFilename : string; aIndent : Boolean = True);
  35. procedure MapTo(aTgtObj : TObject);
  36. procedure MapFrom(aSrcObj : TObject);
  37. end;
  38. TJsonRecord = class(TInterfacedObject,IJsonable)
  39. public
  40. constructor CreateFromJson(const aJson : string);
  41. constructor CreateFromFile(const aJsonFilename : string);
  42. procedure LoadFromFile(const aJsonFilename : string);
  43. procedure FromJson(const aJson : string);
  44. function ToJson(aIndent : Boolean = False) : string;
  45. procedure SaveToFile(const aJsonFilename : string; aIndent : Boolean = True);
  46. function Map<T : class, constructor> : T;
  47. procedure MapTo(aTgtObj : TObject);
  48. procedure MapFrom(aSrcObj : TObject);
  49. function Clone : TObject; virtual;
  50. end;
  51. implementation
  52. { TJsonRecord }
  53. constructor TJsonRecord.CreateFromJson(const aJson: string);
  54. var
  55. serializer : TJsonSerializer;
  56. begin
  57. //inherited Create;
  58. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  59. try
  60. serializer.JsonToObject(Self,aJson);
  61. finally
  62. serializer.Free;
  63. end;
  64. end;
  65. procedure TJsonRecord.FromJson(const aJson: string);
  66. var
  67. serializer : TJsonSerializer;
  68. begin
  69. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  70. try
  71. serializer.JsonToObject(Self,aJson);
  72. finally
  73. serializer.Free;
  74. end;
  75. end;
  76. procedure TJsonRecord.LoadFromFile(const aJsonFilename: string);
  77. var
  78. json : TStringList;
  79. begin
  80. json := TStringList.Create;
  81. try
  82. json.LoadFromFile(aJsonFilename);
  83. Self.FromJson(json.Text);
  84. finally
  85. json.Free;
  86. end;
  87. end;
  88. constructor TJsonRecord.CreateFromFile(const aJsonFilename : string);
  89. var
  90. json : TStringList;
  91. begin
  92. json := TStringList.Create;
  93. try
  94. json.LoadFromFile(aJsonFilename);
  95. CreateFromJson(json.Text);
  96. finally
  97. json.Free;
  98. end;
  99. end;
  100. procedure TJsonRecord.SaveToFile(const aJsonFilename : string; aIndent : Boolean = True);
  101. var
  102. json : TStringList;
  103. begin
  104. json := TStringList.Create;
  105. try
  106. json.Text := Self.ToJson(aIndent);
  107. json.SaveToFile(aJsonFilename);
  108. finally
  109. json.Free;
  110. end;
  111. end;
  112. function TJsonRecord.Map<T> : T;
  113. begin
  114. Result := TMapper<T>.Map(Self);
  115. end;
  116. procedure TJsonRecord.MapFrom(aSrcObj: TObject);
  117. begin
  118. TObjMapper.Map(aSrcObj,Self);
  119. end;
  120. procedure TJsonRecord.MapTo(aTgtObj: TObject);
  121. begin
  122. TObjMapper.Map(Self,aTgtObj);
  123. end;
  124. function TJsonRecord.ToJson(aIndent : Boolean = False) : string;
  125. var
  126. serializer : TJsonSerializer;
  127. begin
  128. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  129. try
  130. Result := serializer.ObjectToJson(Self,aIndent);
  131. finally
  132. serializer.Free;
  133. end;
  134. end;
  135. function TJsonRecord.Clone : TObject;
  136. begin
  137. Result := Self.ClassType.Create;
  138. TObjMapper.Map(Self,Result);
  139. end;
  140. end.