Passing an arbitrary JSONValue to a JSNI function

Posted by Riley Lark on Stack Overflow See other posts from Stack Overflow or by Riley Lark
Published on 2013-10-16T19:17:32Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 277

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about gwt

Related posts about jsni