javascript - How does Crockfords JSON Parser work? - Stack Overflow

I have stared for a long time at the code found here. It's Douglas Crockfords JSON-parsing functio

I have stared for a long time at the code found here. It's Douglas Crockfords JSON-parsing function (called a recursive descent parser). Can anyone elaborate on the mechanics of this parser? I really can't get my head around it.

I have stared for a long time at the code found here. It's Douglas Crockfords JSON-parsing function (called a recursive descent parser). Can anyone elaborate on the mechanics of this parser? I really can't get my head around it.

Share Improve this question asked Apr 9, 2011 at 9:00 KooiIncKooiInc 123k32 gold badges145 silver badges181 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Logically you may start with the actual parse functions which starts at line 311 (omitted the receiver part for clarity).

function (source, reviver) {
    var result;

    text = source;
    at = 0;
    ch = ' ';
    result = value();
    white();
    if (ch) {
        error("Syntax error");
    }

    return result;
}

Initializes function global variables text with the source text, position at with position and current character ch with a space. Afterwards it parses a value by calling function value.

Each object to be parsed is encapsulated in a function itself (in above example the value object). There are several of them: number, string, white, ...). Each one does basically work in the same way. First we'll look into white as basic example:

white = function () {

    // Skip whitespace.

    while (ch && ch <= ' ') {
        next();
    }
}

Note that ch constains always the current character. This variable is only updated by next which reads in the next one. This can be seen within white where each whitespace is eaten by a call to next. Thus after calling this function the first non-space character will be in variable ch.

Let's look for a more plex example value:

value = function () {

// Parse a JSON value. It could be an object, an array, a string, a number,
// or a word.

    white();
    switch (ch) {
    case '{':
        return object();
    case '[':
        return array();
    case '"':
        return string();
    case '-':
        return number();
    default:
        return ch >= '0' && ch <= '9' ? number() : word();
    }
};

It first parses whitespaces by calling white. Note that ch now contains the current character to be parsed. If it is a '{' we'll now that a json object is ing next and call the corresponding function object. If instead it is a '[' we expect an json array and so on.

All other functions are build the same way: inspect the current character, decide what has to e next and then parse this object.

The object itself may contain other values and therefore you'll find an indirect recursive call of function value in object again. Thus by recursively calling all the json object functions they are actually parsed from the source string.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745378248a4625101.html

相关推荐

  • javascript - How does Crockfords JSON Parser work? - Stack Overflow

    I have stared for a long time at the code found here. It's Douglas Crockfords JSON-parsing functio

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信