3

The class I am using to parse JSON does not allow you to have colons in any string. For example, this would cause an error in parsing:

{
    "Test:": "Just a test"
}

Any ideas on how to make this class ignore colons that are in a string? Here is the class I am using:

class JSON {
    static var inst;
    var text;
    function JSON() {
    }
    static function getInstance() {
        if (inst == null) {
            inst = new cinqetdemi.JSON();
        }
        return (inst);
    }
    static function stringify(arg) {
        var _local3;
        var _local2;
        var _local6;
        var _local1 = "";
        var _local4;
        switch (typeof (arg)) {
            case "object" :
                if (arg) {
                    if (arg instanceof Array) {
                        _local2 = 0;
                        while (_local2 < arg.length) {
                            _local4 = stringify(arg[_local2]);
                            if (_local1) {
                                _local1 = _local1 + ",";
                            }
                            _local1 = _local1 + _local4;
                            _local2++;
                        }
                        return (("[" + _local1) + "]");
                    } else if (typeof (arg.toString) != "undefined") {
                        for (_local2 in arg) {
                            _local4 = arg[_local2];
                            if ((typeof (_local4) != "undefined") && (typeof (_local4) != "function")) {
                                _local4 = stringify(_local4);
                                if (_local1) {
                                    _local1 = _local1 + ",";
                                }
                                _local1 = _local1 + ((stringify(_local2) + ":") + _local4);
                            }
                        }
                        return (("{" + _local1) + "}");
                    }
                }
                return ("null");
            case "number" :
                return ((isFinite(arg) ? (String(arg)) : "null"));
            case "string" :
                _local6 = arg.length;
                _local1 = "\"";
                _local2 = 0;
                while (_local2 < _local6) {
                    _local3 = arg.charAt(_local2);
                    if (_local3 >= " ") {
                        if ((_local3 == "\\") || (_local3 == "\"")) {
                            _local1 = _local1 + "\\";
                        }
                        _local1 = _local1 + _local3;
                    } else {
                        switch (_local3) {
                            case "\b" :
                                _local1 = _local1 + "\\b";
                                break;
                            case "\f" :
                                _local1 = _local1 + "\\f";
                                break;
                            case newline :
                                _local1 = _local1 + "\\n";
                                break;
                            case "\r" :
                                _local1 = _local1 + "\\r";
                                break;
                            case "\t" :
                                _local1 = _local1 + "\\t";
                                break;
                            default :
                                _local3 = _local3.charCodeAt();
                                _local1 = _local1 + (("\\u00" + Math.floor(_local3 / 16).toString(16)) + (_local3 % 16).toString(16));
                        }
                    }
                    _local2 = _local2 + 1;
                }
                return (_local1 + "\"");
            case "boolean" :
                return (String(arg));
        }
        return ("null");
    }
    static function parse(text) {
        if (!text.length) {
            throw new Error("JSONError: Text missing");
        }
        var _local1 = getInstance();
        _local1.at = 0;
        _local1.ch = " ";
        _local1.text = text;
        return (_local1.value());
    }
    function error(m) {
        var _local2 = ((("JSONError: " + m) + " at ") + (at - 1)) + newline;
        _local2 = _local2 + (text.substr(at - 10, 20) + newline);
        _local2 = _local2 + "        ^";
        throw new Error(_local2);
    }
    function next() {
        ch = text.charAt(at);
        at = at + 1;
        return (ch);
    }
    function white() {
        while (ch) {
            if (ch <= " ") {
                next();
            } else if (ch == "/") {
                switch (next()) {
                    case "/" :
                        while ((next() && (ch != newline)) && (ch != "\r")) {
                        }
                        break;
                    case "*" :
                        next();
                        while (true) {
                            if (ch) {
                                if (ch == "*") {
                                    if (next() == "/") {
                                        next();
                                        break;
                                    }
                                } else {
                                    next();
                                }
                            } else {
                                error("Unterminated comment");
                            }
                        }
                        break;
                    default :
                        error("Syntax error");
                }
            } else {
                break;
            }
        }
    }
    function str() {
        var _local5;
        var _local2 = "";
        var _local4;
        var _local3;
        var _local6 = false;
        if ((ch == "\"") || (ch == "'")) {
            var _local7 = ch;
            while (next()) {
                if (ch == _local7) {
                    next();
                    return (_local2);
                } else if (ch == "\\") {
                    switch (next()) {
                        case "b" :
                            _local2 = _local2 + "\b";
                            break;
                        case "f" :
                            _local2 = _local2 + "\f";
                            break;
                        case "n" :
                            _local2 = _local2 + newline;
                            break;
                        case "r" :
                            _local2 = _local2 + "\r";
                            break;
                        case "t" :
                            _local2 = _local2 + "\t";
                            break;
                        case "u" :
                            _local3 = 0;
                            _local5 = 0;
                            while (_local5 < 4) {
                                _local4 = parseInt(next(), 16);
                                if (!isFinite(_local4)) {
                                    _local6 = true;
                                    break;
                                }
                                _local3 = (_local3 * 16) + _local4;
                                _local5 = _local5 + 1;
                            }
                            if (_local6) {
                                _local6 = false;
                                break;
                            }
                            _local2 = _local2 + String.fromCharCode(_local3);
                            break;
                        default :
                            _local2 = _local2 + ch;
                    }
                } else {
                    _local2 = _local2 + ch;
                }
            }
        }
        error("Bad string");
    }
    function key() {
        var _local2 = ch;
        var _local6 = false;
        var _local3 = text.indexOf(":", at);
        var _local4 = text.indexOf("\"", at);
        var _local5 = text.indexOf("'", at);
        if (((_local4 <= _local3) && (_local4 > -1)) || ((_local5 <= _local3) && (_local5 > -1))) {
            _local2 = str();
            white();
            if (ch == ":") {
                return (_local2);
            } else {
                error("Bad key");
            }
        }
        while (next()) {
            if (ch == ":") {
                return (_local2);
            }
            if (ch <= " ") {
            } else {
                _local2 = _local2 + ch;
            }
        }
        error("Bad key");
    }
    function arr() {
        var _local2 = [];
        if (ch == "[") {
            next();
            white();
            if (ch == "]") {
                next();
                return (_local2);
            }
            while (ch) {
                if (ch == "]") {
                    next();
                    return (_local2);
                }
                _local2.push(value());
                white();
                if (ch == "]") {
                    next();
                    return (_local2);
                } else if (ch != ",") {
                    break;
                }
                next();
                white();
            }
        }
        error("Bad array");
    }
    function obj() {
        var _local3;
        var _local2 = {};
        if (ch == "{") {
            next();
            white();
            if (ch == "}") {
                next();
                return (_local2);
            }
            while (ch) {
                if (ch == "}") {
                    next();
                    return (_local2);
                }
                _local3 = this.key();
                if (ch != ":") {
                    break;
                }
                next();
                _local2[_local3] = value();
                white();
                if (ch == "}") {
                    next();
                    return (_local2);
                } else if (ch != ",") {
                    break;
                }
                next();
                white();
            }
        }
        error("Bad object");
    }
    function num() {
        var _local2 = "";
        var _local3;
        if (ch == "-") {
            _local2 = "-";
            next();
        }
        while (((((ch >= "0") && (ch <= "9")) || (ch == "x")) || ((ch >= "a") && (ch <= "f"))) || ((ch >= "A") && (ch <= "F"))) {
            _local2 = _local2 + ch;
            next();
        }
        if (ch == ".") {
            _local2 = _local2 + ".";
            next();
            while ((ch >= "0") && (ch <= "9")) {
                _local2 = _local2 + ch;
                next();
            }
        }
        if ((ch == "e") || (ch == "E")) {
            _local2 = _local2 + ch;
            next();
            if ((ch == "-") || (ch == "+")) {
                _local2 = _local2 + ch;
                next();
            }
            while ((ch >= "0") && (ch <= "9")) {
                _local2 = _local2 + ch;
                next();
            }
        }
        _local3 = Number(_local2);
        if (!isFinite(_local3)) {
            error("Bad number");
        }
        return (_local3);
    }
    function word() {
        switch (ch) {
            case "t" :
                if (((next() == "r") && (next() == "u")) && (next() == "e")) {
                    next();
                    return (true);
                }
                break;
            case "f" :
                if ((((next() == "a") && (next() == "l")) && (next() == "s")) && (next() == "e")) {
                    next();
                    return (false);
                }
                break;
            case "n" :
                if (((next() == "u") && (next() == "l")) && (next() == "l")) {
                    next();
                    return (null);
                }
                break;
        }
        error("Syntax error");
    }
    function value() {
        white();
        switch (ch) {
            case "{" :
                return (obj());
            case "[" :
                return (arr());
            case "\"" :
            case "'" :
                return (str());
            case "-" :
                return (num());
        }
        return ((((ch >= "0") && (ch <= "9")) ? (num()) : (word())));
    }
    var at = 0;
    var ch = " ";
}
Matt
  • 93
  • 2
  • 11
  • Did you try to escape the colons with a backslash? – MiiinimalLogic Jun 23 '15 at 19:03
  • This looks like decompiled or automatically generated code. If it's automatically generated, do you have the source it's being generated from? That might be an easier thing to modify. – Dan Getz Jun 23 '15 at 19:04
  • @MiiinimalLogic it's a string, there should be no reason to have to escape a colon in it. The question was how to fix the parsing code, not how to work around it. – Dan Getz Jun 23 '15 at 19:05
  • It looks like this library: https://github.com/CaravaggioSoftwareDev/json-as2/blob/master/JSON.as – Cᴏʀʏ Jun 23 '15 at 19:12
  • @Cᴏʀʏ Oh god, I was wrong. I had thought the original code authors would be able to name their variables better than an obfuscator or decompiler or whatever's been run on this... – Dan Getz Jun 23 '15 at 19:14
  • 2
    I'd go for a different parser. For example, JSON doesn't allow single quotes for strings, this parser does. JSON doesn't allow unescaped control characters, this parser does. – gnasher729 Jun 23 '15 at 19:20
  • 2
    Seeing that not only is this rejecting colons in some strings, but also accepting strings delimited by single quotes, this isn't a JSON parser to begin with. And the reason it's not working for your situation is that the `key()` function is terribly written. There's multiple things wrong with it, and I'm not sure if it's worth the time trying to debug it, when who knows if there are other bugs in other parts of the code that you haven't found yet. – Dan Getz Jun 23 '15 at 19:21
  • 1
    It parses comments. That's so wrong. It doesn't parse Unicode outside the base page, so no Emojis for you :-) – gnasher729 Jun 23 '15 at 19:24

