@badcafe/jsonizer

Easy nested instance reviving for JSON

Usage no npm install needed!

<script type="module">
  import badcafeJsonizer from 'https://cdn.skypack.dev/@badcafe/jsonizer';
</script>

README

Jsonizer

Easy nested instance reviving for JSON

@badcafe/jsonizer is a Typescript library that takes care of instances of classes in the hierarchy of your data structure when you use JSON.stringify() and JSON.parse().

Overview

Full documentation and API available HERE

Let's consider some data :

const person = {
    name: 'Bob',
    birthDate: new Date('1998-10-21'),
    hobbies: [
        {   hobby: 'programming',
            startDate: new Date('2021-01-01'),
        },
        {   hobby: 'cooking',
            startDate: new Date('2020-12-31'),
        },
    ]
}
const personJson = JSON.stringify(person);
// store or send the data

Now, let's use Jsonizer 😍

const personReviver = Jsonizer.reviver<typeof person>({
    birthDate: Date,
    hobbies: {
        '*': {
            startDate: Date
        }
    }
});
const personFromJson = JSON.parse(personJson, personReviver);

Every dates string in the JSON text have been mapped to Date objects in the parsed result.

Jsonizer can indifferently revive JSON data structures (arrays, objects) or class instances with recursively nested custom classes, third-party classes, built-in classes, or sub JSON structures (arrays, objects).

Full documentation and API available HERE