serializr-fp
v0.2.0
Published
(De)serialize complex object graphs to/from JSON
Downloads
14
Maintainers
Readme
serializr-fp
(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;
API
Maintainers
| :---: | dietergeerts |
Contribute
All contributions welcome
Contributors
| :---: | dietergeerts |