XtraJson
XtraJson offers a flexible solution to transform objects into JSON-serializable representations and back. Unlike other solutions, such as superjson, XtraJson efficiently handles transformations without unnecessarily duplicating objects.
Features
- Out-of-the-box support for
undefined
, Buffer
, Date
, BigInt
. - Ability to handle any other type using custom transformers.
- Faster and more efficient than superjson.
Why XtraJson?
While libraries like SuperJSON create a copy of any object, even if it doesn't need to be transformed, XtraJson ensures optimized and efficient transformations.
Usage
Basic Setup
import XtraJson from '@erboladaiorg/earum-sequi';
const xjs = new XtraJson();
Registering Custom Transformers
xjs.register({
name: "my custom transformer",
key: "c",
isApplicable(value: unknown): value is MyClass {
},
serialize(value) {
},
deserialize(value) {
}
});
Serialization & Deserialization
const data = xjs.serialize(payload);
const json = xjs.stringify(payload);
const jsonAlt = JSON.stringify(payload, xjs.replacer);
const resultAlt = JSON.parse(payload, xjs.reviver);
const result = xjs.deserialize(data);
const resultStr = xjs.parse(json);