JSON.stringify(MyObject) should do the trick. If you don't have this function, look for json2.js in the interwebs
Link: https://github.com/douglascrockford/JSON-js
If you want to have it inside your object, you can add a function like this:
MyObject.prototype.toString= function () {
return JSON.stringify(this);
}
By doing this you could build a subset of your object and stringify that instead of the object prototype itself:
MyObject.prototype.toString = function () {
var subset = {
...
}
return JSON.stringify(subset);
}
Here is a post on how to add a .js file to a rhino script
From the json license:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.