View Full Version : Run function on rawData before JSON reader parses it

15 Dec 2011, 5:11 PM
I have a DataView set up like this (BrowsePage extends Ext.DataView):

Ext.create('my.app.BrowsePage', {
id: 'searchResults',
itemTpl: '<h1>hi!</h1>',
store: {
autoLoad: true,
fields: ['applications', 'query'],
proxy: {
type: 'ajax',
url: Settings.searchUrl,
reader: {
type: 'json',
root: 'MyApp::Search',
totalProperty: 'matchcount'

But Settings.searchUrl contains invalid JSON (and this will eventually be on a foreign server so I can't control that data). I need to run my custom Data.stripChars() function on the string before the JSON reader tries to parse it. Does anyone know of a way to do this while still defining my ajax proxy within the Store like this?

15 Dec 2011, 5:33 PM
Well if anyone is interested, you can override getResponseData like this, then use 'myjson' as the reader type instead of json. It works for now --

Ext.define('Ext.data.reader.MyJson', {
extend: 'Ext.data.reader.Json',
alias: 'reader.myjson',
//inherit docs

// warning: this regex sucks!
cleanForJSON: function(str) {
return str.replace(/[^a-z 0-9\{\}\[\]\/\\\!<>:"'\.\$\-_,]+/gi,'');

getResponseData: function(response) {
try {
var data = Ext.decode(this.cleanForJSON(response.responseText));
catch (ex) {
response: response,
json: response.responseText,
parseError: ex,
msg: 'Unable to parse the JSON returned by the server: ' + ex.toString()
if (!data) {
Ext.Error.raise('JSON object not found');

return data;