@open-kappa/myjson
Version:
A simple JSON management library.
258 lines • 7.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MyJsonBaseObject = void 0;
const myjsonImpl_1 = require("./myjsonImpl");
/**
* @brief Base class for classes mapping JSON objects.
*/
class MyJsonBaseObject extends myjsonImpl_1.MyJson {
/**
* @brief Constructor.
* @param {boolean} isMandatory True if the object must appear in the
* hierarchy.
* @param {string} name The name of the object.
*/
constructor(isMandatory, name) {
super(isMandatory, name);
}
/**
* @brief Common implementation of clone().
* @param {MyJsonBaseObject} other The copy.
*/
_cloneImpl(other) {
const self = this;
function insert(key, value) {
other.set(key, value.clone());
return false;
}
self._forEachImpl(insert);
}
/**
* @brief Common implementation of isEqual().
* @param {MyJson} other The other object of comparison.
* @return {boolean} True if they are equals.
*/
_isEqualImpl(other) {
const self = this;
if (self.getSize() !== other.getSize())
return false;
let ret = true;
function doCheck(key, value) {
const otherValue = other.get(key);
ret = value.isEqual(otherValue);
return !ret;
}
self._forEachImpl(doCheck);
return ret;
}
/**
* @brief Check whether the object is empty.
* @return {boolean} True if empty.
*/
isEmpty() {
return this.getSize() === 0;
}
/**
* @brief Get the number of stored elements.
* @return {number} The array size.
*/
getSize() {
return Object.keys(this).length;
}
/**
* @brief Get the element with the given key.
* @param {string} key The key.
* @return {MyJson} The internal element.
*/
get(key) {
const self = this;
const anySelf = self;
if (!self.has(key)) {
self._throwValidatorError("MyJsonBaseObject.get(): key not found: " + key);
}
return anySelf[key];
}
/**
* @brief Set the element with given key.
* @param {key} key The key.
* @param {MyJson} value The element.
*/
set(key, value) {
const self = this;
const anySelf = self;
value.setJsonName(key);
// @todo should check validity...
// @todo add a generic json object...
// if (self.has(key) && self.get(key).isMandatory())
// {
// const el = self.get(key);
// }
// else
// {
// const el = self._getValidatorElement();
// }
anySelf[key] = value;
}
/**
* @brief Check if the element exists.
* @param {string} key The key.
* @return {boolean} True if it exists.
*/
has(key) {
const self = this;
return key in self;
}
/**
* @brief Common implementation of forEach()..
* The callback takes the key, the element, and the whole object.
* It returns true to break the loop before having rolled on all elements.
* @param {(key:string,value:MyJson,obj:MyJsonBaseObject)=>boolean} func
* The callback.
* @param {any | null | undefined} [funcThis=null] The optional "this" for
* the callback.
*/
_forEachImpl(func, funcThis = null) {
const self = this;
const keys = Object.keys(self);
let ret = false;
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const value = self.get(key);
ret = func.apply(funcThis, [key, value, self]);
if (ret)
break;
}
return ret;
}
/**
* @brief Common implementation of merge().
* Already existent keys are merged recursively.
* @param {MyJsonBaseObject} other The other object to merge.
* @throws {Error} If the two objects cannot be merged.
*/
_mergeImpl(other) {
const self = this;
function doMerge(key, otherValue) {
if (self.has(key)) {
const value = self.get(key);
value.merge(otherValue);
}
else {
self.set(key, otherValue.clone());
}
return false;
}
other._forEachImpl(doMerge);
}
/**
* @brief Add a new child element.
* This is a build-time support method.
* @param {MyJson} element The child element.
* @return {MyJson} This.
* @throws {Error} If element cannot be added.
*/
add(element) {
const self = this;
self.set(element.getJsonName(), element);
return self;
}
/**
* @brief Get element as array.
* @param {string} key The key.
* @throws {Error} If element is not an array.
*/
getAsArray(key) {
const self = this;
const ret = self.get(key);
if (!(ret instanceof myjsonImpl_1.MyJsonArray)) {
self._throwValidatorError("Not a MyJsonArray");
}
// @todo add template check
return ret;
}
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* @brief Get element as fixed.
* @param {string} key The key.
* @throws {Error} If element is not a fixed.
*/
getAsFixed(key) {
/* eslint-enable @typescript-eslint/no-unused-vars */
const self = this;
const ret = self.get(key);
if (!(ret instanceof myjsonImpl_1.MyJsonFixed)) {
self._throwValidatorError("Not a MyJsonFixed");
}
// @todo add template check
return ret;
}
/**
* @brief Get element as flex.
* @param {string} key The key.
* @throws {Error} If element is not a flex.
*/
getAsFlex(key) {
const self = this;
const ret = self.get(key);
if (!(ret instanceof myjsonImpl_1.MyJsonFlex)) {
self._throwValidatorError("Not a MyJsonFlex");
}
// @todo add template check
return ret;
}
/**
* @brief Get element as map.
* @param {string} key The key.
* @throws {Error} If element is not a map.
*/
getAsMap(key) {
const self = this;
const ret = self.get(key);
if (!(ret instanceof myjsonImpl_1.MyJsonMap)) {
self._throwValidatorError("Not a MyJsonMap");
}
// @todo add template check
return ret;
}
/**
* @brief Get element as value.
* @param {string} key The key.
* @throws {Error} If element is not a value.
*/
getAsValue(key) {
const self = this;
const ret = self.get(key);
if (!(ret instanceof myjsonImpl_1.MyJsonValue)) {
self._throwValidatorError("Not a MyJsonValue");
}
// @todo add template check
return ret;
}
/**
* @brief Get element as value.
* @param {string} key The key.
* @throws {Error} If element is not a value.
*/
getAsStringValue(key) {
return this.getAsValue(key);
}
/**
* @brief Get element as value.
* @param {string} key The key.
* @throws {Error} If element is not a value.
*/
getAsNumberValue(key) {
return this.getAsValue(key);
}
/**
* @brief Get element as value.
* @param {string} key The key.
* @throws {Error} If element is not a value.
*/
getAsBooleanValue(key) {
return this.getAsValue(key);
}
}
exports.MyJsonBaseObject = MyJsonBaseObject;
exports.default = MyJsonBaseObject;
//# sourceMappingURL=MyJsonBaseObject.js.map