';
} else {
output += '
';
}
output += this.valueToHTML( json );
output += '
';
if ( callback ) {
output += '
)
';
}
return this.toHTML( output, uri );
}, // Produce an error document for when parsing fails.
errorPage: function( error, data, uri ) {
// var output = '
' + this.stringbundle.GetStringFromName('errorParsing') + '
';
// output += '
' + this.stringbundle.GetStringFromName('docContents') + ':
';.
var output = '
Error parsing JSON: ' + error.message + '
';
output += '
' + error.stack + ':
';
output += '
' + this.htmlEncode( data ) + '
';
return this.toHTML( output, uri + ' - Error' );
}, // Wrap the HTML fragment in a full document. Used by jsonToHTML and errorPage.
toHTML: function( content ) {
return content;
}
};
// Sanitize & output -- all magic from JSONView Firefox.
this.jsonFormatter = new JSONFormatter();
// This regex attempts to match a JSONP structure:
// * Any amount of whitespace (including unicode nonbreaking spaces) between the start of the file and the callback name.
// * Callback name (any valid JavaScript function name according to ECMA-262 Edition 3 spec).
// * Any amount of whitespace (including unicode nonbreaking spaces).
// * Open parentheses.
// * Any amount of whitespace (including unicode nonbreaking spaces).
// * Either { or [, the only two valid characters to start a JSON string.
// * Any character, any number of times.
// * Either } or ], the only two valid closing characters of a JSON string.
// * Any amount of whitespace (including unicode nonbreaking spaces).
// * A closing parenthesis, an optional semicolon, and any amount of whitespace (including unicode nonbreaking spaces) until the end of the file.
// This will miss anything that has comments, or more than one callback, or requires modification before use.
var outputDoc = '';
// text = text.match(jsonp_regex)[1]; .
var cleanData = '', callback = '';
var callback_results = jsonp_regex.exec( this.data );
if ( callback_results && callback_results.length === 3 ) {
if ( this.debug ) {
console.log( 'THIS IS JSONp' );
}
callback = callback_results[1];
cleanData = callback_results[2];
} else {
if ( this.debug ) {
console.log( 'Vanilla JSON' );
}
cleanData = this.data;
}
if ( this.debug ) {
console.log( cleanData );
}
// Covert, and catch exceptions on failure.
try {
// var jsonObj = this.nativeJSON.decode(cleanData); .
var jsonObj = JSON.parse( cleanData );
if ( jsonObj ) {
outputDoc = this.jsonFormatter.jsonToHTML( jsonObj, callback );
} else {
throw 'There was no object!';
}
} catch ( e ) {
if ( this.debug ) {
console.log( e );
}
outputDoc = this.jsonFormatter.errorPage( e, this.data );
}
var links = '';
if ( this.targetType !== undefined ) {
this.idType = this.targetType;
this.id = this.target;
}
var el;
if ( this.idType === 'class' ) {
el = document.getElementsByClassName( this.id );
if ( el ) {
el.className += el.className ? ' jsonViewOutput' : 'jsonViewOutput';
el.innerHTML = links + outputDoc;
}
} else if ( this.idType === 'id' ) {
el = document.getElementById( this.id );
if ( el ) {
el.className += el.className ? ' jsonViewOutput' : 'jsonViewOutput';
el.innerHTML = links + outputDoc;
}
el.innerHTML = links + outputDoc;
}
var items = document.getElementsByClassName( 'collapsible' );
var len = items.length;
for ( var i = 0; i < len; i ++ ) {
addCollapser( items[i].parentNode );
}
} else {
// console.log("JSONView: this is not json, not formatting."); .
}
}