Nerdiest Precious Modules

    jsonlize

    0.5.2 • Public • Published

    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())

    Install

    npm i jsonlize

    DownloadsWeekly Downloads

    2

    Version

    0.5.2

    License

    MIT

    Unpacked Size

    54.9 kB

    Total Files

    30

    Last publish

    Collaborators

    • jvverde