Quick.JSONUtils.pas 2.5 KB

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