Quick.JSON.Utils.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 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 : 24/09/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.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. EOL = #13#10;
  47. INDENT = ' ';
  48. SPACE = ' ';
  49. var
  50. c : char;
  51. LIndent : string;
  52. isEOL : Boolean;
  53. isIntoString : Boolean;
  54. isEscape : Boolean;
  55. i : Integer;
  56. begin
  57. Result := '';
  58. isEOL := True;
  59. isIntoString := False;
  60. isEscape := False;
  61. {$IFNDEF NEXTGEN}
  62. for i := 1 to json.Length do
  63. {$ELSE}
  64. for i := 0 to json.Length - 1 do
  65. {$ENDIF}
  66. begin
  67. c := json[i];
  68. if isIntoString then
  69. begin
  70. isEOL := False;
  71. Result := Result + c;
  72. end
  73. else
  74. begin
  75. case c of
  76. ':' : Result := Result + c + SPACE;
  77. '{','[' :
  78. begin
  79. LIndent := LIndent + INDENT;
  80. if json[i+1] in ['}',']'] then Result := Result + c
  81. else Result := Result + c + EOL + LIndent;
  82. isEOL := True;
  83. end;
  84. ',' :
  85. begin
  86. isEOL := False;
  87. Result := Result + c + EOL + LIndent;
  88. end;
  89. '}',']' :
  90. begin
  91. Delete(LIndent, 1, Length(INDENT));
  92. if not isEOL then Result := Result + EOL;
  93. if json[i+1] = ',' then Result := Result + LIndent + c
  94. else if json[i-1] in ['{','['] then Result := Result + c + EOL
  95. else Result := Result + LIndent + c + EOL;
  96. isEOL := True;
  97. end;
  98. else
  99. begin
  100. isEOL := False;
  101. Result := Result + c;
  102. end;
  103. end;
  104. end;
  105. if not isEscape and (c = '"') then isIntoString := not isIntoString;
  106. isEscape := (c = '\') and not isEscape;
  107. end;
  108. end;
  109. end.