0.5.2 • Published 3 years ago

jsonlize v0.5.2

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Serialize

Serialize Objects, Functions, primitive types, builtin objects, nested objects, class instances, methods, setters and getters, cross and self references objects, Regular Expressions (including status), Maps, Sets, Errors, binary arrays

Setup

1. Install

npm install jsonlize

Usage

1. Serialize and deserialize an class instance

const { serialize, deserialize } = require('jsonlize')
class A {
  constructor(n) {
    this.n = n
  }
  inc(n) {
    this.n += n
  }
}
const a = new A(10)

const json = serialize(a)

const aa = deserialize(json)
aa.inc(5)

2. Serialize and deserialize nested objects

const { serialize, deserialize } = require('jsonlize')
class A {
  constructor(n) {
    this.n = n
  }
  inc(n) {
    this.n += n
  }
}

class C {
  constructor(n) {
    this._a = new A(n)
  }
  set a (value) {
    this._a = value
  }
  get a () {
    return this._a
  }
}

const c = new C(10)

const json = serialize(c)

const cc = deserialize(json)
cc.a.inc(5)

Limitations

1. Functions defined in a scope

It won't work if functions access to scoped variables

let obj = {/*...*/}
const f = function () { return obj.prop }
const json = serialize(f)

2. Class Private members

Instances of classes with private members cannot be serialized without errors

class A {
  #x = 'I am private'
}
const json = serialize(new A())
0.5.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.4.1

3 years ago

0.4.2

3 years ago

0.4.0

3 years ago

0.1.1

3 years ago

0.0.1

4 years ago