I have attempted the following code snippet:

String data = "new $wnd.Date(2010,0,28,17,8,48,0)";
JSONValue value = JSONParser.parse(data);

To my dismay it returns a JSONNumber !!

Now I looked in the source code (JSONParser.java: parse, evaluate &
createObject) and I created the following test program.

....
String data = "new $wnd.Date(2010,0,28,17,8,48,0)";
myParser(data);
...

  private native void myParser(String data) /*-{
    var x = eval('(' + data + ')');
    $wnd.alert(" typeof = " + typeof x + ", valueOf = " +
x.valueOf());

  }-*/;


The alert displays typeof = object and in valueOf = 1264727328000.

Because typeof = object the execution flow calls createObject:

private static native JSONValue createObject(Object o) /*-{
    if (!o) {
      return @com.google.gwt.json.client.JSONNull::getInstance()();
    }
    var v = o.valueOf ? o.valueOf() : o;
    if (v !== o) {
      // It was a primitive wrapper, unwrap it and try again.
      var func =
@com.google.gwt.json.client.JSONParser::typeMap[typeof v];
      return func ? func(v) :
@com.google.gwt.json.client.JSONParser::throwUnknownTypeException(Ljava/
lang/String;)(typeof v);
    } else if (o instanceof Array || o instanceof $wnd.Array) {
      // Looks like an Array; wrap as JSONArray.
      // NOTE: this test can fail for objects coming from a different
window,
      // but we know of no reliable tests to determine if something is
an Array
      // in all cases.
      return @com.google.gwt.json.client.JSONArray::new(Lcom/google/
gwt/core/client/JavaScriptObject;)(o);
    } else {
      // This is a basic JavaScript object; wrap as JSONObject.
      // Subobjects will be created on demand.
      return @com.google.gwt.json.client.JSONObject::new(Lcom/google/
gwt/core/client/JavaScriptObject;)(o);
    }
  }-*/;


Please note the line:

    var v = o.valueOf ? o.valueOf() : o;

What is the reason for doing this? It changes the type of object to
something that is not and that is a number.

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
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.

Reply via email to