node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »

jsonsca

JSON SCA

A serialization tool supporting HTML5 Structured Clone Algorithm types.

You can read more about the SCA types from MDN: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

The algorithm is part of HTML5 specifications that define things like what types may be stored in IndexedDB and how data is transferred between Web Workers and iFrames via the postMessage() API. This covers a much wider range of types than JSON, which postMessage() originally used, but is not actually a serialization format. SCA only defines how the data is copied across boundaries, not any way to actually represent this.

Supported Types

This is a work in progress, but was fully functioning as part of a previous project. As a stand-alone library support for some types is still forthcoming. Currently known to work and be tested includes:

  • string
  • number
  • boolean
  • null
  • undefined
  • objects (including multiple references and cyclic references)
  • arrays (including some reference features)

Upcoming:

  • RegExp objects
  • ImageData
  • File and FileList and Blob

API

jsonsca.stringify(obj)

Serialization a compatible object into a string. Returns a promise, because some types can only be serialized by processing them through asynchronous means.

jsonsca.parse(string)

Deserialize data previously serialized. Preserves multiple references to shared objects and cyclic references.