Git Product home page Git Product logo

structured-clone's Introduction

structured-clone

Implements the structured clone algorithm. Clone JSON types, RegExp, Buffers, and Dates, returning a cloned object or a buffer that can be deserialized into a structured clone

npm install structured-clone
var clone = require('structured-clone');
  • clone(obj) → Object
  • clone.serialize(obj) → Buffer
  • clone.deserialize(buf) → Object

Encoded format

The encoded format takes this form:

  1. A UTF-8 encoded JSON string.
  2. A null byte.
  3. A binary blob that is the concatenation of all binary buffers in the original object. There are no delimiters in this buffer, indexes are represented in the JSON value (see below).

Dates, regexps, buffers, and cycles are encoded in a particular way to be decoded properly:

  • Dates are encoded as the string '\x10d' followed by the JSON-stringified encoding of the date.
  • Regexps are encoded as the string '\x10r{flags},{regexp source}'.
  • Buffers are encoded as the string '\x10b{start},{length}'. All buffers in the encoded value are concatenated and placed in the binary blob. The start and length parameters indicate the indexes the slice of the buffer was encoded in.
  • Lastly, string that begin with '\x10' are encoded as '\x10s{string}' to properly escape them.

Optimizations: If only a JSON value is being encoded (i.e. no Buffer values included), the null byte can be omitted, thus making the encoded format equivalent to a JSON-encoded string. If only a buffer is being encoded, it is equivalent to a null byte followed by the buffer (i.e. the JSON string is 0-length).

License

MIT

structured-clone's People

Contributors

johnnyman727 avatar tcr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.