Quick.JSON.Helper.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.JSON.Helper
  4. Description : Utils for working with JSON
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 27/01/2017
  8. Modified : 09/05/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.JSON.Helper;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. {$IFDEF DELPHIRX102_UP}
  28. JSON.Types,
  29. REST.Json,
  30. System.JSON,
  31. JSON.Serializers,
  32. {$ELSE}
  33. System.JSON,
  34. REST.JSON,
  35. Rest.Json.Types,
  36. {$ENDIF}
  37. Quick.Json.Utils;
  38. type
  39. TObjectHelper = class helper for TObject
  40. public
  41. function ToJson : string;
  42. procedure FromJson(const json : string);
  43. procedure Clone(cObj : TObject);
  44. end;
  45. //only public properties will be cloned or gotten
  46. var
  47. GlobalJsonIdenter : Boolean = True;
  48. implementation
  49. function TObjectHelper.ToJSON : string;
  50. {$IFDEF DELPHIRX102_UP}
  51. var
  52. Serializer : TJsonSerializer;
  53. {$ENDIF}
  54. begin
  55. Result := '';
  56. try
  57. {$IFDEF DELPHIRX102_UP}
  58. Serializer := TJsonSerializer.Create;
  59. try
  60. if GlobalJsonIdenter then Serializer.Formatting := TJsonFormatting.Indented;
  61. Result := Serializer.Serialize<TObject>(Self);
  62. finally
  63. Serializer.Free;
  64. end;
  65. {$ELSE}
  66. Result := TJson.ObjectToJsonString(Self);
  67. {$ENDIF}
  68. except
  69. on e : Exception do raise Exception.Create(e.Message);
  70. end;
  71. end;
  72. procedure TObjectHelper.FromJson(const json :string);
  73. var
  74. jObj : TJSONObject;
  75. begin
  76. try
  77. jObj := TJSonObject.ParseJSONValue(json,true) as TJSONObject;
  78. try
  79. TJson.JsonToObject(self,jObj);
  80. finally
  81. jObj.Free;
  82. end;
  83. except
  84. on e : Exception do raise Exception.Create(e.Message);
  85. end;
  86. end;
  87. procedure TObjectHelper.Clone(cObj : TObject);
  88. begin
  89. cObj.FromJson(Self.ToJson);
  90. end;
  91. end.