Home Manual Reference Source Test Repository

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.