JavaScript Stringify
Stringify is to
eval
asJSON.stringify
is toJSON.parse
.
Installation
npm install javascript-stringify --savebower install javascript-stringify --save
Node
var javascriptStringify = ;
AMD
;
<script>
tag
Usage
The API is similar to JSON.stringify
. However, any value returned by the replacer will be used literally. For this reason, the replacer is passed three arguments - value
, indentation
and stringify
. If you need to continue the stringification process inside your replacer, you can call stringify(value)
with the new value.
The options
object allows some additional configuration:
- maxDepth (number, default: 100) The maximum depth of values to stringify
- maxValues (number, default: 100000) The maximum number of values to stringify
- references (boolean, default: false) Restore circular/repeated references in the object (uses IIFE)
- skipUndefinedProperties (boolean, default: false) Omits
undefined
properties instead of restoring asundefined
Examples
; // "{}"; // "true"; // "'foo'" ; // "{x:5,y:6}"; // "[1,2,3,'string']" ; // "{a:{b:{}}}" /** * Invalid key names are automatically stringified. */ ; // "{'some-key':10}" /** * Some object types and values can remain identical. */ ; // "[/.+/gi,new Number(10),new Date(1406623295732)]" /** * Unknown or circular references are removed. */ var obj = x: 10 ;objcircular = obj; ; // "{x:10}"; // "(function(){var x={x:10};x.circular=x;return x;}())" /** * Specify indentation - just like `JSON.stringify`. */ ; // "{\n a: 2\n}"; // "{\n\tuno: 1,\n\tdos: 2\n}" /** * Add custom replacer behaviour - like double quoted strings. */ ;//=> '["test","string"]'
License
MIT