Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting JS to Map

1,115 views
Skip to first unread message

Patrick Lightbody

unread,
Mar 4, 2009, 12:33:04 AM3/4/09
to dev-tech-js-...@lists.mozilla.org
I'd like to have my JS code do something like this:

foo.someJavaMethod({
key: 'value'
});

Where someJavaMethod looks like:

public void someJavaMethod(Map<String, String> params) {
...
}

However, when I do that, I get an error in JS: "Cannot convert [object
Object] to java.util.Map". If I make params an Object, then I can get
a handle to the NativeObject, but I can't quite figure out how to
navigate and convert it through. Any tips?

Patrick

Ruland Kevin-BHP637

unread,
Mar 4, 2009, 7:12:16 AM3/4/09
to Patrick Lightbody, dev-tech-js-...@lists.mozilla.org
Patrick,

I'm typing this off the cuff, it might not be fully correct.
NativeObject.getIds() returns all the property names. This includes
array indexes. This snippet of code should properly decode arrays (or
array portions of objects) into the map. The keys to the array values
are the string representation of the index number.

public Map<String,Object> objectToMap( NativeObject obj ) {

HashMap<String,Object> map = new HashMap<String,Object>();

for( Object id: obj.getIds() ) {
String key;
Object value;
if (id instanceof String ) {
key = (String) id;
value = obj.get(key,obj);
} else if (id instanceof Integer) {
key = id.toString();
value = obj.get( ((Integer)id).intValue(), obj);
} else {
throw new IllegalArgumentException();
}
map.put( key, value );
}

return map;
}

Kevin

foo.someJavaMethod({
key: 'value'
});

Where someJavaMethod looks like:

Patrick
_______________________________________________
dev-tech-js-engine-rhino mailing list
dev-tech-js-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Patrick Lightbody

unread,
Mar 4, 2009, 9:20:22 AM3/4/09
to Ruland Kevin-BHP637, dev-tech-js-...@lists.mozilla.org
Ruland,
OK - that's what I ended up doing last night. I was just hoping there
would be some sort of more advanced type coercion system. No biggie -
thanks!

Patrick

Attila Szegedi

unread,
Mar 4, 2009, 9:25:38 AM3/4/09
to Patrick Lightbody, Ruland Kevin-BHP637, dev-tech-js-...@lists.mozilla.org
Honestly, there should be advanced type coercion. We're sticking to
the LiveConnect "standard" for Java-JS interop, but my own opinion is
that LiveConnect is outdated and we should enhance it any way we see
fit (starting with recognizing boolean as a first-class data type!).
Automatically coercing a Scriptable to Map sounds like one of these
things that should Just Work(TM). I certainly implemented all kinds of
commonsensical coercions to expected Java types over in the FreeMarker
project, and people are generally happy when their language runtimes
do that work for them...

Attila.

Marcello Bastéa-Forte

unread,
Mar 4, 2009, 4:09:56 PM3/4/09
to Attila Szegedi, Ruland Kevin-BHP637, Patrick Lightbody, dev-tech-js-...@lists.mozilla.org
Can it be done as a "non-standard" extension to rhino?

Marcello

Patrick Lightbody

unread,
Mar 6, 2009, 11:56:09 AM3/6/09
to Attila Szegedi, Ruland Kevin-BHP637, dev-tech-js-...@lists.mozilla.org
Atilla,
Yes, FreeMarker is a perfect example of how I'd love to see this work.
Where do you imagine this could fit in to the Rhino roadmap?
0 new messages