Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way to pass JSON between java and javascript

I'm fairly new to Nashorn and scripting on top of the JVM and wanted to know if I can get my java code and javascripts to communicate more effectively.

I'm using a 3rd party JS lib that works with JS objects, and in my java code I have the data I want to pass as a Map<String, Object> data.

Because that 3rd party JS expects to work with plain JS objects I can't pass my data as is, although the script engine allows you to access a Map as if it was a plain JS object.

The script i'm using uses 'hasOwnProperty' on the data argument and fails when invoked on an Java object.

When I tried using Object.prototype.hasOwnProperty.call(data, 'myProp') it also didn't work and always returned 'false'. The basic problem is that a Java Object is not a javascript object prototype.

what I ended up doing something like this :

Map<String, Object> data;

ObjectMapper mapper = new ObjectMapper();   
String rawJSON = mapper.writeValueAsString(data);

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

engine.eval('third_party_lib.js');
engine.eval('function doSomething(jsonStr) { var jsObj = JSON.parse(jsonStr); return doSomethingElse(jsObj); }');
Object value = ((Invocable) engine).invokeFunction("doSomething", rawJSON);

This works as expected, but all this JSON parsing back and forth is heavy and feels like there might be simpler, faster and more straight forward way to do it.

So, is there a better way to pass JSON between Java and Javascript or a way to create a compatible JS object in my Java code ?

I've seen this guide to template rendering using mustache.js but it's doing pretty much the same thing.

Thanks !

like image 645
Ronny Shapiro Avatar asked Jul 08 '15 12:07

Ronny Shapiro


People also ask

How to consume JSON in JavaScript?

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How to send JSON data to server using JavaScript?

Send JSON Data from the Client SideCreate a JavaScript object using the standard or literal syntax. Use JSON. stringify() to convert the JavaScript object into a JSON string. Send the URL-encoded JSON string to the server as part of the HTTP Request.

How to access JSON data from JavaScript?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

Can I use JSON in JavaScript?

JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. Even though it closely resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability to read (parse) and generate JSON.


1 Answers

Nashorn treats java.util.Map objects specially. Nashorn allows Map keys to be treated as "properties". See also https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-SpecialtreatmentofobjectsofspecificJavaclasses

So if your map contains "foo" as key, script can access mapObj.foo to get it's value. It does not matter the script you evaluated is third-party one. As long as the script is evaluated by Nashorn, nashorn will specially link Map property access and get the result you want. This approach avoid unnecessary JSON string conversion and parse round trip (as you yourself mentioned).

like image 51
A. Sundararajan Avatar answered Sep 23 '22 03:09

A. Sundararajan