9.0.2 • Published 2 months ago

@badcafe/jsonizer v9.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Jsonizer

Structural reviving for JSON

Easy nested instance reviving for JSON

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

@badcafe/jsonizer is written in Typescript but can also be used just in JS.

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

Dates in personJson will appear as text (e.g. "birthDate":"1998-10-21T00:00:00.000Z"), and if you parse back that JSON string to a plain object, every date field will be string instead of Date !

Now, let's use Jsonizer 😍

// we are using Typescript in this example
// but without the type it works the same in pure JS 
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).

See it in action : Open in StackBlitz

Full documentation and API available HERE

9.0.2

2 months ago

9.0.1

3 months ago

9.0.0

3 months ago

8.1.3

4 months ago

8.1.2

8 months ago

8.1.0

11 months ago

8.1.1

11 months ago

8.0.4

1 year ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

1 year ago

8.0.2

2 years ago

7.0.6

2 years ago

7.0.5

2 years ago

7.0.4

2 years ago

7.0.3

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

6.0.4

2 years ago

7.0.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

4.0.0

2 years ago

5.0.0

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

2.0.0

2 years ago

1.0.0

3 years ago