Skip to content
Grant Carthew edited this page Aug 6, 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.
  • Converts Error objects properties into enumerable properties.
  • Replaces circular references with the [Circular] string token.
  • Removes non-JSON related constructs such as Functions.
  • 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.

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