Re: JSONParser bug ?
// It was a primitive wrapper, unwrap it and try again. It looks like javascript's date is a primitive wrapper to a number (maybe seconds from epoch?) Which makes sense as that's the only bit of data it should store. Maybe look at implementing Date with a JavaScriptObject that wraps the object. On Feb 4, 3:04 pm, Costa wrote: > 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 google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JSONParser bug ?
On Feb 4, 9:04 pm, Costa wrote: > 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. If you want to flag this behavior as a bug, then I'd rather say JSONParser should throw, because you're obviously not giving it JSON to parse. The day GWT switches to using native JSON.parse when available, it'll throw (or return null, I don't remember). -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
JSONParser bug ?
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 google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.