views:

499

answers:

3

It seems like this should be relatively simple, but apparently not so much. I can't figure out for the life of me how to store strings and integers in an array in GWT. What data type do you use? If I use JsArrayString, it throws an IllegalArgumentException when retrieving an index containing a number. I obviously can't use JsArrayInteger (I have strings). JsArray requires a type, of which I have no idea what to use (if it can be used), I've tried String but get the same results.

The data being retrieved is from a script page and it does not have the ability to distinguish between strings and ints (Coldfusion).

A: 

I think you can use Object as your type. You'll have to box your primitives (int) in the Integer class if it doesn't do it automatically for you. You will also have to do some type checking after you grab what's in the array.

CookieOfFortune
JavascriptObject as type in the array?
Organiccat
yes try using JavascriptObject as a type. I'm not 100% certain it'll work, but give it a try.
CookieOfFortune
Unfortunately not, java.lang.ClassCastException: java.lang.String cannot be cast to com.google.gwt.core.client.JavaScriptObject(String can't be cast to JavaScriptObject)
Organiccat
JsArray are for JSON arrays... are you trying to just create a regular array?
CookieOfFortune
A JsArrayString to be precise, but Coldfusion refuses to pass in numbers (1, 2, 3, 4) as strings and GWT doesn't appear to have anything that will properly parse an array that holds both numbers and strings (on initialization it fails when retrieving numbers, even if I am attempting to cast it as a string).The workaround hack I used was to add a non-breaking space at the end and then when in GWT remove it with a regex.
Organiccat
Sorry I'm not more of an expert on this subject, but I guess hand-parsing JSON isn't too difficult.
CookieOfFortune
Yeah, I wouldn't mind doing that, it's just somewhat upsetting GWT can't handle a regular array with two different data types. My final solution was to add a few characters at the end and parse them out in GWT (Java).
Organiccat
A: 

It's kinda clunky, but you could always do some brute-force determinations of whether a given value is an int or string:

<cfset val1="one">
<cfset val2="1">
<cfif int(val(val1)) eq val1>int<cfelse>string</cfif>
<br />
<cfif int(val(val2)) eq val2>int<cfelse>string</cfif>

This should give you

 string
 int
Ben Doom
A: 

GWT does not currently have the functionality to parse an array structure that contains two different types of data unless in strict JSON form (not as part of a JSON structure in an array passed from ColdFusion apparently).

Organiccat