Created
March 16, 2012 17:46
-
-
Save joseafga/2051416 to your computer and use it in GitHub Desktop.
JSON Stringify function for jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jQuery JSON Stringify | |
* see also http://www.json.org/ | |
* | |
* Copyright: José Almeida (http://joseafga.com.br) | |
* License: MIT (http://opensource.org/licenses/mit-license.php) | |
* | |
* Based on: Mootools JSON.encode function (http://mootools.net/) Copyright (c) 2006-2010 Valerio Proietti (http://mad4milk.net/) | |
* | |
* Syntax: | |
* $.stringifyJSON(mixed); | |
*/ | |
var special = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'}, | |
escape = function(chr){ return special[chr] || '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4); }; | |
jQuery.stringifyJSON = function(data){ | |
// use native if exists | |
if (window.JSON && window.JSON.stringify) | |
return window.JSON.stringify(data); | |
switch (jQuery.type(data)){ | |
case 'string': | |
return '"' + data.replace(/[\x00-\x1f\\"]/g, escape) + '"'; | |
case 'array': | |
return '[' + jQuery.map(data, jQuery.stringifyJSON) + ']'; | |
case 'object': | |
var string = []; | |
jQuery.each(data, function(key, val){ | |
var json = jQuery.stringifyJSON(val); | |
if (json) | |
string.push(jQuery.stringifyJSON(key) + ':' + json); | |
}); | |
return '{' + string + '}'; | |
case 'number': | |
case 'boolean': | |
return '' + data; | |
case 'undefined': | |
case 'null': | |
return 'null'; | |
} | |
return data; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jQuery JSON Stringify | |
* | |
* Copyright: José Almeida (http://joseafga.com.br) | |
* License: MIT | |
* | |
* Based on: Mootools JSON.encode function (http://mootools.net/) Copyright 2006-2010 Valerio Proietti (http://mad4milk.net/) | |
*/ | |
var special={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},escape=function(a){return special[a]||"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4);};jQuery.stringifyJSON=function(b){if(window.JSON&&window.JSON.stringify){return window.JSON.stringify(b);}switch(jQuery.type(b)){case"string":return'"'+b.replace(/[\x00-\x1f\\"]/g,escape)+'"';case"array":return"["+jQuery.map(b,jQuery.stringifyJSON)+"]";case"object":var a=[];jQuery.each(b,function(d,e){var c=jQuery.stringifyJSON(e);if(c){a.push(jQuery.stringifyJSON(d)+":"+c);}});return"{"+a+"}";case"number":case"boolean":return""+b;case"undefined":case"null":return"null";}return b;}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment