bowling-analysis-system
Version:
A comprehensive system for analyzing bowling techniques using video processing and metrics calculation
78 lines (71 loc) • 1.82 kB
JavaScript
/**
* @module JsonTransformer
* @description Transforms JSON data
*/
/**
* @class JsonTransformer
* @description Transforms JSON data
*/
class JsonTransformer {
/**
* Create a new JSON transformer
* @param {Object} options - Transformer options
*/
constructor(options = {}) {
this.options = {
operation: 'parse', // 'parse' or 'stringify'
...options
};
}
/**
* Process the input data
* @param {string|Object} data - Input data
* @returns {Promise<Object|string>} Transformed data
*/
async process(data) {
if (this.options.operation === 'parse') {
return this._parseJson(data);
} else if (this.options.operation === 'stringify') {
return this._stringifyJson(data);
} else {
throw new Error(`Unknown operation: ${this.options.operation}`);
}
}
/**
* Parse JSON string to object
* @param {string} data - JSON string
* @returns {Object} Parsed object
* @private
*/
_parseJson(data) {
try {
if (typeof data !== 'string') {
return data; // Already parsed
}
return JSON.parse(data);
} catch (error) {
console.error(`Error parsing JSON: ${error.message}`);
throw error;
}
}
/**
* Stringify object to JSON string
* @param {Object} data - Object to stringify
* @returns {string} JSON string
* @private
*/
_stringifyJson(data) {
try {
if (typeof data === 'string') {
return data; // Already stringified
}
const space = this.options.space || 2;
const replacer = this.options.replacer || null;
return JSON.stringify(data, replacer, space);
} catch (error) {
console.error(`Error stringifying JSON: ${error.message}`);
throw error;
}
}
}
module.exports = JsonTransformer;