Quick.Json.fpc.Compatibility.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.Json.fpc.Compatibility (only freepascal)
  4. Description : Delphi Json compatibility functions
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 09/03/2018
  8. Modified : 27/05/2019
  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.fpc.Compatibility;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. fpjson,
  28. jsonparser;
  29. type
  30. TJsonPair = class
  31. private
  32. fJsonString : string;
  33. fJsonValue : TJsonData;
  34. public
  35. constructor Create(const aName : string; aValue : TJsonData);
  36. property JsonString : string read fJsonString write fJsonString;
  37. property JsonValue : TJsonData read fJsonValue write fJsonValue;
  38. end;
  39. { TJsonArray }
  40. TJsonArray = class(fpjson.TJSONArray)
  41. public
  42. procedure AddElement(aValue : TJsonData);
  43. end;
  44. { TJsonObject }
  45. TJsonObject = class(fpjson.TJsonObject)
  46. private
  47. function GetPair(Index : Integer): TJsonPair;
  48. public
  49. procedure AddPair(aValue : TJsonPair); overload;
  50. procedure AddPair(const aName : TJsonStringType; aValue : TJsonData); overload;
  51. class function ParseJSONValue(const JSON: string; const UseUTF8: Boolean = True): TJSONData;
  52. function GetValue(const aName : string) : TJsonData;
  53. function ToJson : TJSONStringType;
  54. property Pairs[Index : Integer] : TJsonPair read GetPair; default;
  55. end;
  56. TJsonValue = TJsonData;
  57. TJsonNumber = class(fpjson.TJsonIntegerNumber)
  58. public
  59. constructor Create(aValue : Integer); overload;
  60. constructor Create(aValue : Extended); overload;
  61. end;
  62. TJsonString = class(fpjson.TJsonString)
  63. public
  64. constructor Create(const aValue : string); overload;
  65. end;
  66. TJsonBool = class(fpjson.TJSONBoolean)
  67. public
  68. constructor Create(aValue : Boolean);
  69. end;
  70. implementation
  71. { TJsonArray }
  72. procedure TJsonArray.AddElement(aValue: TJsonData);
  73. begin
  74. Add(aValue);
  75. end;
  76. function TJsonObject.GetPair(Index : Integer): TJsonPair;
  77. begin
  78. Result := TJsonPair.Create(Self.Names[Index],Self.Items[Index]);
  79. end;
  80. procedure TJsonObject.AddPair(aValue : TJsonPair);
  81. begin
  82. Add(aValue.JsonString,aValue.JsonValue);
  83. end;
  84. procedure TJsonObject.AddPair(const aName: TJsonStringType; aValue: TJsonData);
  85. begin
  86. Add(aName,aValue);
  87. end;
  88. class function TJsonObject.ParseJSONValue(const JSON: string; const UseUTF8: Boolean = True): TJSONData;
  89. begin
  90. Result := GetJson(JSON,UseUTF8);
  91. end;
  92. function TJsonObject.ToJson : TJSONStringType;
  93. begin
  94. try
  95. Result := AsJson;
  96. except
  97. raise Exception.Create('Json not valid');
  98. end;
  99. end;
  100. function TJsonObject.GetValue(const aName : string) : TJsonData;
  101. begin
  102. Result := Find(aName);
  103. end;
  104. constructor TJsonPair.Create(const aName : string; aValue : TJsonData);
  105. begin
  106. fJsonString := aName;
  107. fJsonValue := aValue;
  108. end;
  109. constructor TJsonNumber.Create(aValue : Integer);
  110. begin
  111. inherited Create(aValue);
  112. end;
  113. constructor TJsonNumber.Create(aValue : Extended);
  114. begin
  115. Create(aValue);
  116. end;
  117. constructor TJsonBool.Create(aValue : Boolean);
  118. begin
  119. inherited Create(aValue);
  120. end;
  121. constructor TJsonString.Create(const aValue : string);
  122. begin
  123. inherited Create(aValue);
  124. end;
  125. end.