npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

Readme

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

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.