Quick.JSONUtils.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.JSONUtils
  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.JSONUtils;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. System.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. type
  38. TObjectHelper = class helper for TObject
  39. public
  40. function ToJson : string;
  41. procedure FromJson(const json : string);
  42. procedure Clone(cObj : TObject);
  43. end;
  44. //only public properties will be cloned or gotten
  45. function IncludeJsonBraces(const json : string) : string;
  46. function RemoveJsonBraces(const json : string) : string;
  47. var
  48. GlobalJsonIdenter : Boolean = True;
  49. implementation
  50. function TObjectHelper.ToJSON : string;
  51. {$IFDEF DELPHIRX102_UP}
  52. var
  53. Serializer : TJsonSerializer;
  54. {$ENDIF}
  55. begin
  56. Result := '';
  57. try
  58. {$IFDEF DELPHIRX102_UP}
  59. Serializer := TJsonSerializer.Create;
  60. try
  61. if GlobalJsonIdenter then Serializer.Formatting := TJsonFormatting.Indented;
  62. Result := Serializer.Serialize<TObject>(Self);
  63. finally
  64. Serializer.Free;
  65. end;
  66. {$ELSE}
  67. Result := TJson.ObjectToJsonString(Self);
  68. {$ENDIF}
  69. except
  70. on e : Exception do raise Exception.Create(e.Message);
  71. end;
  72. end;
  73. procedure TObjectHelper.FromJson(const json :string);
  74. var
  75. jObj : TJSONObject;
  76. begin
  77. try
  78. jObj := TJSonObject.ParseJSONValue(json,true) as TJSONObject;
  79. try
  80. TJson.JsonToObject(self,jObj);
  81. finally
  82. jObj.Free;
  83. end;
  84. except
  85. on e : Exception do raise Exception.Create(e.Message);
  86. end;
  87. end;
  88. procedure TObjectHelper.Clone(cObj : TObject);
  89. begin
  90. cObj.FromJson(Self.ToJson);
  91. end;
  92. function IncludeJsonBraces(const json : string) : string;
  93. begin
  94. if (not json.StartsWith('{')) and (not json.EndsWith('}')) then Result := '{' + json + '}'
  95. else Result := json;
  96. end;
  97. function RemoveJsonBraces(const json : string) : string;
  98. begin
  99. if (json.StartsWith('{')) and (json.EndsWith('}')) then Result := Copy(json,2,Json.Length - 2)
  100. else Result := json;
  101. end;
  102. end.