Is there native JSON support in the JDK6 Script Engine?
I am using standard JDK6 scripts. I need to store and retrieve some JavaScript objects that also contain Java objects for JSON. I have loaded json2.js into ScriptENgine and can use it without any problem if all objects are created in Scrip Engine. The moment I try to use my own Java classes, I get some errors like "object does not support JSON" errors.
I don't know much about JSON in the context of Java Scripting / Rhino.
Am I doing something completely wrong? What is the best way to achieve this requirement?
a source to share
As far as I know, rhino does not come with built-in JSON serialization capability.
The errors you see are probably because json2.js uses an operator typeof value.toJSON
to define the object in question, implements its own serialization function. Property lookup errors are returned on JavaScript objects undefined
. On Java objects, lookup errors are not handled by exceptions.
One approach to solve this problem would be to modify json2 so that when traversing the object structure:
- it checks if each value is propagated from a
Object
JavaScript database base class or a Java base class,Object
and - applies different coding logic to Java and JavaScript object instances.
I don't know if anyone has resolved the issue of serializing an arbitrary Java object to JSON through reflection. If so, then it might be useful to use.
Hope this helps!
a source to share