Motivation: JSON doesn't support serializing functions, dates, or regular expressions. I wanted a quick and simple way to push trusted data structures with code from Node down to the browser.
This should make it easier to share code and modules between the server and client.
The following code:
var toSource = require('tosource')
console.log(toSource(
[ 4, 5, 6, "hello", {
a:2,
'b':3,
'1':4,
'if':5,
yes:true,
no:false,
nan:NaN,
infinity:Infinity,
'undefined':undefined,
'null':null,
foo: function(bar) {
console.log("woo! a is "+a)
console.log("and bar is "+bar)
}
},
/we$/gi,
new Date("Wed, 09 Aug 1995 00:00:00 GMT")]
))
Outputs:
[ 4,
5,
6,
"hello",
{ "1":4,
a:2,
b:3,
"if":5,
yes:true,
no:false,
nan:NaN,
infinity:Infinity,
"undefined":undefined,
"null":null,
foo:function (bar) {
console.log("woo! a is "+a)
console.log("and bar is "+bar)
} },
/we$/gi,
new Date(807926400000) ]
See test.js for more examples.
func.toString(), no closure properties are serialized{$circularReference:1}toSource is open source software under the zlib license.
--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
Not to diminish your work but you should look at http://github.com/substack/browserify and http://github.com/substack/dnode
They provide different functions but allow code from the server to be used in the browser.