Quick.JSONUtils.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 : 13/02/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. interface
  23. uses
  24. Classes,
  25. System.SysUtils,
  26. {$IFDEF DELPHIRX102_UP}
  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. function IncludeJsonBraces(const json : string) : string;
  45. function RemoveJsonBraces(const json : string) : string;
  46. implementation
  47. function TObjectHelper.ToJSON : string;
  48. {$IFDEF DELPHIRX102_UP}
  49. var
  50. Serializer : TJsonSerializer;
  51. {$ENDIF}
  52. begin
  53. Result := '';
  54. try
  55. {$IFDEF DELPHIRX102_UP}
  56. Serializer := TJsonSerializer.Create;
  57. try
  58. Serializer.Formatting := TJsonFormatting.Indented;
  59. Result := Serializer.Serialize<TObject>(Self);
  60. finally
  61. Serializer.Free;
  62. end;
  63. {$ELSE}
  64. Result := TJson.ObjectToJsonString(Self);
  65. {$ENDIF}
  66. except
  67. on e : Exception do raise Exception.Create(e.Message);
  68. end;
  69. end;
  70. procedure TObjectHelper.FromJson(const json :string);
  71. var
  72. jObj : TJSONObject;
  73. begin
  74. try
  75. jObj := TJSonObject.ParseJSONValue(json,true) as TJSONObject;
  76. try
  77. TJson.JsonToObject(self,jObj);
  78. finally
  79. jObj.Free;
  80. end;
  81. except
  82. on e : Exception do raise Exception.Create(e.Message);
  83. end;
  84. end;
  85. procedure TObjectHelper.Clone(cObj : TObject);
  86. begin
  87. cObj.FromJson(Self.ToJson);
  88. end;
  89. function IncludeJsonBraces(const json : string) : string;
  90. begin
  91. if (not json.StartsWith('{')) and (not json.EndsWith('}')) then Result := '{' + json + '}'
  92. else Result := json;
  93. end;
  94. function RemoveJsonBraces(const json : string) : string;
  95. begin
  96. if (json.StartsWith('{')) and (json.EndsWith('}')) then Result := Copy(json,2,Json.Length - 2)
  97. else Result := json;
  98. end;
  99. end.