Skip to content
Grant Carthew edited this page Aug 27, 2018 · 10 revisions

Perj JSON Serialization

Initially with version 1 of the perj logger the JavaScript objects were converted into JSON strings (serialized) by using the fast-safe-stringify module.

It was discovered soon after the initial version 1 release that the fast-safe-stringify module had issues recursively parsing some browser specific objects such as Vue.js. It was also made apparent that because of fast-safe-stringify, using the passThrough option was literally passing the object references though to the write function. If a custom write function changed an object it would cause side effects.

With these issues in mind a new internal module was created called notation-copy.

notation-copy Module

The notation-copy module is an internal module to the perj project. It offers major improvements in the parsing of JavaScript objects both in the Node.js and Browser environments.

The notation-copy module supports the following features:

  • Fast object parsing.
  • Supports the same API as Object.assign.
  • Deep copies JavaScript objects in preparation for JSON stringification.
  • Limits recursion to 2000 calls.
  • Converts Error objects properties into enumerable properties.
  • Replaces circular references with the [Circular] string token.
  • Removes non-JSON related constructs.
  • Follows the JSON.stringify MDN reference documentation for JavaScript type support.

By passing an object into the notation-copy module the returned resulting object has no references to the original object and is void of non-JSON constructs.

The notation-copy module is used to copy objects passed into the perj log methods. These sanitized objects are then stringified with a simple JSON.stringify call to provide the logger output. If the passThrough option is used with a custom write function, the object passed into the custom write function is clear of circular references and non-JSON related constructs.

Due to cross platform support, release v3.0.1 of the Perj module added a recursion limit. Prior to the recursion limit, if the browser window object was logged (e.g. log.info(window) ) it would kill the browser. Now if the object logged causes 2000 or more recursion calls, the object analysis is aborted and a warning is displayed on the console.

Despite copying the content from logged objects, perj using the notation-copy module remains extremely fast. See the Performance document for more detail.