joeferner / node-java

Bridge API to connect with existing Java APIs.
MIT License
1.87k stars 280 forks source link

Store Javascript object in Java objects #259

Open joeferner opened 8 years ago

joeferner commented 8 years ago

Currently we return NULL for Javascript objects we can't convert (see https://github.com/joeferner/node-java/blob/master/src/utils.cpp#L367). It would be nice to be able to store those Javascript objects and retrieve them later.

var Test = java.import("Test");
Test.staticMethodSetObjectSync(function() { return 42; });
var fn = Test.staticMethodGetObjectSync();
fn(); // returns 42

I have created a branch here: https://github.com/joeferner/node-java/commit/732d26dc3606694cc279ddf4ecbdd60720b15afe

One problem is when do we delete the persisted object. https://github.com/joeferner/node-java/blob/store-javascript-objects-in-java/src/utils.cpp#L377

dasarindam commented 7 years ago

@joeferner Could you please help me to know how to pass a javascript obj to the backend java program ? Something like this.. I am trying this and getting undefined... JavaScriptObj -> remoteVar = {'remServer': '123'}; Javacode ->

    public String jarunargs2(Object Obj){
        String str = Obj.toString();    
        return "J Ran with args " + str;
    }

Script call - > test.jarunargs2(remoteVar, function(err,result){console.log("hello 2" + result )});

I am getting console as below. hello 2undefined

dasarindam commented 7 years ago

Small finding... I found that the remoteVar has a value in JS. But after passing to the JAVA method its showing as null... and erroring out. TIA

joeferner commented 7 years ago

For now your best bet is going to be converting that object into a string before passing it to java JSON.stringify and then in Java you something like Jackson to parse it out.