Giter VIP home page Giter VIP logo

xtrajson's Introduction

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 'xtrajson';

const xjs = new XtraJson();

Registering Custom Transformers

xjs.register({
    name: "my custom transformer",
    key: "c", // key should be unique and as short as possible
    isApplicable(value: unknown): value is MyClass {
        // Define when this transformer should be used
    },
    serialize(value) {
        // Convert your custom type to a JSON-serializable format
    },
    deserialize(value) {
        // Convert the JSON-serializable format back to your custom type
    }
});

Serialization & Deserialization

const data = xjs.serialize(payload); // Convert to a JSON-serializable object
const json = xjs.stringify(payload); // Convert to a JSON string

// Alternate methods using built-in JSON methods
const jsonAlt = JSON.stringify(payload, xjs.replacer); // Using XtraJson as a replacer
const resultAlt = JSON.parse(payload, xjs.reviver); // Using XtraJson as a reviver

const result = xjs.deserialize(data); // Convert back from a JSON-serializable object
const resultStr = xjs.parse(json); // Convert back from a JSON string

xtrajson's People

Contributors

cah4a avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.