srlz
A small serialization library that can be adapted to your own needs.
API
GenericSerializer
A generic serializer which returns the input value and is the base for every serializer implementation.
ObjectSerializer
A serializer which serializes / deserializes values based on the configures fields and corresponding serializers.
In most cases the class has to be extends by a more specific serializer class which takes care about a specific object type.
fields
Instance member
Type: Object
Each key of the objects maps to a field name and each value to a field specification.
Field specification
serializer
(optional)
Type:Class extends GenericSerializer
Defines the serializer class which should be used for the field.required
(optional)
Type:boolean
Throws an error if the field is missing during deserialization.
Usage
Custom serializer
Creates serializer which convers the given array to a string by using a configured seperator.
;// ORconst GenericSerializer = ; separator = '||'; { return value; } { return value; } const serializer = ;serializer;// Output: '1||2||3||4||5'
Custom object serializer
Converts a person object into an object in which the skills are represented as string.
;// ORconst ObjectSerializer = ; fields = name: {} skills: serializer: JoinedArraySerializer const serializer = ;serializer;// Output: {// name: 'John Doe',// skills: 'Javascript||Typescript'// }
Complex example
This example shows how to create custom serializers and how a serialization for the non scalar type Date
could look
like.
const GenericSerializer ObjectSerializer = ; const post = title: 'My Blog Title' publishedAt: '2020-08-01T09:30:00.000Z'; { return value; } { return value; } fields = title: {} publishedAt: serializer: ISODateSerializer const postSerializer = ;postSerializer;// Output: {// title: 'My Blog Title',// publishedAt: '2020-08-01T09:30:00.000Z'// }