On Saturday, September 15, 2012 5:10:12 AM UTC+2, pash7ka wrote:
>
> I've spent a huge amount of time trying to find the source of one strange
> bug in my app, so I want to share what I've found.
> First I should say I'm using GWT 2.3 currently, so i don't know if the
> described behavior changed in latest version.
>
> My app is receiving some data from my server, displays it and after some
> user actions send something from this data back to the server.
> I’ve used standart GWT RPC for this data exchange and everything was fine.
> Then we decided to receive that data via JSON requests and here my trubles
> started: trying to send data back to the server (with the old GWT RPC
> method) throwed the com.google.gwt.user.client.rpc.SerializationException:
> undefined at Unknown.anonymous(Unknown Source).
>
> Well, I’m not going to tell all the steps how I’ve found the problem, just
> the result.
> My data class looks like this:
> public final class MyDataObj extends JavaScriptObject {
> //… NoArgs constructor and other stuff
> public final native Ineger getMyDataVal() /*-{ return this.myDataVal; }-*/;
> }
>
> I’ve used Integer and not int here because null value is perfectly valid
> here and has its own meaning.
> But in JSON this was a normal JavaScript integer or the absence of the
> property.
> It was fine. Just until GWT had to serialize this Integer for RPC. Or
> until I’ve tried to use toString() or intValue() on it. That fails, since
> this was not an Integer.
>
>
JSNI doesn't do auto-boxing/unboxing at Java/JS boundaries, i.e. an Integer
is an opaque object in JS (typeof == 'object'), not a JS Number.
You have to change your JSNI method to:
native Integer getMyDataVal() /*-{ this.myDataVal == null ? null :
@java.lang.Integer::validOf(I)(this.myDataVal); }-*/;
or change your API to have an hasMyDataVal():boolean and getMyDataVal()int:
native boolean hasMyDataVal() /*-{ return this.myDataVal != null; }-*/;
native int getMyDataVal() /*-{ return this.myDataVal; }-*/;
BTW, your code as it is today should throw in DevMode, so I suppose you
only tested it in prod mode?
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-web-toolkit/-/yh1ZnvlwLzIJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.