Converting POJOs to ScriptObjects?

Tony Zakula tonyzakula at gmail.com
Fri Nov 18 13:53:19 UTC 2016


Hey All,

Seems there should be a way to do this that maybe I am missing?

To pass JS objects to Java, we use:

var data = {"myVar":"hello"};
Java.asJSONCompatible(data);

We also want to pass a POJO created on the Java side back to JS and convert
it to a ScriptObject so it acts like a standard JS object.  We have a Java
Object we built that is in global scope that the acts like a bridge to call
back to Java.  So the code works something like this right now:

var order = JSON.parse(bridge.call("getOrder"));

On the Java side:

Order order = getOrderFromDB();
return convertObjectToJSON(order);

This works fine, and the performance is decent, but it would seem that we
could reduce objects and overhead if we could convert the Order object to a
ScriptObject.  Maybe if we even created an empty ScriptObject and copied in
the properties.

Am I missing an easy way to do this all on the Java side?  I have thought
about calling back to the JS from Java to create the JS object and then
populate it from Java?

Any input is appreciated.  Nashorn is awesome.

Thanks!

Tony


More information about the nashorn-dev mailing list