@esentri/de-serializer
    TypeScript icon, indicating that this package has built-in type declarations

    0.9.9 • Public • Published

    De-Serializer

    Build Status Coverage Status npm version

    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

    1. check if object has a serialize() method
      1. true: call serialize() on object and return the result
      2. false: continue to next step
    2. check if object is a primitive
      1. true: return primitive
      2. false: continue to next step
    3. create an empty data structure
    4. iterate over all properties that are not functions
    5. 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

    1. check if the Class has a static deserialize(dataStructure) method
      1. true: call deserialize and return the result
      2. false: continue to next step
    2. instantiate a new object of the wanted class
    3. iterate over each property in dataStructure
    4. for each property
      1. check if property is primitive
        1. true: assign primitive as the property to new created object
        2. false: call this algorithm on the property and assign the value to the new created object
    5. 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 dependencies

    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.

    Keywords

    none

    Install

    npm i @esentri/de-serializer

    DownloadsWeekly Downloads

    2

    Version

    0.9.9

    License

    MIT

    Unpacked Size

    75.7 kB

    Total Files

    32

    Last publish

    Collaborators

    • esentri-admin
    • maurice-mueller
    • yannick.meny