npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deserializable

v0.3.3

Published

Utilities for automatically deserializing certain JavaScript objects and instances.

Downloads

22

Readme

deserializable

build status npm version npm downloads

Utilities for automatically deserializing certain JavaScript objects and instances.

Table of contents

  1. Installation
  2. Usage (basic)
  3. Usage (advanced)
  4. Supported types
  5. Example

Installation

npm install deserializable --save

Usage (basic)

Two main exports:

stringify (Mixed value, Optional Mixed space)

Uses JSON.stringify under the hood, but for types that will lose their original form when using JSON.parse on the result (e.g., ES6 Map ES6 Set, Date, RegExp, etc.), it returns an object resembling { __type__, __value__ } so that this package's parse export can return the proper value.

parse (String value)

Uses JSON.parse under the hood, but when an object is found containing a __type__ and __value__, it will attempt to recontruct the proper value using either global[__type__].fromJSON(__value__) (if the fromJSON static method exists) or new global[__type__](__value__).

Usage (advanced)

And other exports that may be useful to you if you have more complex requirements:

getType (Mixed value)

Gets the type of some value if it has a toJSON method and is not typically properly deserialized using JSON.parse.

fixedTypes

Simple object containing toJSON and fromJSON methods for each type. Used for providing enforcing specific methods for certain types. For example, the native Map doesn't have toJSON or fromJSON methods but we fix this by setting:

fixedTypes.Map = {
  toJSON: value => [ ...value ],
  fromJSON: value => new Map(value)
};

ignoredTypes

Simple object containing known deserializable types using JSON.parse. The getType function uses this internally.

reviver (String key, Mixed value)

The parse function uses this internally to return the proper value.

patchAll (Object obj, RegExp ignore)

Since JSON.stringify calls toJSON on values before the replacer function argument receives each value, we have to monkey-patch each prototype.toJSON to attempt to get a type using getType and then return the new value if necessary - i.e., { __type__, __value__ } - which will only occur if this package's stringify method is called, otherwise it will return toJSON's usual value. The patchAll function is immediately called on the window (or global), but you can use it to patch other constructors as values within the obj argument.

patch (String key, Function fn)

Patches the function.

Supported types

This should work for any constructor containing prototype.toJSON, and if you have some special requirements when reconstructing the value using parse, attach a fromJS static method to the constructor. The following types are tested:

Map

ES6 Map instance

Set

ES6 Set instance

Date

Date instance

RegExp

RegExp instance

Note: This package adds prototype.toJSON and fromJSON to the RegExp constructor. See index.js.

Example

See redux-replicate-localforage.