Skip to content

Move JSONFormat into separate file json-format.js #89

Open

Description

@Joncom

The JSONFormat function is currently found inside jquery-1.7.1.min.js

(function(window) {
var p = [],
push = function( m ) { return '\\' + p.push( m ) + '\\'; };
pop = function( m, i ) { return p[i-1] };
tabs = function( count ) { return new Array( count + 1 ).join( '\t' ); };
window.JSONFormat = function( json ) {
p = [];
var out = "",
indent = 0;
// Extract backslashes and strings
json = json
.replace( /\\./g, push )
.replace( /(".*?"|'.*?')/g, push )
.replace( /\s+/, '' );
// Indent and insert newlines
for( var i = 0; i < json.length; i++ ) {
var c = json.charAt(i);
switch(c) {
case '{':
case '[':
out += c + "\n" + tabs(++indent);
break;
case '}':
case ']':
out += "\n" + tabs(--indent) + c;
break;
case ',':
out += ",\n" + tabs(indent);
break;
case ':':
out += ": ";
break;
default:
out += c;
break;
}
}
// Strip whitespace from numeric arrays and put backslashes
// and strings back in
out = out
.replace( /\[[\d,\s]+?\]/g, function(m){ return m.replace(/\s/g,''); } )
.replace( /\\(\d+)\\/g, pop );
return out;
};
})(window);

However, it does not seem to be related to jQuery in any way, and should probably be in its own file called json-format.js

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions