orkui/template/default/script/default.js
/**
 * Converts the given data structure to a JSON string.
 * Argument: arr - The data structure that must be converted to JSON
 * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
 * 			var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
 * http://www.openjs.com/scripts/data/json_encode.php
 */
Converts the given data structure to a JSON string.
Converts the given data structure to a JSON string .
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { 
            if(is_list) parts.push(array2json(value)); 
            else parts[key] = array2json(value); 
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';
            if(typeof value == "number") str += value; 
            else if(value === false) str += 'false'; 
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; 
            parts.push(str);
        }
    }
    var json = parts.join(",");
    if(is_list) return '[' + json + ']';
    return '{' + json + '}';
}
function array2json ( arr ) { var parts = [ ] ; var is_list = ( Object . prototype . toString . apply ( arr ) === '[object Array]' ) ; for ( var key in arr ) { var value = arr [ key ] ; if ( typeof value == "object" ) { if ( is_list ) parts . push ( array2json ( value ) ) ; else parts [ key ] = array2json ( value ) ; } else { var str = "" ; if ( ! is_list ) str = '"' + key + '":' ; if ( typeof value == "number" ) str += value ; else if ( value === false ) str += 'false' ; else if ( value === true ) str += 'true' ; else str += '"' + value + '"' ; parts . push ( str ) ; } } var json = parts . join ( "," ) ; if ( is_list ) return '[' + json + ']' ; return '{' + json + '}' ; }
Converts the given data structure to a JSON string.
Converts the given data structure to a JSON string .
//Custom handling for arrays /* :RECURSION: */ /* :RECURSION: */ //Custom handling for multiple data types //Numbers //The booleans //All other things // :TODO: Is there any more datatype we should be in the lookout for? (Functions?) //Return numerical JSON //Return associative JSON