title: How can I convert a Rhino-produced JSON datastructure (NativeObject) to Java datastructure?
body: <p>I am trying to generate a data structure inside JavaScript code, and process the results from Java (Rhino in Java 6 or 7).</p>
  
  <p>I succeeded in invoking the JS function from Java using:</p>
  
  <pre><code>Object result = invocableEngine.invokeFunction(functionName, parameters);
  </code></pre>
  
  <p>Now, on JS side, that result is a JSON data structure as follows:</p>
  
  <pre><code>{ 
    "key1" : { "key1_1" : "value1_1", "key1_2" : 11 },
  
    "key2" : [ {  "key2_1": "foo2"
                , "key2_1" : "bar2" },
  
               {  "key2_1": "foo2"
                , "key2_1" : "bar2"
                , "key2_3" : [1,2,3] } ]
  }
  </code></pre>
  
  <p>On the Java side, however, "result" is an object of type <code>sun.org.mozilla.javascript.internal.NativeObject</code></p>
  
  <p><strong>Is there a way for me to convert it to a reasonable Java datastructure</strong>, without having the overhead of serializing that JSON to a string and then using Gson to parse it?</p>
  
  <p>Assume for the moment that the data structure is reasonably well defined, e.g.
  I know that the object is a hash with keys "key1" and "key2", the first maps to a hash with keys "key1_1" and "key1_2"; the second maps to an array of hashes, each of which has keys "key2_1" and "key2_2" that are scalar and "key2_3" which is an array.</p>
  
  <p>My 2 priorities are, in that order:</p>
  
  <ol>
  <li>Performance (which is one reason why I'm wary of serialize + Gson-parse)</li>
  <li>Portability between Java 6, 7 and 8 (however, I'd prefer a materially faster non-portable solution).</li>
  </ol>