UNPKG

jsoneo

Version:

A powerful JSON enhancement library that supports all JSON primitives, Date, RegExp, Symbol, Functions, Map, Set, TypedArray and much more! Almost everything in JavaScript.

60 lines (59 loc) 3.22 kB
import type { ParseOptions, StringifyOptions } from './types'; /** * Notes: * * 1. Do not use 'closure' in the function, the function body can be serialized but the closure * reference can't. If it's a class, set the closures to the class properties, and use 'this' to * access them. If it's a normal function, set the closures to the function reference, and use * the function name to access them. * 2. Do not use anonymous Symbols in both object keys and values. Use system predefined Symbols * instead or use `Symbol.for` to create a Symbol. The anonymous Symbols can't be serialized and * event cannot be created. * 3. No direct or indirect circular references are allowed. * 4. For classes, should avoid private properties and methods, because they are not accessible from * outside and can't be serialized. Please use a normal property or method instead, and starts * with `_` to indicate it's a private member. If you are using TypeScript, you can use the * `private` keyword to declare as private, which looks a little better. * 5. Class constructors will be dropped. * 6. All native methods will be dropped, as `toString` method just returns `[native code]`. * 7. Do not use `function.bind`, because the bound functions become native methods. * 8. Respect `toJSON` and `fromJSON` method, if the object has a `toJSON` method, it will be called to * get the serialized value. If the object has a `fromJSON` method, it will be called with * serialized json to restore the original value. * 9. Make sure you trust the source of the serialized string, because the deserialization need to * evaluate script codes. A carefully crafted strings may embed malicious code, thus posing a * security threat. * 10. Buffer is supported in Node.js environment, and will be converted to Uint8Array in web browsers. */ /** * Advantages: * * 1. Supports serialization of complex JavaScript objects, including functions and prototypes. * 2. Supports serialization of Map, Set, ArrayBuffer, DataView, and other complex types. * 3. Supports serialization of circular references. * 4. Supports serialization of Symbol keys and values. * 5. Supports serialization of custom property descriptors. * 6. Supports serialization of non-enumerable properties. * 7. Supports toJSON and fromJSON methods for custom serialization and deserialization. * 8. Supports raw JSON objects (via JSON.rawJSON() method). */ /** * Serialize JavaScript object to string, support functions. Should including all fields of both * object and prototype. * * @param {any} value - The value to serialize. * * @returns The serialized string. */ export declare function stringify(value: any, options?: StringifyOptions): string; /** * Deserialize JavaScript object from string, support functions. Should including all fields of both * object and prototype. * * @param {string} input - The string to deserialize. * @param {object} options - The options to deserialize. * @param {function} [options.closure] - The closure to use when deserializing the object. * * @returns The deserialized object. */ export declare function parse(input: string, options?: ParseOptions): any;