Wednesday, September 20, 2017

elemental.json confusion/error

Hi there

I recently started using elemental.json. But I recently stumbled upon strange (expected?) behaviour. Is there any good documentation? The javadoc is quiet lacking, at least in this regard. So, what am I confused about?

First, there is a "getNumber" (and it's equivalents), but those fail with a classcastexception, if you use it on parsed Json. There is no problem if you use it on values that were "put" there. Is that working as intended? 
Second, and I guess this is a bug: If you use get(key).asBoolean() on a recently parsed value, you get the correct value. But if you use it on a value "false" you "put" in, you get true. 

I would be happy if someone could shine some light on this for me :)

I wrote a small "test" to demonstrate it:

import com.google.gwt.core.shared.GWT;

import elemental.json.JsonObject;
import elemental.json.impl.JsonUtil;


public class ElementalJsonConfusion {
public static void testBoolean() {
String json = "{\"booleanValue\":false}";

JsonObject jsonValue = (JsonObject) JsonUtil.parse(json);
// output false
GWT.log("First asBoolean: " + jsonValue.get("booleanValue").asBoolean());

jsonValue.put("booleanValue", false);
// output true
GWT.log("Second asBoolean: " + jsonValue.get("booleanValue").asBoolean());

jsonValue = (JsonObject) JsonUtil.parse(json);
// output Exception
try {
GWT.log("First getBoolean: " + jsonValue.getBoolean("booleanValue"));
} catch (ClassCastException e) {
GWT.log("First getBoolean ClassCastException");
}
jsonValue.put("booleanValue", false);
// Output false
GWT.log("Second getBoolean: " + jsonValue.getBoolean("booleanValue"));
}



public static void testNumber() {
String json = "{\"numberValue\":123}";

JsonObject jsonValue = (JsonObject) JsonUtil.parse(json);

jsonValue = (JsonObject) JsonUtil.parse(json);
// output Exception
try {
GWT.log("First getNumber: " + jsonValue.getNumber("numberValue"));
} catch (ClassCastException e) {
GWT.log("First getNumber ClassCastException");
}
jsonValue.put("numberValue", 123);
// Output 123
GWT.log("Second getNumber: " + jsonValue.getNumber("numberValue"));
}
}


--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment