mediawiki-extensions-Visual.../modules/es/serializers/es.JsonSerializer.js

101 lines
2.2 KiB
JavaScript
Raw Normal View History

/**
* Serializes a WikiDom into JSON.
*
* @class
* @constructor
* @extends {es.Serializer}
* @property options {Object} List of options for serialization
* @property options.indentWith {String} Text to use as indentation, such as \t or 4 spaces
* @property options.joinWith {String} Text to use as line joiner, such as \n or '' (empty string)
*/
es.JsonSerializer = function( options ) {
es.Serializer.call( this );
this.options = $.extend( {
'indentWith': '\t',
'joinWith': '\n'
}, options || {} );
};
/* Static Methods */
es.JsonSerializer.typeOf = function( value ) {
if ( typeof value === 'object' ) {
if ( value === null ) {
return 'null';
}
switch ( value.constructor ) {
case [].constructor:
return 'array';
case ( new Date() ).constructor:
return 'date';
case ( new RegExp() ).constructor:
return 'regex';
default:
return 'object';
}
}
return typeof value;
};
es.JsonSerializer.prototype.encode = function( data, indention ) {
if ( indention === undefined ) {
indention = '';
}
var type = es.JsonSerializer.typeOf( data ),
key;
// Open object/array
var json = '';
if ( type === 'array' ) {
if (data.length === 0) {
// Empty array
return '[]';
}
json += '[';
} else {
var empty = true;
for ( key in data ) {
if ( data.hasOwnProperty( key ) ) {
empty = false;
break;
}
}
if ( empty ) {
return '{}';
}
json += '{';
}
// Iterate over items
var comma = false;
for ( key in data ) {
if ( data.hasOwnProperty( key ) ) {
json += ( comma ? ',' : '' ) + this.options.joinWith + indention +
this.options.indentWith + ( type === 'array' ? '' : '"' + key + '"' + ': ' );
switch ( es.JsonSerializer.typeOf( data[key] ) ) {
case 'array':
case 'object':
json += this.encode( data[key], indention + this.options.indentWith );
break;
case 'boolean':
case 'number':
json += data[key].toString();
break;
case 'null':
json += 'null';
break;
case 'string':
json += '"' + data[key].replace(/[\n]/g, '\\n').replace(/[\t]/g, '\\t') + '"';
break;
// Skip other types
}
comma = true;
}
}
// Close object/array
json += this.options.joinWith + indention + ( type === 'array' ? ']' : '}' );
return json;
};