Passing an arbitrary JSONValue to a JSNI function
- by Riley Lark
I have a JSONValue in my Java that may be a JSONArray, a JSONObject, a JSONString, etc. I want to pass it to a JSNI function that can accept any of those types. If I naively write my JSNI as something like:
public final native jsni(Object parameter) /*-{
doSomething(parameter);
}-*/;
public void useFunction(JSONValue value) {
jsni(value); //Throws js exception at runtime :(
}
then I get a javascript exception, because GWT doesn't know how to convert the JSONValue to a JavaScriptObject (or native string / number value).
My current workaround is
public final native jsniForJSO(Object parameter) /*-{
doSomething(parameter);
}-*/;
public final native jsniForString(String parameter) /*-{
doSomething(parameter);
}-*/;
public final native jsniForNumber(double parameter) /*-{
doSomething(parameter);
}-*/;
public actuallyUseFunction(JSONValue value) {
if (value.isObject()) {
jsniForJSO(value.isObject().getJavaScriptObject());
} else if (value.isString()) {
jsniForString(value.isString().stringValue());
} else {
//etc
}
}
This is a big burden for code maintainability, etc... especially if you have more than one parameter. Is there a way to generate these functions automatically, or get around this issue altogether? I've taken to wrapping everything in a JSONObject first, so I can definitely get a JavaScriptObject to pass to my jsni, but that's another clumsy mechanic.