Search code examples
javascriptnode.jssoapnode-java

Assigning java objecs to node.js variable


This is my piece of code that creates java objects in node.js using the 'java' module. Four java objects are created and I want them together as a single object. So I am using the variable, 'args' to hold these java objects.

var java = require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
var soap = require('soap');

var vehId = java.newInstanceSync("java.lang.Integer", 922);
var lattitude = java.newInstanceSync("java.lang.Double", 8.6717136);
var longitude = java.newInstanceSync("java.lang.Double", 76.8168311);
var GregorianCalendar = java.import('java.util.GregorianCalendar');
var time = new GregorianCalendar(2014,4,29,8,7,6);
console.log('vehId: '+vehId+'\nlattitude: '+lattitude+'\nlongitude '+longitude+'\ntime: '+time+'\n\n');

var args = {
            vehicleId : vehId,
            lat : lattitude,
            lan : longitude,
            packetTime: time
};
console.log(args);

This is the output I got.

vehId: 922

lattitude: 8.6717136

longitude 76.8168311

time: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2014,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=29,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=7,SECOND=6,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]

{ vehicleId: 922,

lat: 8.6717136,

lan: 76.8168311,

packetTime: {}

}

Anybody please tell me why 'packetTime' variable of 'args' object is not assigned with 'time' value?? Why it is showing as {} ???


Solution

  • It's not evident how the GregorianCalendar object is structured once converted to a JavaScritp object.

    But the reason why it works in your first log is that in that case you are concatenating it to a string, so you are actually triggering the toString method in Gregorian calendar, which seems to use the Java's implementation of the method.

    In other words, if you did either of these:

    console.log('' + time);
    console.log(time.toString());
    console.log("%s", time);
    

    You would get the expected output string.

    On the other hand, console.log(time) does not invoke the object's toString method, but attempts to retrieve all the keys in the hash object. It looks like the GregorianCalendar object does not have any enumerable properties that are visible and that's why you would get an empty result.

    console.log("%j", time);