Monday, May 2, 2011

Upgrade to Prototype 1.7 = JSON serialization problem

Recently I had to upgrade our Prototype JavaScript framework, version 1.6.0.2 to version 1.7. The reason was assure compatibility of our website with the Internet Explorer 9.
The main problem after the upgrade was that one of our AJAX component stopped working. This component is using JSON serialization. The deserialization on client side had some issues.
The reason was that new Prototype version 1.7 does not properly prepare (clean up) the passed JSON string and leaves there: 

<?xml version="1.0" encoding="utf-8"?>

Therefore I had to remove this string before the JSON string got processed.
The issue is in: stripTags() function. (line number 574)
I had to change the stripTags function directly in the Prototype JavaScript file from :

function stripTags() {
return this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
}

to:

function stripTags() {
var result = this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
result = result.replace('<?xml version="1.0" encoding="utf-8"?>', '');
return result;
}

Bingo! After that all started to work how it supposed to!

I hope this solution will help somebody.
I am not really good in RegEx. If somebody has better (cleaner) solution, I’ll be happy to swap into it.
Cheers!