Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Rhino's native JSON.Stringify from Java

Tags:

rhino

Is there a cleaner way to get the JSON representation of a Javascript object than with the following kludge?

System.out.println(((ScriptableObject) scope).callMethod(
    cx, (Scriptable) scope.get("JSON", scope), 
    "stringify", new Object[]{jsObject}));

Where jsObject is the ScriptableObject I want to stringify.

like image 900
ironchefpython Avatar asked Apr 23 '12 21:04

ironchefpython


People also ask

Can I use JSON Stringify in Java?

stringify(JSONObject) is a Javascript function and will not be available in Java. If you're using the org. json. * package built in the Android SDK, the equivalent would be to simply call toString() on your JSONObject instance, or the more human-friendly toString(int) .

How do you Stringify an object in Java?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.

How do I Stringify a string in JSON?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

What is JSON Stringify in react native?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.


2 Answers

Note that Hannes has now addressed this in Rhino. So the usage simplifies to this:

import org.mozilla.javascript.NativeJSON;
// ...

Object json = NativeJSON.stringify(cx, scope, jsObject, null, null);

The org.mozilla.javascript.NativeJSON class should be public in the Rhino 1.7R4 release.

like image 55
Tim Schaub Avatar answered Sep 19 '22 04:09

Tim Schaub


I was able to get this working within an Apache Ant target using the NativeJSON class.

importPackage(org.mozilla.javascript);

var context = Context.enter();
var json = '{}';
// The call to parse required a reviver function that should return the
// state of a key/value pair.
var reviver = function(key, value) { return value; };
var scope = context.initStandardObjects();
var object = NativeJSON.parse(context, scope, json, reviver);

// The call to stringify does not require the replacer or space parameters.
// The replacer is a function that takes a key/value pair and returns the
// new value or an array of keys from the input JSON to stringify. The space
// parameter is the indentation characters or length.
json = NativeJSON.stringify(context, scope, config, null, 4);

http://mozilla.github.io/rhino/javadoc/org/mozilla/javascript/NativeJSON.html https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/NativeJSON.java

like image 25
Trevor Karjanis Avatar answered Sep 22 '22 04:09

Trevor Karjanis