tsbase
Version:
Base class libraries for TypeScript
140 lines • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonSerializer = void 0;
var tslib_1 = require("tslib");
var Strings_1 = require("../../System/Strings");
/**
* Keys to check for simple values when data is structured as shown here:
* example: field: [ { value: '1' } ]
*/
var keysToCheck = [
'value',
'target_id'
];
/**
* Deserializes raw json data into an instance of T
* **Some conventions are necessary - see wiki for full details**
* - No ***required*** constructor params
* - Init all property values
* - Array<T> / List<T> values must be initialized with a single instance inside
*/
var JsonSerializer = /** @class */ (function () {
function JsonSerializer() {
}
JsonSerializer.prototype.Deserialize = function (t, data) {
var _this = this;
var object = new t();
var classProperties = Object.keys(object);
var jsonKeys = Object.keys(data);
jsonKeys.forEach(function (element) {
var instanceKey = _this.getInstanceKey(classProperties, element);
var jsonElement = data[element];
if (instanceKey !== Strings_1.Strings.Empty && jsonElement) {
var property = object[instanceKey];
if (_this.propertyIsSimple(property)) {
_this.serializeSimpleField(jsonElement, object, instanceKey);
}
else if (_this.propertyIsArrayOfObjects(property)) {
var values = _this.getArrayValuesFromSerializer(property[0], jsonElement);
object[instanceKey] = values;
}
else {
var json = Array.isArray(jsonElement) ? jsonElement[0] : jsonElement;
object[instanceKey] = _this.getValueFromSerializer(property, json);
}
}
});
return object;
};
JsonSerializer.prototype.getInstanceKey = function (fields, jsonKey) {
var _this = this;
jsonKey = this.cleanString(jsonKey);
var instanceKey = Strings_1.Strings.Empty;
fields.forEach(function (element) {
if (_this.cleanString(element) === jsonKey) {
instanceKey = element;
}
});
return instanceKey;
};
JsonSerializer.prototype.cleanString = function (stringToClean) {
return stringToClean.replace('field_', Strings_1.Strings.Empty)
.replace(/[^a-zA-Z0-9]/g, Strings_1.Strings.Empty)
.trim()
.toLowerCase();
};
JsonSerializer.prototype.propertyIsSimple = function (property) {
return Array.isArray(property) && typeof property[0] !== 'object' || typeof property !== 'object';
};
JsonSerializer.prototype.propertyIsArrayOfObjects = function (property) {
return Array.isArray(property) && typeof property[0] === 'object';
};
JsonSerializer.prototype.getValueFromSerializer = function (property, json) {
var newSerializer = new JsonSerializer();
try {
return newSerializer.Deserialize(property.constructor, json);
}
catch (error) {
return new property.constructor();
}
};
JsonSerializer.prototype.getArrayValuesFromSerializer = function (property, json) {
var e_1, _a;
var values = [];
if (property) {
try {
for (var json_1 = tslib_1.__values(json), json_1_1 = json_1.next(); !json_1_1.done; json_1_1 = json_1.next()) {
var element = json_1_1.value;
values.push(this.getValueFromSerializer(property, element));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (json_1_1 && !json_1_1.done && (_a = json_1.return)) _a.call(json_1);
}
finally { if (e_1) throw e_1.error; }
}
}
return values;
};
JsonSerializer.prototype.serializeSimpleField = function (jsonElement, object, instanceKey) {
if (!Array.isArray(jsonElement)) {
object[instanceKey] = jsonElement;
}
else {
if (jsonElement.length === 1) {
object[instanceKey] = typeof jsonElement[0] === 'object' ?
this.getSingleValue(jsonElement[0]) : [jsonElement[0]];
}
else if (jsonElement.length > 1) {
object[instanceKey] = this.getArrayValue(jsonElement);
}
}
};
JsonSerializer.prototype.getSingleValue = function (object) {
var value = null;
keysToCheck.forEach(function (element) {
if (object && object[element]) {
value = object[element];
}
});
return value;
};
JsonSerializer.prototype.getArrayValue = function (array) {
var _this = this;
var values = new Array();
array.forEach(function (element) {
if (typeof element !== 'object') {
values.push(element);
}
else {
values.push(_this.getSingleValue(element));
}
});
return values;
};
return JsonSerializer;
}());
exports.JsonSerializer = JsonSerializer;
//# sourceMappingURL=JsonSerializer.js.map