0.2.0 • Published 6 years ago

serializr-fp v0.2.0

Weekly downloads
6
License
GPL-3.0
Repository
github
Last release
6 years ago

serializr-fp

npm Codecov Travis David

(De)serialize complex object graphs to/from JSON

A FP (Functional Programming) alternative to serializr, with a simpler approach for ease of use and better customization, configure (de)serialization through schema's, improving readability and maintainability.

More info about the internals and principles →

Install

npm install serializr-fp --save

Usage

import {alias, computed, DATE_ONLY, deserialize, object, PRIMITIVE, serialize} from 'serializr-fp';

const PATIENT_SCHEMA = object({
    id: alias('_id', PRIMITIVE),
    person: object({
        firstName: PRIMITIVE, 
        lastName: PRIMITIVE,
        fullName: computed(json => `${json.firstName} ${json.lastName}`),
    }),
    dateOfBirth: alias('dob', DATE_ONLY),
});

const source = {
    id: '123',
    person: {
        firstName: 'Dieter', 
        lastName: 'Geerts', 
        fullName: 'Dieter Geerts',
    },
    dateOfBirth: new Date('1982-10-08'),
}; 

const json = PATIENT_SCHEMA.serialize(source);
const patient = PATIENT_SCHEMA.deserialize(json);

console.log(json); 
console.log(patient);

// Or you can export functions for other modules to use,
// without the need to know how (de)serialization is done.

export const deserializePatient = PATIENT_SCHEMA.deserialize;
export const serializePatient = PATIENT_SCHEMA.serialize;

More useful examples →

API

Read the API →

Maintainers

dietergeerts
dietergeerts

Contribute

Codacy grade Code Climate David

All contributions welcome

Contributors

dietergeerts

License

GNU General Public License v3.0

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.2

6 years ago