@rxap/xml-parser
Version:
Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into
32 lines • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultToJson = defaultToJson;
const reflect_metadata_1 = require("@rxap/reflect-metadata");
const metadata_keys_1 = require("./decorators/metadata-keys");
function defaultToJson() {
var _a, _b, _c, _d;
const json = {
__tag: this.__tag,
__parent: (_a = this.__parent) === null || _a === void 0 ? void 0 : _a.__tag,
__xmlns: Array.from((_c = (_b = this.__xmlns) === null || _b === void 0 ? void 0 : _b.entries()) !== null && _c !== void 0 ? _c : []).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: value })), {}),
};
const parserInstances = (_d = (0, reflect_metadata_1.getMetadata)(metadata_keys_1.ElementParserMetaData.PARSER_INSTANCE, this)) !== null && _d !== void 0 ? _d : [];
const toJson = (value) => {
if (value && typeof value === 'object') {
if (Array.isArray(value)) {
return value.map((v) => toJson(v));
}
else if ('toJSON' in value) {
return value.toJSON();
}
}
return value;
};
for (const parser of parserInstances) {
const propertyKey = parser.propertyKey;
const value = this[propertyKey];
json[propertyKey] = toJson(value);
}
return json;
}
//# sourceMappingURL=default-to-json.js.map