2

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().

Robert Kerr
  • 1,291
  • 2
  • 17
  • 34
  • 2
    Is `JSON.stringify` present in Rhino? – J. K. Oct 18 '12 at 13:23
  • @IanKuca in sufficiently recent versions of Rhino, yes. – Pointy Oct 18 '12 at 13:24
  • Hmm good idea, looks like it was added. Unfortunately the rhino I'm using is an older version and updating it is not an option. But that puts me on the right track, if I can dig down in stringify() and see if the licensing allows me to copy that. – Robert Kerr Oct 18 '12 at 13:27

2 Answers2

3

Have you tried it with JSON.stringify()? It should be present, even in Rhino.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • For those stuck on JDK6 and using Rhino via the `ScriptEngine` stuff, it's not there, but that's one of the better reasons to get off JDK6 anyway :-) – Pointy Oct 18 '12 at 13:25
1

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.

Community
  • 1
  • 1
Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33
  • older version of rhino. updating not an option. – Robert Kerr Oct 18 '12 at 13:30
  • Can't you add a library as in a browser? If you can't, then just copy-paste the contents of json2.js before using the function (you should check for licensing problems, but I don't believe in this case there could be a problem) – Pablo Mescher Oct 18 '12 at 13:34
  • I'm locked purely in the scripting end so have to write everything I need. Looks like copying from json.js is the way to go. – Robert Kerr Oct 18 '12 at 13:47