4

I'm using python-spidermonkey to run JavaScript code.

In order to pass objects (instead of just strings) to Python, I'm thinking of returning a JSON string.

This seems like a common issue, so I wonder whether there are any facilities for this built into either Spidermonkey or python-spidermonkey. (I do know about uneval but that is not meant to be used for JSON serialization - and I'd rather avoid injecting a block of JavaScript to do this.)

AnC
  • 4,099
  • 8
  • 43
  • 69

1 Answers1

7

I would use JSON.stringify. It's part of the ECMAScript 5 standard, and it's implemented in the current version of spidermonkey. I don't know if it's in the version used by python-spidermonkey, but if it isn't, you can get a JavaScript implementation from http://www.json.org/js.html.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
  • I've installed the spidermonkey-bin package from the Ubuntu repositories (version info: JavaScript-C 1.7.0 2007-10-03). However, there's no global JSON (or json) object there ("ReferenceError: JSON is not defined"). – AnC Jun 29 '09 at 21:52
  • 3
    You'll need 1.8.1 to have the JSON object built-in. – Matthew Crumley Jun 29 '09 at 22:54
  • Ah, that explains it - thanks! I'll look into getting (compiling?) the latest version then, and will use the JS file in the meantime. – AnC Jun 30 '09 at 10:33