Advertisement
Nommiin

json_pretty(string)

Mar 18th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @description Takes in a JSON string and return a prettified version.
  2. /// @param string The JSON input string.
  3. var jsonOutput = "", tabWidth = 4, tabCount = 0, inString = false, inEscape = false;
  4. for(var i = 1; i < string_length(argument[0]) + 1; i++) {
  5.     var charGet = string_char_at(argument[0], i);
  6.     switch (charGet) {
  7.         case "}": case "]": {
  8.             if (inString == false) {
  9.                 jsonOutput += "\n" + string_repeat(chr($20), --tabCount * tabWidth);
  10.             }
  11.         break;}
  12.     }
  13.        
  14.     if (inString == true) {
  15.         jsonOutput += charGet;
  16.     } else {
  17.         if (charGet != chr($20)) {
  18.             jsonOutput += charGet;
  19.         }
  20.     }
  21.        
  22.     switch (charGet) {
  23.         case "{": case "[": {
  24.             if (inString == false) {
  25.                 jsonOutput += "\n" + string_repeat(chr($20), ++tabCount * tabWidth);
  26.             }
  27.         break};
  28.            
  29.         case ":": {
  30.             if (inString == false) {
  31.                 jsonOutput += " ";
  32.             }
  33.         break;}
  34.            
  35.         case ",": {
  36.             if (inString == false) {
  37.                 jsonOutput += "\n" + string_repeat(chr($20), tabCount * tabWidth);
  38.             }
  39.         break;}
  40.            
  41.         case "\"": {
  42.             if (inEscape == false) {
  43.                 inString = !inString;
  44.             }
  45.         break;}
  46.     }
  47.    
  48.     if (inEscape == true) {
  49.         inEscape = false;
  50.     } else if (charGet == "\\") {
  51.         inEscape = true;
  52.     }
  53. }
  54.  
  55. return jsonOutput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement