@esentri/de-serializer
v0.9.9
Published
[![Build Status](https://travis-ci.org/esentri/js-de-serializer.svg?branch=master)](https://travis-ci.org/esentri/js-de-serializer) [![Coverage Status](https://coveralls.io/repos/github/esentri/js-de-serializer/badge.svg?branch=master)](https://coveralls.
Downloads
43
Keywords
Readme
De-Serializer
De-Serializer will support you in various serializing and deserializing tasks in JavaScript.
Currently, the following features are implemented:
- serialize JS object to data structure
- deserialize data structure to JS object
- serialize JS object to string
- deserialize string to JS object
- serialize JS object to ArrayBuffer
- deserialize ArrayBuffer to JS object
The basic idea is that you can truly deserialize data to a fully working object. If you use
JSON.stringify
/ JSON.parse
for example you only get a data structure (i.e. an object with
fields only - no functions / etc.). Or you have to provide for every object a custom receiver
function inside JSON.parse
.
De-Serializer provides common functionality for de/serializing basic objects. Furthermore, see section Custom de/serialization if you need to adapt the logic for specific objects.
Install
npm install @esentri/de-serializer
Usage
JS object to data structure
Functional:
import {SimpleSerialize} from '@esentri/de-serializer'
let myObject = new MyObject()
SimpleSerialize(myObject).then(serialized => {
// do stuff
})
- myObject is the object you want to serialize
JS object to string
Functional:
import {SimpleSerialize, SerializedType} from '@esentri/de-serializer'
let myObject = new MyObject()
SimpleSerialize(myObject, SerializedType.STRING).then(serialized => {
// do stuff
})
- myObject is the object you want to serialize
Data structure to JS object
Functional:
import {SimpleDeserialize} from '@esentri/de-serializer'
let myDataStructure = { field1: 'test' }
SimpleDeserialize(myDataStructure, MyObject).then(deserialized => {
// do stuff
})
- myDataStructure is the data structure for deserializing
- MyObject is the class you want to create
Object oriented:
import {Deserializer} from '@esentri/de-serializer'
let myDeserializer = Deserializer.simple(MyObject)
myDeserializer.deserialize(myDataStructure).then(deserialized => {
// do stuff
})
- myDataStructure is the data structure for deserializing
- MyObject is the class you want to create
String to JS object
Functional:
import {SimpleDeserialize, SerializedType} from '@esentri/de-serializer'
let myString = "{ field1: 'test' }"
SimpleDeserialize(myString, MyObject, SerializedType.STRING).then(deserialized => {
// do stuff
})
- myString is the string for deserializing
- MyObject is the class you want to create
Object oriented:
import {Deserializer, SerializedType} from '@esentri/de-serializer'
let myDeserializer = Deserializer.simple(MyObject, SerializedType.STRING)
myDeserializer.deserialize(myString).then(deserialized => {
// do stuff
})
- myString is the data structure for deserializing
- MyObject is the class you want to create
Custom de/serializing
Custom serialization
- add the method serialize() to your class
- in TypeScript you can (but don't need to) implement the interface Serializable
- this method needs to return a data structure
Keep in mind that you need to do the serializations for the fields as well (or use the built-in SimpleSerialize).
E.g.:
class MyClass {
constructor(fieldA) {
this.fieldA = fieldA
}
serialize() {
return new Promise(resolve =>
resolve({ fieldA: 'overwritten during serialization' })
)
}
}
let myObject = new MyClass('Hello world')
SimpleSerialize(myObject).then(serialized => {
// do stuff
})
- the data structure will look like this
{ fieldA: 'overwritten during serialization' }
Custom deserialization
- add a static method deserialize(dataStructure) to your class
- implement all the logic inside and return a new object
E.g.:
class MyClass {
constructor(fieldA) {
this.fieldA = fieldA
}
static deserialize(dataStructure) {
return new Promise(resolve => {
resolve(new MyClass(dataStructure.fieldA + ' some stuff'))
})
}
}
let myDataStructure = { fieldA: 'hello' }
SimpleDeserialize(myDataStructure, MyClass).then(deserialized => {
// do stuff
})
- myObject will look like this:
{ fieldA: 'hello some stuff' }
Algorithm
SimpleSerialize
- check if object has a serialize() method
- true: call serialize() on object and return the result
- false: continue to next step
- check if object is a primitive
- true: return primitive
- false: continue to next step
- create an empty data structure
- iterate over all properties that are not functions
- for each property create a property in the new data structure and assign the value you get by repeating this algorithm for the property
SimpleDeserialize
- check if the Class has a static deserialize(dataStructure) method
- true: call deserialize and return the result
- false: continue to next step
- instantiate a new object of the wanted class
- iterate over each property in dataStructure
- for each property
- check if property is primitive
- true: assign primitive as the property to new created object
- false: call this algorithm on the property and assign the value to the new created object
- check if property is primitive
- return new created object
Limitations
Currently, the methods will be serialized for nested objects as well inside an attribute called __functions__. If you send a serialized objects across the wire you need to keep in mind to skip this field if you deserialize the object with another library / language.
Projects used
- Typescript Library Starter
- License: MIT
- as a template for the setup
- JEST
- License: MIT
- Colors
- License: MIT
- Commitizen
- License: MIT
- Definitley Typed
- License: MIT
- Coveralls
- License: BSD-2-Clause
- Cross-env
- License: MIT
- cz-conventional-changelog
- License: MIT
- Husky
- License: MIT
- lint-staged
- License: MIT
- lodash.camelcase
- License: MIT
- Prompt
- License: MIT
- replace-in-file
- License: MIT
- rimraf
- License: ISC
- rollup
- License: MIT
- semantic-release
- License: MIT
- tslint
- License: Apache-2.0
- typedoc
- License: Apache-2.0
- typescript
- License: Apache-2.0
- validate-commit-msg
- License: MIT
License
MIT License
Copyright (c) 2018 esentri
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.