Quick.JSONRecord.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. {$IFNDEF FPC}
  45. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  46. {$ELSE}
  47. serializer := TJsonSerializer.Create;
  48. {$ENDIF}
  49. try
  50. serializer.JsonToObject(Self,aJson);
  51. finally
  52. serializer.Free;
  53. end;
  54. end;
  55. procedure TJsonRecord.FromJson(const aJson: string);
  56. var
  57. serializer : TJsonSerializer;
  58. begin
  59. {$IFNDEF FPC}
  60. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  61. {$ELSE}
  62. serializer := TJsonSerializer.Create;
  63. {$ENDIF}
  64. try
  65. serializer.JsonToObject(Self,aJson);
  66. finally
  67. serializer.Free;
  68. end;
  69. end;
  70. function TJsonRecord.ToJson: string;
  71. var
  72. serializer : TJsonSerializer;
  73. begin
  74. {$IFNDEF FPC}
  75. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  76. {$ELSE}
  77. serializer := TJsonSerializer.Create;
  78. {$ENDIF}
  79. try
  80. Result := serializer.ObjectToJson(Self);
  81. finally
  82. serializer.Free;
  83. end;
  84. end;
  85. end.