diff --git a/README.md b/README.md index 42fa520..0223e5f 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,25 @@ var parse = require('warp10/parse'); var object = parse(json); ``` +## JSON stringifyPrepare/finalize + +The `stringifyPrepare` function can be used to produce a JavaScript object that is safe to serialize using the native `JSON.stringify` method. The `finalize` method should be called on the parsed object to produce the final object with duplicate objects and circular dependencies intact. + +_On the server:_ + +```javascript +var warp10 = require('warp10').stringifyPrepare; +var object = stringifyPrepare(object); // Returns an Object +var json = JSON.stringify(object); +``` + +_In the browser:_ + +```javascript +var finalize = require('warp10/finalize'); +var clone = finalize(JSON.parse(json)); +``` + # Examples ## Serialize examples diff --git a/finalize.js b/finalize.js new file mode 100644 index 0000000..17c486c --- /dev/null +++ b/finalize.js @@ -0,0 +1 @@ +module.exports = require('./src/finalize'); \ No newline at end of file diff --git a/src/finalize.js b/src/finalize.js new file mode 100644 index 0000000..fed005f --- /dev/null +++ b/src/finalize.js @@ -0,0 +1,50 @@ +var isArray = Array.isArray; + +function resolve(object, path, len) { + var current = object; + for (var i=0; i