@open-kappa/myjson
Version:
A simple JSON management library.
96 lines (95 loc) • 3.27 kB
TypeScript
import { MyJson, MyJsonBaseObject } from "./myjsonImpl";
/**
* @brief A special kind of JSON object.
* It has some mandatory fields, and other optionals, as long as they respect a
* validator.
* It is inspired by C "flex" structs, i.e. structs with some fields ending with
* an array:
* ```c
* struct FlexExample
* {
* int a;
* double b;
* unsigned flex[0];
* };
* ```
*/
declare class MyJsonFlex<Element extends MyJson> extends MyJsonBaseObject {
/**
* @brief Constructor.
* @param {Element} element The validator for flex elements. If it is
* mandatory, then the list cannot be empty.
* @param {boolean} isMandatory True if the object must appear in the
* hierarchy.
* @param {string} name The name of the object.
*/
constructor(element: Element, isMandatory: boolean, name: string);
/**
* @brief Return the internal validator element of the flex object.
* It uses a symbol to hide the properties to "usual" javascript methods.
* @private
* @return {Element} The validator.
*/
private _getValidatorElement;
/**
* @brief Parse a JSON object.
* @param {any} json The JSON to parse.
* @throws {Error} If a validation error occurrs.
*/
protected parseJsonImpl(json: any): void;
/**
* @brief Ensure that mandatory properties exists.
* @param {any} json The json to validate.
* @throws {Error} If the check fails.
*/
private _ensureMandatory;
/**
* @brief Do actual parsing.
* @param {any} json The json to parse.
* @throws {Error} If the parsing fails.
*/
private _parseJson;
private _parseRecurse;
/**
* @brief Clear the object content.
*/
protected clearImpl(): void;
/**
* @brief Clone this object.
* @return {MyJsonFlex<Element>} The copy.
*/
clone(): MyJsonFlex<Element>;
/**
* @brief Check if two JSON objects are equals.
* @param {MyJson} other The other object of comparison.
* @return {boolean} True if they are equals.
*/
isEqual(other: MyJson): boolean;
/**
* @brief Execute the given callback on each element.
* 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:MyJsonFlex<Element>)=>boolean} func
* The callback.
* @param {any | null | undefined} [funcThis=null] The optional "this" for
* the callback.
*/
forEach(func: (key: string, value: MyJson, obj: MyJsonFlex<Element>) => boolean, funcThis?: any | null | undefined): boolean;
/**
* @brief Merge the two objects.
* Already existent keys are merged recursively.
* @param {MyJson} other The other object to merge.
* @throws {Error} If the two objects cannot be merged.
*/
merge(other: MyJson): void;
/**
* @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: MyJson): MyJsonFlex<Element>;
}
export { MyJsonFlex };
export default MyJsonFlex;