JSPM

object-tojson

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q61245F
  • License ISC

Turn a Javascript primitive into its "flat" representation. Aims to be equivalent to `JSON.parse(JSON.stringify(obj))`.

Package Exports

  • object-tojson

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-tojson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

object-tojson

Build Status

Turn a Javascript primitive into its "flat" representation. Aims to be equivalent to JSON.parse(JSON.stringify(obj)).

Usage

Turn a Javascript primitive into its "flat" representation. Aims to be equivalent to JSON.parse(JSON.stringify(obj)). Note that it will recursively visit all properties and attempt to call toJSON, before trying to parse the value. undefined is not a valid JSON value, so it will attempt to remove properties that resolve to this value, unless it is an array element, in which case it resolves to null.

var serialise = require('object-tojson')

serialise({
  key: 'value',
  arr: [undefined, null, true],
  keyFn: {toJSON: function () { return 'valueFn' }}
}) // => {key: 'value', [null, null, true], keyFn: 'valueFn'}

// Impl compliant with replacer array
serialise({
  key: 'value',
  arr: [undefined, null, true],
  keyFn: {toJSON: function () { return 'valueFn' }}
}, ['key', 0]) // => {key: 'value', [null, null, true]}

// Spec compliant with replacer array
serialise({
  key: 'value',
  arr: [undefined, null, true],
  keyFn: {toJSON: function () { return 'valueFn' }}
}, ['key', 0], true) // => {key: 'value', [null, null, null]}

API

serialise(value, [replacer], [specCompliant = false], [circular = false])

Recursively serialise value as JSON.strinfigy, with optional replacer. replacer(key, value) is called with the holder of vale as the context (this). Return the replacement for value from this function.

specCompliant makes the replacer spec compliant. The spec says the replacer must be applied recursively to array elements with the index as the key, however neither Chrome nor Firefox let's you filter array elements in this way.

circular controls whether circular references should throw an error like JSON.stringify or preserve a circular reference to the serialised result.

``

Install

npm install object-tojson

License

ISC