/*
 * !COPYRIGHT!
 *                      Copyright (c) 2007 Teleformix LLC
 *                       All Rights Reserved
 * 
 * 
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF TELEFORMIX LLC
 * 
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 * 
 * This document contains trade secret data and proprietary information of
 * Teleformix, LLC and should be treated as confidential.  No part of this 
 * information may be copied, or disclosed in part or in whole as permitted 
 * by Teleformix LLC
 * 
 * Copyright (c) 1998-2007 by Teleformix, LLC.
 * All Rights Reserved
 * 
 * $Id: json.js,v 1.3 2007/08/07 20:32:16 pdiverde Exp $
 * !!
 */

/*
	Merged from two versions of http://www.json.org/json.js

    Both versions released to the Public Domain
*/
var JSON = function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        if (!x.hasOwnProperty || x.hasOwnProperty(i)) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a.push(s.string(i), ':', v);
                                    b = true;
                                }
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };
    return {
/*
    Stringify a JavaScript value, producing a JSON text.
*/
        stringify: function (v) {
            var f = s[typeof v];
            if (f) {
                v = f(v);
                if (typeof v === 'string') {
                    return v;
                }
            }
            return;
        },
/*
    Parse a JSON text, producing a JavaScript value.
    It returns false if there is a syntax error.
*/
        parse: function (text) {
            var j;

            if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$/.test(text.
                    replace(/\\./g, '@').
                    replace(/"[^"\\\n\r]*"/g, ''))) {
                j = eval('(' + text + ')');

                return j;
            }

            return false;
        }
    };
}();
