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 🙏

© 2026 – Pkg Stats / Ryan Hefner

serializerjs

v1.0.0-beta.1

Published

JavaScript Serializer

Readme

Serializer.js

Build Status bitHound Overall Score Code Climate Codacy Badge

npm version nodejs version npm License

Sauce Test Status

A serializer is responsible for transforming a data in a specific format into a data in another format/schema.

For example, the serializer can be used to map client-side models and back-end side objects by checking all the properties of a model to generate an object for which the values are understandable for the back-end service.

 +--------+ ----- serialize -----> +----------+
 | Client |                        | Back-End |
 +--------+ <---- deserialize ---- +----------+

Note that JavaScript does not support interfaces per se. By "Interface" is meant that this class should not implement any method but provide the skeleton for child classes instead.

Table of Content

  1. Installation
  2. Usage
  3. Advanced documentation
    1. Reusable serializers: SerializerAware
    2. Dynamic (de-serialization): formats & contexts
    3. Frameworks integration
  4. Resources
  5. Credits
  6. License

Installation

npm install --save serializerjs

ES6 (ES2015)

import SerializerFactory from 'serializerjs';

// access to the other elements of the package:
import SerializerInterface from 'serializerjs'.SerializerInterface;
// or
const SerializerInterface = SerializerFactory.SerializerInterface;
// or
import SerializerInterface from 'serializerjs/lib/Serializer/SerializerInterface';

Nodejs/ES5

var SerializerFactory = require('serializerjs');

// access to the other elements of the package:
var SerializerInterface = SerializerFactory.SerializerInterface;
// or
var SerializerInterface = require('serializerjs/lib/Serializer/SerializerInterface');

Usage

The two key classes of this library are Serializer and SerializerInterface. To use it, you just have to write your own serializers (simple class implementing SerializerInterface). Once done, create a Serializer instance serializer to aggregate all your serializers. You will then be able to use serializer to (de-)serialize any kind of data supported by the registered serializers.

Example: For some reason, we are fetching data where instead of having booleans true and false in our JSON response, we have integers 0 or 1. Let us write a simple BooleanSerializer that will be responsible for deserializing them into proper booleans.

// app/Serializer/BooleanSerializer.js
import SerializerFactory from 'serializerjs';

export default class BooleanSerializer extends SerializerFactory.SerializerInterface {
    /**
     * @inheritDoc
     */
    supportsSerialize(data, format = null) {
        return false;
    }

    /**
     * @inheritDoc
     *
     * @param {number|boolean|null} data
     *
     * @return {boolean}
     */
    deserialize(data, className, format = null, context = null) {
        if ('boolean' === typeof data || null === data) {
            return data;
        }

        return Boolean(data);
    }

    /**
     * @inheritDoc
     */
    supportsDeserialize(data, className, format = null) {
        if ('boolean' !== className) {
            return false;
        }

        if ('boolean' === typeof data || null === data) {
            return true;
        }

        if (0 === data || 1 === data) {
            return true;
        }

        return false;
    }
}

Then create the serializer service with all the serializers you which to register. In our case we only have BooleanSerializer:

// app/Serializer/Serializer.js
import SerializerFactory from 'serializerjs';
import BooleanSerializer from './BooleanSerializer';

export default SerializerFactory(new Map([
    ['BooleanSerializer', booleanSerializer],
]));

You can then use it like so:

// app/index.js
import serializer from './Serializer/Serializer';

serializer.supportsDeserialize(1, 'boolean')); // => true
serializer.deserialize(1, 'boolean'); // => true

serializer.supportsSerialize(true); // => true
serializer.serialize(true); // => true

Back to the Table of Content

Resources

Credits

Project made by Théo FIDRY and sponsored by HAIRCVT.

License

Copyright © 2015 HAIRCVT Licensed under the MIT license.