Quick.JSON.Utils.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. for i := 1 to json.Length do
  62. begin
  63. c := json[i];
  64. if isIntoString then
  65. begin
  66. isEOL := False;
  67. Result := Result + c;
  68. end
  69. else
  70. begin
  71. case c of
  72. ':' : Result := Result + c + SPACE;
  73. '{','[' :
  74. begin
  75. LIndent := LIndent + INDENT;
  76. if json[i+1] in ['}',']'] then Result := Result + c
  77. else Result := Result + c + EOL + LIndent;
  78. isEOL := True;
  79. end;
  80. ',' :
  81. begin
  82. isEOL := False;
  83. Result := Result + c + EOL + LIndent;
  84. end;
  85. '}',']' :
  86. begin
  87. Delete(LIndent, 1, Length(INDENT));
  88. if not isEOL then Result := Result + EOL;
  89. if json[i+1] = ',' then Result := Result + LIndent + c
  90. else if json[i-1] in ['{','['] then Result := Result + c + EOL
  91. else Result := Result + LIndent + c + EOL;
  92. isEOL := True;
  93. end;
  94. else
  95. begin
  96. isEOL := False;
  97. Result := Result + c;
  98. end;
  99. end;
  100. end;
  101. isEscape := (c = '\') and not isEscape;
  102. if not isEscape and (c = '"') then isIntoString := not isIntoString;
  103. end;
  104. end;
  105. end.