svgui/pyjs/lib/json.js
author laurent
Wed, 23 May 2012 10:37:43 +0200
changeset 746 2e09777a40d3
parent 728 e0424e96e3fd
permissions -rw-r--r--
Fix refresh of ConfTreeNodeEditors content when values change
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
json_parse = (function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
// This is a function that can parse a JSON text, producing a JavaScript
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
// data structure. It is a simple, recursive descent parser. It does not use
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     5
// eval or regular expressions, so it can be used as a model for implementing
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     6
// a JSON parser in other languages.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
// We are defining the function inside of another function to avoid creating
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
// global variables.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
    var at,     // The index of the current character
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    12
        ch,     // The current character
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    13
        escapee = {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    14
            '"':  '"',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    15
            '\\': '\\',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    16
            '/':  '/',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
            b:    '\b',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
            f:    '\f',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
            n:    '\n',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
            r:    '\r',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
            t:    '\t'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
        text,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
        error = function (m) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
// Call error when something is wrong.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
            throw {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
                name:    'SyntaxError',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
                message: m,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
                at:      at,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
                text:    text
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
            };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
        next = function (c) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
// If a c parameter is provided, verify that it matches the current character.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
            if (c && c !== ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
                error("Expected '" + c + "' instead of '" + ch + "'");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
// Get the next character. When there are no more characters,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
// return the empty string.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
            ch = text.charAt(at);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
            at += 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
            return ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
        number = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
// Parse a number value.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
            var number,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
                string = '';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
            if (ch === '-') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
                string = '-';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
                next('-');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
            while (ch >= '0' && ch <= '9') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
                string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
                next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
            if (ch === '.') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
                string += '.';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
                while (next() && ch >= '0' && ch <= '9') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
                    string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
            if (ch === 'e' || ch === 'E') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
                string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
                next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
                if (ch === '-' || ch === '+') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
                    string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
                    next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
                while (ch >= '0' && ch <= '9') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
                    string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
                    next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
            number = +string;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
            if (isNaN(number)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
                error("Bad number");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
            } else {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
                return number;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
        string = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
// Parse a string value.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
            var hex,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
                i,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
                string = '',
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
                uffff;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
// When parsing for string values, we must look for " and \ characters.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
            if (ch === '"') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
                while (next()) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
                    if (ch === '"') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   108
                        next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
                        return string;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
                    } else if (ch === '\\') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   111
                        next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
                        if (ch === 'u') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
                            uffff = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
                            for (i = 0; i < 4; i += 1) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
                                hex = parseInt(next(), 16);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
                                if (!isFinite(hex)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   117
                                    break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   118
                                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
                                uffff = uffff * 16 + hex;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
                            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   121
                            string += String.fromCharCode(uffff);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   122
                        } else if (typeof escapee[ch] === 'string') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   123
                            string += escapee[ch];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
                        } else {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   125
                            break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
                        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
                    } else {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
                        string += ch;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   129
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   131
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   132
            error("Bad string");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   133
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
        white = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   136
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   137
// Skip whitespace.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   138
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   139
            while (ch && ch <= ' ') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   140
                next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   141
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   142
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   143
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   144
        word = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   145
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   146
// true, false, or null.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   147
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   148
            switch (ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   149
            case 't':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   150
                next('t');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   151
                next('r');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   152
                next('u');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   153
                next('e');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   154
                return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   155
            case 'f':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   156
                next('f');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   157
                next('a');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   158
                next('l');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   159
                next('s');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   160
                next('e');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   161
                return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   162
            case 'n':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   163
                next('n');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   164
                next('u');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   165
                next('l');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   166
                next('l');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   167
                return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   168
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   169
            error("Unexpected '" + ch + "'");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   170
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   171
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   172
        value,  // Place holder for the value function.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   173
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   174
        array = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   175
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   176
// Parse an array value.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   177
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   178
            var array = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   179
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   180
            if (ch === '[') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   181
                next('[');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   182
                white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   183
                if (ch === ']') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   184
                    next(']');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   185
                    return array;   // empty array
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   186
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   187
                while (ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   188
                    array.push(value());
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   189
                    white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   190
                    if (ch === ']') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   191
                        next(']');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   192
                        return array;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   194
                    next(',');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   195
                    white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   197
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
            error("Bad array");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
        object = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   203
// Parse an object value.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   205
            var key,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
                object = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   208
            if (ch === '{') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   209
                next('{');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   210
                white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   211
                if (ch === '}') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
                    next('}');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   213
                    return object;   // empty object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   214
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
                while (ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
                    key = string();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
                    white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
                    next(':');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   219
                    if (Object.hasOwnProperty.call(object, key)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
                        error('Duplicate key "' + key + '"');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   221
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   222
                    object[key] = value();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   223
                    white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   224
                    if (ch === '}') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   225
                        next('}');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
                        return object;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   227
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   228
                    next(',');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   229
                    white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   230
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   231
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   232
            error("Bad object");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   233
        };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   234
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   235
    value = function () {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   236
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   237
// Parse a JSON value. It could be an object, an array, a string, a number,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   238
// or a word.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   239
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   240
        white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   241
        switch (ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   242
        case '{':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   243
            return object();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   244
        case '[':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   245
            return array();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   246
        case '"':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   247
            return string();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   248
        case '-':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   249
            return number();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   250
        default:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   251
            return ch >= '0' && ch <= '9' ? number() : word();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   252
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   253
    };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   254
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   255
// Return the json_parse function. It will have access to all of the above
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   256
// functions and variables.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   257
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   258
    return function (source, reviver) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   259
        var result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   260
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   261
        text = source;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   262
        at = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   263
        ch = ' ';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   264
        result = value();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   265
        white();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   266
        if (ch) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   267
            error("Syntax error");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   268
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   269
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   270
// If there is a reviver function, we recursively walk the new structure,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   271
// passing each name/value pair to the reviver function for possible
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   272
// transformation, starting with a temporary root object that holds the result
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   273
// in an empty key. If there is not a reviver function, we simply return the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   274
// result.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   275
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   276
        return typeof reviver === 'function' ? (function walk(holder, key) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   277
            var k, v, value = holder[key];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   278
            if (value && typeof value === 'object') {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   279
                for (k in value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   280
                    if (Object.hasOwnProperty.call(value, k)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   281
                        v = walk(value, k);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   282
                        if (v !== undefined) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   283
                            value[k] = v;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   284
                        } else {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   285
                            delete value[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   286
                        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   287
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   288
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   289
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   290
            return reviver.call(holder, key, value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   291
        }({'': result}, '')) : result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   292
    };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   293
}());