1

jQuery has altered it's implementation of $.parseJSON as of version 1.9.0 and we really depended on the way earlier versions of jQuery parsed null and empty strings, e.g. jQuery used to not throw an exception and would return a null value for null and empty string.

We would like to utilize the newest version of jQuery which at the time of writing is 1.9.1, but replace the implementation of $.parseJSON.

Documentation stating the change from jQuery: http://api.jquery.com/jQuery.parseJSON/

Is there some JavaScript we could use to tell jQuery to replace it's "natural" version of the $.parseJSON function with another implementation / function with the same name...the version from jQuery 1.8.3?

http://code.jquery.com/jquery-1.8.3.js has the function's implementation that we need.

3
  • have you tried the migrate plugin? Or you could simply test it yourself, if it's blank, skip ahead to null, else, pass it to $.parseJSON Commented Mar 27, 2013 at 3:43
  • I think it is a bad idea, still if you want just copy and paste the method to your local jquery file, but you wouldn't be able to use any CDN Commented Mar 27, 2013 at 3:46
  • Using jQuery.parseJSON(notReallyJSONVariable || "null") in your own code should do it. That will return a null if notReallyJSONVariable is an empty string, null, or other falsy values that aren't JSON. This solution is also suggested by the jQuery Migrate plugin when you use it. Commented Mar 28, 2013 at 13:57

3 Answers 3

3

If you must, do it this way:

jQuery._parseJSON = jQuery.parseJSON;

jQuery.parseJSON = function( data ) {

    if ( !data || typeof data !== "string") {
        return null;
    }

    return jQuery._parseJSON( data );

}
Sign up to request clarification or add additional context in comments.

Comments

2

I wouldn't recommend it, but if you still want to do it

create a jquery-override.js file and add the below contents to it

jQuery.parseJSON = function( data ) {
        if ( !data || typeof data !== "string") {
            return null;
        }

        // Make sure leading/trailing whitespace is removed (IE can't handle it)
        data = jQuery.trim( data );

        // Attempt to parse using the native JSON parser first
        if ( window.JSON && window.JSON.parse ) {
            return window.JSON.parse( data );
        }

        // Make sure the incoming data is actual JSON
        // Logic borrowed from http://json.org/json2.js
        if ( rvalidchars.test( data.replace( rvalidescape, "@" )
            .replace( rvalidtokens, "]" )
            .replace( rvalidbraces, "")) ) {

            return ( new Function( "return " + data ) )();

        }
        jQuery.error( "Invalid JSON: " + data );
    }

Then include this file after jquery-1.9.1.js file

1 Comment

If you take this advice, be sure to include the license for jQuery with it since it was a copy/pasta from that code.
0

If your question is related to the $.parseJSON() call that occurs in the context of jQuery's $.ajax() method, then here is a nice solution. You can override the default conversion from JSON String to JS Object by setting up a converter like so:

$.ajaxSetup({ 
            converters: { "text json": function (jsonString) {
                var jsonObj = mySpecialParsingMethod(jsonString);
                return jsonObj;
            } }
});

If you are not asking this question in regard to the $.ajax() method.....then nevermind. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.