substack/json-stable-stringify — pull request #7 by kapetan (2014-05-27 12:19:04+00:00)
Title: Added options.replacer for custom object serialization

Added `replacer` option. Pass a function to customize object serialization. The function gets called with the current key and value, and `this` refers to the parent object. The return value of the function is serialized instead. Should behave the same as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter (passing an array is not supported).

These changes also fix some weird behaviour when dealing with undefined values. Before the following happened.

serialize({ a: 1, z: undefined }) => '{"a":1,"z":undefined}'
serialize([1,undefined,2]) => '[1,undefined,2]'

Now undefined object properties are ignored and are replaced with null in arrays.

serialize({ a: 1, z: undefined }) => '{"a":1}'
serialize([1,undefined,2]) => '[1,null,2]'