Hi guys, I have resolved the earlier error that I reported, I thought I was using the correct jdk but then I wasn't and switching to the latest version of the OpenJDK (nashorn) source and then bootstrapping to the image created by the build process fixed it (hinted by you) - thanks for that.
I'm now able to access the member elements, returned by Javascript through the JSObject. Although the JSON I created also contained an array of string elements, which I wasn't able to access but got [object Array] when I printed it to console. I tried casting it to org.json.JSONArray but it was of no use. Then I came across the link http://cr.openjdk.java.net/~hannesw/8007060/webrev.01/src/jdk/nashorn/internal/objects/NativeFunction.java.html, and applying the checks on the object to see if it can be converted or not I got the output: function.apply.expects.array (didn't pass any of the checks, and terminated at the last else). Is there a JSONArray object within the nashorn package that I could use? Do you have any other example for that? Thanks. Regards. Mani On Fri, Jun 7, 2013 at 8:03 PM, Mani Sarkar <[email protected]> wrote: > Thanks Sundar, Jim for the explanation. > > I extracted the jsobjectTest() from ScriptEngineTest.java as it is and > compiled and ran it, when I run it I still get the same error that I got > with my example. I have put all of this in a gist to demonstrate what I was > doing: > https://gist.github.com/neomatrix369/5731520 > > I also ran the ScriptEngineTest using the jtreg command and that ran > successfully: > > ~/jtreg/linux/bin/jtreg -jdk:/usr/lib/jdk1.8.0 > -Xbootclasspath/p:$HOME/sources/jdk8_tl/nashorn/dist/nashorn.jar > -verbose:fail test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java > > Regards, > mani > > > On Thu, Jun 6, 2013 at 11:25 AM, Mani Sarkar <[email protected]> wrote: > >> Hi Jim, >> >> You have been very quick with your response to my query. >> >> Thanks for going through the trouble, apologies for the empty folders, >> they are submodules that haven;t been pushed correctly to my repo. >> >> I have made the changes you suggested and rebuilt my class (I had to get >> the latest version of the codebase as JSObject wasn;t present in >> nashorn.jar in my old code-base). >> >> After building and running I get the below: >> >> Exception in thread "main" java.lang.ClassCastException: >> jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to >> jdk.nashorn.api.scripting.JSObject >> at JSJSONInJava.main(JSJSONInJava.java:19) >> >> Lines 18 and 19 are as follows: >> 17: // fetch the value of a variable >> 18: Object objFromJS = engine.get("JSONObjectUsingJavaScript"); >> 19: JSObject jsonObjFromJS = (JSObject) objFromJS; >> >> Any ideas, or is it something new? >> >> Regards, >> mani >> >> >> On Wed, Jun 5, 2013 at 8:51 PM, Jim Laskey <[email protected]>wrote: >> >>> The repo you provided has empty JSON-java and JSON-js directories, so >>> I'll give you an alternative example. The main thing to note is that, >>> because of a JS object's dynamic nature, it can not mirror a Java object. >>> A better analogy would be to think of a JS object as a Map object, where >>> properties are keys and you access values with keys. >>> >>> Run the enclosed example as follows; >>> >>> javac Example.java >>> java Example >>> >>> The class of myObject is a JSObject. You can access properties of a >>> JSObject with getMember/setMember (or getSlot/setSlot for integer keys.) >>> >>> === Example.java === >>> >>> import javax.script.*; >>> import jdk.nashorn.api.scripting.JSObject; >>> >>> public class Example { >>> public static void main(String[] args) throws Exception { >>> ScriptEngineManager factory = new ScriptEngineManager(); >>> ScriptEngine engine = factory.getEngineByName("nashorn"); >>> engine.eval(new java.io.FileReader("Example1.js")); >>> JSObject myObject = (JSObject)engine.get("myObject"); >>> System.out.println(myObject.getMember("a")); >>> System.out.println(myObject.getMember("b")); >>> System.out.println(myObject.getMember("c")); >>> myObject.setMember("d", "A new string"); >>> engine.eval(new java.io.FileReader("Example2.js")); >>> >>> } >>> } >>> >>> === Example1.js === >>> >>> var myObject = { >>> a: "A string", >>> b: 100, >>> c: true >>> } >>> >>> === Example2.js === >>> >>> print(myObject.d); >>> >>> ==== Output === >>> >>> A string >>> 100 >>> true >>> A new string >>> >>> >>> Cheers, >>> >>> -- Jim >>> >>> >>> >>> >>> >>> >>> On 2013-06-05, at 3:50 PM, Mani Sarkar <[email protected]> wrote: >>> >>> Hi, >>> >>> I have another query regarding the example (see >>> >>> https://github.com/neomatrix369/NashornHackDay/blob/master/examples/JSON_in_JS_and_Java/JSJSONInJava.java >>> ) >>> created sometime back during the Nashorn hackday. When I bring a JS >>> object >>> created in Nashorn into Java I'm not able to access the object directly, >>> how do I access it like a normal java object. >>> >>> If its a raw / primitive type then the contents are accessible (you can >>> see >>> the value) while for JS object, when I say >>> >>> *System.out.println(JSObjectFromNashorn);* >>> >>> >>> I get the below output >>> >>> *[object object]* >>> >>> >>> The full implementation of what I'm talking about can be found at the >>> above >>> link. >>> >>> Regards, >>> mani >>> >>> -- >>> *Twitter:* @theNeomatrix369 *Blog:* >>> http://neomatrix369.wordpress.com >>> *JUG activity:* LJC Advocate (@adoptopenjdk & @adoptajsr programs) >>> *Meet-a-Project:* https://github.com/MutabilityDetector >>> *Bitbucket:* https://bitbucket.org/neomatrix369 * **Github:* >>> https://github >>> .com/neomatrix369 >>> *LinkedIn:* http://uk.linkedin.com/pub/mani-sarkar/71/a77/39b >>> *Devoxx UK 2013* was a grand success: >>> http://www.devoxx.com/display/UK13/Home >>> >>> *Don't chase success, rather aim for "Excellence", and success will come >>> chasing after you!* >>> >>> >>> >> >> >> -- >> *Twitter:* @theNeomatrix369 *Blog:* >> http://neomatrix369.wordpress.com >> *JUG activity:* LJC Advocate (@adoptopenjdk & @adoptajsr programs) >> *Meet-a-Project:* https://github.com/MutabilityDetector >> *Bitbucket:* https://bitbucket.org/neomatrix369 * **Github:* https:// >> github.com/neomatrix369 >> *LinkedIn:* http://uk.linkedin.com/pub/mani-sarkar/71/a77/39b >> *Devoxx UK 2013* was a grand success: >> http://www.devoxx.com/display/UK13/Home >> >> *Don't chase success, rather aim for "Excellence", and success will come >> chasing after you!* >> > > > > -- > *Twitter:* @theNeomatrix369 *Blog:* > http://neomatrix369.wordpress.com > *JUG activity:* LJC Advocate (@adoptopenjdk & @adoptajsr programs) > *Meet-a-Project:* https://github.com/MutabilityDetector > *Bitbucket:* https://bitbucket.org/neomatrix369 * **Github:* https:// > github.com/neomatrix369 > *LinkedIn:* http://uk.linkedin.com/pub/mani-sarkar/71/a77/39b > *Devoxx UK 2013* was a grand success: > http://www.devoxx.com/display/UK13/Home > > *Don't chase success, rather aim for "Excellence", and success will come > chasing after you!* > -- *Twitter:* @theNeomatrix369 *Blog:* http://neomatrix369.wordpress.com *JUG activity:* LJC Advocate (@adoptopenjdk & @adoptajsr programs) *Meet-a-Project:* https://github.com/MutabilityDetector *Bitbucket:* https://bitbucket.org/neomatrix369 * **Github:* https://github .com/neomatrix369 *LinkedIn:* http://uk.linkedin.com/pub/mani-sarkar/71/a77/39b *Devoxx UK 2013* was a grand success: http://www.devoxx.com/display/UK13/Home *Don't chase success, rather aim for "Excellence", and success will come chasing after you!*
