emilbayes/object-tojson


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

License: ISC

Language: JavaScript


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'}

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

API

serialise(value, [replacer], [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.

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

Project Statistics

Sourcerank 8
Repository Size 7.81 KB
Stars 7
Forks 0
Watchers 1
Open issues 0
Dependencies 2
Contributors 1
Tags 3
Created
Last updated
Last pushed

Top Contributors See all

Emil Bay

Packages Referencing this Repo

object-tojson
Turn a Javascript primitive into its "flat" representation. Aims to be equivalent to `JSON.parse(...
Latest release 3.0.0 - Updated - 7 stars

Recent Tags See all

v3.0.0 July 12, 2020
v2.0.0 July 12, 2020
v1.0.1 October 16, 2015

Something wrong with this page? Make a suggestion

Last synced: 2020-07-12 14:36:44 UTC

Login to resync this repository