RPG Maker MV/MZ Plugin Configuration Parser
RPG Maker MV/MZ plugin configuration parser (beta code).
JavaScript
/** Class that defines custom JSON parsing methods. */
class Parser {
/**
* @method constructor
* @description Create a custom JSON parser.
*/
constructor () {
/**
* @description Helper functions used to perform custom parsing on specific JSON keys.
* @type {object}
*/
this.helpers = { };
}
/**
* @method parse
* @description JSON parser that handles nested plugin structs and incorporates defaults.
* @param {string} json
* @param {object} defaults
* @return {object} Parsed configuration data (with default values applied).
*/
parse (json, defaults) {
/** I did not want to use 'self', but I need to be able to use 'this' within the
JSON reviver function to re-name keys. */
let self = this;
try {
return JSON.parse(json, function(key, value) {
/** Split into base key name, key type, and optional helper function name. */
let [_key, _type, _fnc] = key.split('__');
/** Get default value for key. */
let _def = defaults[_key];
/** Ensure value has a value. */
let _val = value === '' ? _def : value;
/** If both key and type, then the value will need to be parsed. */
if (_key && _type) {
switch (_type) {
/** Call defined helper function to parse and return value. */
case 'function':
_val = self.helpers[_fnc].call(this, _val);
break;
/** Deep parse simple or struct array. */
case 'array':
_val = value === '' ? [] : JSON.parse(_val);
for (let i = 0; i < _val.length; i++) {
_val[i] = self.parse(_val[i], _def[0]);
}
/** Avoid JSON error and do not parse strings. */
case 'string':
break;
/** Default parsing action for other types. */
default:
_val = self.parse(_val, _def);
break;
};
/** Re-name and...