2019-02-14 22:16:34 +00:00
|
|
|
import * as plugins from './smartjson.plugins';
|
|
|
|
|
|
|
|
export class Smartjson {
|
|
|
|
// ======
|
|
|
|
// STATIC
|
|
|
|
// ======
|
2019-08-09 08:55:17 +00:00
|
|
|
public static parse = JSON.parse;
|
2019-02-14 22:16:34 +00:00
|
|
|
|
2019-12-15 19:05:20 +00:00
|
|
|
public static stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options']) => {
|
2019-07-04 15:04:24 +00:00
|
|
|
return plugins.stableJson(objArg, optionsArg);
|
2019-08-25 14:03:34 +00:00
|
|
|
}
|
2019-02-14 22:16:34 +00:00
|
|
|
|
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2017-02-27 13:05:03 +00:00
|
|
|
|
2019-08-09 08:55:17 +00:00
|
|
|
public saveableProperties: string[];
|
2017-02-27 13:05:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* folds a class into an object
|
|
|
|
*/
|
2019-08-09 08:55:17 +00:00
|
|
|
public foldToObject() {
|
|
|
|
const newFoldedObject: { [key: string]: any } = {};
|
|
|
|
for (const keyName of this.saveableProperties) {
|
2019-07-04 15:03:15 +00:00
|
|
|
newFoldedObject[keyName] = plugins.lodashCloneDeep(this[keyName]);
|
2017-02-27 13:05:03 +00:00
|
|
|
}
|
2018-07-23 22:38:54 +00:00
|
|
|
return newFoldedObject;
|
2017-02-27 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enfolds data from an object
|
|
|
|
*/
|
2019-08-09 08:55:17 +00:00
|
|
|
public enfoldFromObject(objectArg) {
|
|
|
|
for (const keyName in objectArg) {
|
2017-02-27 13:05:03 +00:00
|
|
|
if (this.saveableProperties.indexOf(keyName) !== -1) {
|
2018-07-23 22:38:54 +00:00
|
|
|
this[keyName] = objectArg[keyName];
|
2017-02-27 13:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator that marks a property as foldable
|
|
|
|
*/
|
|
|
|
export let foldDec = () => {
|
|
|
|
return (target: any, key: string) => {
|
2018-07-23 22:38:54 +00:00
|
|
|
if (!target.saveableProperties) {
|
|
|
|
target.saveableProperties = [];
|
|
|
|
}
|
|
|
|
target.saveableProperties.push(key);
|
|
|
|
};
|
|
|
|
};
|