Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @description Takes in a JSON string and return a prettified version.
- /// @param string The JSON input string.
- var jsonOutput = "", tabWidth = 4, tabCount = 0, inString = false, inEscape = false;
- for(var i = 1; i < string_length(argument[0]) + 1; i++) {
- var charGet = string_char_at(argument[0], i);
- switch (charGet) {
- case "}": case "]": {
- if (inString == false) {
- jsonOutput += "\n" + string_repeat(chr($20), --tabCount * tabWidth);
- }
- break;}
- }
- if (inString == true) {
- jsonOutput += charGet;
- } else {
- if (charGet != chr($20)) {
- jsonOutput += charGet;
- }
- }
- switch (charGet) {
- case "{": case "[": {
- if (inString == false) {
- jsonOutput += "\n" + string_repeat(chr($20), ++tabCount * tabWidth);
- }
- break};
- case ":": {
- if (inString == false) {
- jsonOutput += " ";
- }
- break;}
- case ",": {
- if (inString == false) {
- jsonOutput += "\n" + string_repeat(chr($20), tabCount * tabWidth);
- }
- break;}
- case "\"": {
- if (inEscape == false) {
- inString = !inString;
- }
- break;}
- }
- if (inEscape == true) {
- inEscape = false;
- } else if (charGet == "\\") {
- inEscape = true;
- }
- }
- return jsonOutput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement