Why do not implement this function, that already exists in Prototype.js ?
I actually don't know how to implement the script on the core, so, here is the snippet.
This script implements $(object).isJSON, $.isJSON(object) and [String].isJSON() functions
Feel free to fix any bug.
- (function ($) {
- $.isJSON = function (json) {
- json = json.replace(/\\["\\\/bfnrtu]/g, '@');
- json = json.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
- json = json.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
- return (/^[\],:{}\s]*$/.test(json))
- }
- $.fn.isJSON = function () {
- var json = this;
- if (jQuery(json).is(":input")) {
- json = jQuery(json).val();
- json = new String(json);
- return jQuery.isJSON(json)
- } else {
- throw new SyntaxError("$(object).isJSON only accepts fields!");
- }
- }
- String.prototype.isJSON = function () {
- var y = this;
- return jQuery.isJSON(y);
- }
- })(jQuery);
this probably will help everyone that uses json
Victor G.