maskofa

Pretty Format

Jan 5th, 2022
2,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.43 KB | None | 0 0
  1. var
  2.   cLevel: array[1..100] of Integer;
  3.  
  4. function  TfrmUtama.IncLevel(ALevel: Integer): Integer;
  5. begin
  6.   inc(ALevel);
  7.   cLevel[ALevel] := 0;
  8.   result := ALevel;
  9. end;
  10.  
  11. function  TfrmUtama.PrettyJSON(AJSON: TJSONObject; var level: Integer): String;
  12. const
  13.   cSpasi = 4;
  14. var
  15.   iii: Integer;
  16.   jsp: TJSONPair;
  17.   sss: String;
  18.   res: String;
  19.   cls: TClass;
  20.   sps: String;
  21.   cnt: Integer;
  22.   jjj: Integer;
  23.   jsa: TJSONObject;
  24.   buk: String;
  25. begin
  26.   res := '';
  27.  
  28.   for iii := 0 to AJSON.Count - 1 do
  29.   begin
  30.     jsp := AJSON.Pairs[iii];
  31.     sss := jsp.JsonString.Value;
  32.     cls := jsp.JsonValue.ClassType;
  33.     sps := DupeString(' ', Level * cSPasi);
  34.  
  35.     cLevel[Level] := cLevel[Level] + 1;
  36.  
  37.  
  38.     if cls = TJSONObject then
  39.     begin
  40.       if cLevel[Level] > 1 then
  41.       res := res + ','#13#10 else
  42.       res := res + #13#10;
  43.  
  44.       Level := incLevel(Level);
  45.       jsa := TJSONObject(jsp.JsonValue);
  46.       res := res + sps + '"' + sss + '" : {' + PrettyJSON(jsa, Level);
  47.       res := res + #13#10 + sps + '}';
  48.       dec(Level);
  49.     end else
  50.  
  51.     if cls = TJSONArray then
  52.     begin
  53.       if cLevel[Level] > 1 then
  54.       res := res + ','#13#10 else
  55.       res := res + #13#10;
  56.  
  57.       res := res + sps + '"' + sss + '" : [';
  58.       buk := sps;
  59.       sps := sps + DupeString(' ', cSPasi);
  60.       cnt := TJSONObject(jsp.JsonValue).Count;
  61.       for jjj := 0 to cnt - 1 do
  62.       begin
  63.         Level := incLevel(Level);
  64.  
  65.         if jjj > 0 then
  66.         res := res + ',';
  67.  
  68.         Level := incLevel(Level);
  69.  
  70.         jsa := TJSONObject(jsp.JsonValue.A[jjj]);
  71.         res := res + #13#10 + sps + '{';
  72.         res := res + sps + PrettyJSON(jsa, Level);
  73.         res := res + #13#10;
  74.         res := res + sps + '}';
  75.         dec(Level);
  76.         dec(Level);
  77.       end;
  78.       res := res + #13#10 + buk + ']';
  79.     end else
  80.     begin
  81.       if cLevel[Level] > 1 then
  82.       res := res + ','#13#10 else
  83.       res := res + #13#10;
  84.  
  85.       res := res  + format('%s"%s" : "%s"', [sps, sss, jsp.JsonValue.Value]);
  86.     end;
  87.   end;
  88.  
  89.   result := res;
  90. end;
  91.  
  92. function  TfrmUtama.PrettyFormat(AText: String): String;
  93. var
  94.   sss: String;
  95.   jso: TJSONObject;
  96.   lev: Integer;
  97. begin
  98.   sss := AText;
  99.   jso := TJSONObject(TJSONObject.ParseJSONValue(sss));
  100.  
  101.   if jso <> nil then
  102.   begin
  103.     lev := 1;
  104.     cLevel[1] := 0;
  105.  
  106.     sss := '{';
  107.     sss := sss + PrettyJSON(jso, lev);
  108.     sss := sss + #13#10'}';
  109.   end;
  110.  
  111.   result := sss;
  112. end;
Advertisement