1 Answers1

2

Take a look on my answer of this question and use the JSON class that I mentioned there, it's working fine with your test content :

import JSON;

var json = new JSON();
var data:String = '{"Test:": "Just a test"}';

trace(json.parse(data)['Test:']);   // gives : Just a test

Hope that can help.

Community
  • 1
  • 1
akmozo
  • 9,829
  • 3
  • 28
  • 44
  • The class I was using is supposed to be the same as the class you provided. Mine probably had issues as it was decompiled. It's working great now. Thanks! – Matt Jun 27 '15 at 20:27
  • how to do that in java-script – sasha romanov Aug 29 '19 at 05:03
  • @sasharomanov It's almost the same in JS : `console.log(JSON.parse('{"Test:": "Just a test"}').Test); // gives : Just a test` ... – akmozo Aug 29 '19 at 07:41
  • @akmozo, i mean how to use this without importing JSON, from external source, how to use pure `JSON.parse('{"Test:":"Just a test"}');` without getting any errors – sasha romanov Aug 29 '19 at 17:29
  • 1
    @sasharomanov If you are speaking about JavaScript, you shouldn't get any error when using `JSON` because all modern browsers support native `JSON` encoding and decoding. Also ActionScript 3 has native `JSON` functionality (see [here](https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html)), but if you are speaking about ActionScript 2 then you should import JSON from external source. – akmozo Aug 30 '19 at 07:20