https://github.com/floriangosse/srlz
A small serialization library that can be adapted to your own needs.
https://github.com/floriangosse/srlz
convert deserialization javascript json serialization transform
Last synced: 5 months ago
JSON representation
A small serialization library that can be adapted to your own needs.
- Host: GitHub
- URL: https://github.com/floriangosse/srlz
- Owner: floriangosse
- License: mit
- Created: 2020-07-29T16:58:42.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-06-16T13:11:06.000Z (about 2 years ago)
- Last Synced: 2025-10-25T14:01:18.345Z (8 months ago)
- Topics: convert, deserialization, javascript, json, serialization, transform
- Language: JavaScript
- Homepage:
- Size: 139 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- Funding: .github/FUNDING.yml
- License: license
Awesome Lists containing this project
README
# 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.
```js
import { GenericSerializer } from 'srlz';
// OR
const { GenericSerializer } = require('srlz');
class JoinedArraySerializer extends GenericSerializer {
separator = '||';
serialize(value) {
return value.join(this.separator);
}
deserialize(value) {
return value.splut(this.separator);
}
}
const serializer = new JoinedArraySerializer();
serializer.serialize([ 1, 2, 3, 4, 5 ]);
// Output: '1||2||3||4||5'
```
### Custom object serializer
Converts a person object into an object in which the skills are represented as string.
```js
import { ObjectSerializer } from 'srlz';
// OR
const { ObjectSerializer } = require('srlz');
class PersonSerializer extends ObjectSerializer {
fields = {
name: {},
skills: { serializer: JoinedArraySerializer }
}
}
const serializer = new PersonSerializer();
serializer.serialize({
name: 'John Doe',
skills: [ 'Javascript', 'Typescript' ]
});
// 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.
```js
const { GenericSerializer, ObjectSerializer } = require('srlz');
const post = {
title: 'My Blog Title',
publishedAt: new Date('2020-08-01T09:30:00.000Z')
};
class ISODateSerializer extends GenericSerializer {
serialize(value) {
return value.toISOString();
}
deserialize(value) {
return new Date(value);
}
}
class PostSerializer extends ObjectSerializer {
fields = {
title: {},
publishedAt: { serializer: ISODateSerializer }
}
}
const postSerializer = new PostSerializer();
postSerializer.serialize(post);
// Output: {
// title: 'My Blog Title',
// publishedAt: '2020-08-01T09:30:00.000Z'
// }
```