2019-02-14 22:16:34 +00:00
|
|
|
import * as plugins from './smartjson.plugins';
|
|
|
|
|
2020-10-05 11:22:16 +00:00
|
|
|
/**
|
|
|
|
* allows you to parse a json
|
|
|
|
*/
|
|
|
|
export const parse = plugins.bufferJson.parse;
|
2019-02-14 22:16:34 +00:00
|
|
|
|
2020-10-05 11:22:16 +00:00
|
|
|
/**
|
2020-10-05 11:26:06 +00:00
|
|
|
*
|
2020-10-05 11:22:16 +00:00
|
|
|
* @param objArg
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2020-10-05 16:20:27 +00:00
|
|
|
export const stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options'] = {}): string => {
|
2020-10-05 11:22:16 +00:00
|
|
|
const bufferedJson = plugins.bufferJson.stringify(objArg);
|
|
|
|
objArg = JSON.parse(bufferedJson);
|
|
|
|
return plugins.stableJson(objArg, optionsArg);
|
|
|
|
};
|
|
|
|
|
|
|
|
export class Smartjson {
|
2020-10-05 11:13:17 +00:00
|
|
|
/**
|
|
|
|
* enfolds data from an object
|
|
|
|
*/
|
|
|
|
public static enfoldFromObject(objectArg) {
|
|
|
|
const newInstance = new this();
|
|
|
|
for (const keyName in objectArg) {
|
|
|
|
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
|
|
|
newInstance[keyName] = objectArg[keyName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newInstance;
|
|
|
|
}
|
2019-02-14 22:16:34 +00:00
|
|
|
|
2020-10-05 11:22:16 +00:00
|
|
|
/**
|
|
|
|
* enfold from json
|
|
|
|
*/
|
|
|
|
public static enfoldFromJson(jsonArg: string) {
|
|
|
|
const objectFromJson = parse(jsonArg);
|
|
|
|
return this.enfoldFromObject(objectFromJson);
|
|
|
|
}
|
|
|
|
|
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 } = {};
|
2020-10-05 15:28:29 +00:00
|
|
|
const trackMap = [];
|
2019-08-09 08:55:17 +00:00
|
|
|
for (const keyName of this.saveableProperties) {
|
2020-10-05 15:28:29 +00:00
|
|
|
let value = this[keyName];
|
|
|
|
if (value instanceof Smartjson) {
|
|
|
|
if (trackMap.includes(value)) {
|
|
|
|
throw new Error('cycle detected');
|
|
|
|
}
|
|
|
|
trackMap.push(value);
|
|
|
|
value = value.foldToObject();
|
|
|
|
}
|
|
|
|
newFoldedObject[keyName] = plugins.lodashCloneDeep(value);
|
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
|
|
|
}
|
2020-10-05 11:22:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* folds a class into an object
|
|
|
|
*/
|
|
|
|
public foldToJson() {
|
|
|
|
const foldedObject = this.foldToObject();
|
|
|
|
return stringify(foldedObject, {});
|
|
|
|
}
|
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);
|
|
|
|
};
|
|
|
|
};
|