Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Crockfords JSON Parser work?

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.

like image 313
KooiInc Avatar asked Dec 27 '22 22:12

KooiInc


1 Answers

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 complex 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 coming 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 come 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.

like image 172
Howard Avatar answered Jan 07 '23 00:01

Howard