2
0

Quick.JSON.Utils.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. { ***************************************************************************
  2. Copyright (c) 2015-2021 Kike Pérez
  3. Unit : Quick.JSON.Utils
  4. Description : Json utils
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 21/09/2018
  8. Modified : 09/03/2021
  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.Utils;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils;
  26. type
  27. TJsonUtils = class
  28. public
  29. class function IncludeJsonBraces(const json : string) : string;
  30. class function RemoveJsonBraces(const json : string) : string;
  31. class function JsonFormat(const json : string): string;
  32. end;
  33. implementation
  34. class function TJsonUtils.IncludeJsonBraces(const json : string) : string;
  35. begin
  36. if (not json.StartsWith('{')) and (not json.EndsWith('}')) then Result := '{' + json + '}'
  37. else Result := json;
  38. end;
  39. class function TJsonUtils.RemoveJsonBraces(const json : string) : string;
  40. begin
  41. if (json.StartsWith('{')) and (json.EndsWith('}')) then Result := Copy(json,2,Json.Length - 2)
  42. else Result := json;
  43. end;
  44. class function TJsonUtils.JsonFormat(const json : string) : string;
  45. const
  46. {$IFNDEF DELPHIRX10_UP}
  47. sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF}
  48. {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};
  49. {$ENDIF}
  50. INDENT = ' ';
  51. SPACE = ' ';
  52. var
  53. c : char;
  54. LIndent : string;
  55. isEOL : Boolean;
  56. isIntoString : Boolean;
  57. isEscape : Boolean;
  58. i : Integer;
  59. begin
  60. Result := '';
  61. isEOL := True;
  62. isIntoString := False;
  63. isEscape := False;
  64. {$IFNDEF NEXTGEN}
  65. for i := 1 to json.Length do
  66. {$ELSE}
  67. for i := 0 to json.Length - 1 do
  68. {$ENDIF}
  69. begin
  70. c := json[i];
  71. if isIntoString then
  72. begin
  73. isEOL := False;
  74. Result := Result + c;
  75. end
  76. else
  77. begin
  78. case c of
  79. ':' : Result := Result + c + SPACE;
  80. '{','[' :
  81. begin
  82. LIndent := LIndent + INDENT;
  83. if (json[i+1] = '}') or (json[i+1] = ']') then Result := Result + c
  84. else Result := Result + c + sLineBreak + LIndent;
  85. isEOL := True;
  86. end;
  87. ',' :
  88. begin
  89. isEOL := False;
  90. Result := Result + c + sLineBreak + LIndent;
  91. end;
  92. '}',']' :
  93. begin
  94. Delete(LIndent, 1, Length(INDENT));
  95. if not isEOL then Result := Result + sLineBreak ;
  96. if (i<json.Length) and (json[i+1] = ',') then Result := Result + LIndent + c
  97. else if (json[i-1] = '}') or (json[i-1] = ']') then Result := Result + c + sLineBreak
  98. else Result := Result + LIndent + c + sLineBreak ;
  99. isEOL := True;
  100. end;
  101. else
  102. begin
  103. isEOL := False;
  104. Result := Result + c;
  105. end;
  106. end;
  107. end;
  108. if not isEscape and (c = '"') then isIntoString := not isIntoString;
  109. isEscape := (c = '\') and not isEscape;
  110. end;
  111. end;
  112. end.