Quick.JSONRecord.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.0
  7. Created : 05/05/2018
  8. Modified : 08/07/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. Rest.Json.Types;
  27. type
  28. IJsonable = interface
  29. ['{AF71F59C-89A5-4BFB-8227-0CC3068B7671}']
  30. procedure FromJson(const aJson : string);
  31. function ToJson : string;
  32. end;
  33. TJsonRecord = class(TInterfacedObject,IJsonable)
  34. constructor CreateFromJson(const aJson : string);
  35. procedure FromJson(const aJson : string);
  36. function ToJson : string;
  37. end;
  38. implementation
  39. { TJsonRecord }
  40. constructor TJsonRecord.CreateFromJson(const aJson: string);
  41. var
  42. serializer : TJsonSerializer;
  43. begin
  44. //inherited Create;
  45. {$IFNDEF FPC}
  46. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  47. {$ELSE}
  48. serializer := TJsonSerializer.Create;
  49. {$ENDIF}
  50. try
  51. serializer.JsonToObject(Self,aJson);
  52. finally
  53. serializer.Free;
  54. end;
  55. end;
  56. procedure TJsonRecord.FromJson(const aJson: string);
  57. var
  58. serializer : TJsonSerializer;
  59. begin
  60. {$IFNDEF FPC}
  61. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  62. {$ELSE}
  63. serializer := TJsonSerializer.Create;
  64. {$ENDIF}
  65. try
  66. serializer.JsonToObject(Self,aJson);
  67. finally
  68. serializer.Free;
  69. end;
  70. end;
  71. function TJsonRecord.ToJson: string;
  72. var
  73. serializer : TJsonSerializer;
  74. begin
  75. {$IFNDEF FPC}
  76. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  77. {$ELSE}
  78. serializer := TJsonSerializer.Create;
  79. {$ENDIF}
  80. try
  81. Result := serializer.ObjectToJson(Self);
  82. finally
  83. serializer.Free;
  84. end;
  85. end;
  86. end.