Files
fad-dashboard/frontend/packages/js/utils/jsonSerialize.js
2025-11-08 10:38:36 +08:00

24 lines
826 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 自定义序列化方法解决JSON.stringify方法忽略函数属性的问题
export function customSerialize(obj) {
// 将对象属性和函数转换为字符串形式
const serializedObj = JSON.stringify(obj, function (key, value) {
if (typeof value === "function") {
return value.toString(); // 将函数转换为字符串
}
return value; // 保持其他属性不变
});
return serializedObj;
}
// 自定义反序列化方法
export function customDeserialize(serializedObj) {
const parsedObject = JSON.parse(serializedObj, function (key, value) {
if (typeof value === "string" && value.indexOf("function") === 0) {
// 将字符串还原为函数
return new Function("return " + value)();
}
return value; // 保持其他属性不变
});
return parsedObject;
}