I’m working with Javacript in Rhino and cannot import externals (but I do have access to java.lang and java.util), so I am looking for a toString() that emits JSON of the object on which it lives.
MyOjbect.prototype.toString = function() { /* magically emits JSON of MyObject */ }
I don’t mind writing this from scratch but if this already exists it would save some time.
This is merely for a debugging dump to log of custom objects.
Edit: Older version of Rhino, updating not an option. No stringify().
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:
By doing this you could build a subset of your object and stringify that instead of the object prototype itself:
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.