Follow

Java/Android, WTF is wrong with you? (Yes, this test passes.)

@dbrgn I assume you use developer.android.com/referenc ?

It says: "When the requested type is a long, other Number types will be coerced using Number#longValue(). ..."

In Number: "The specific semantics of the conversion ... to a given primitive type is defined by the Number implementation in question."

Since the number you're trying to parse is larger than Long.MAX_VALUE, it seems the specific implementation returns Long.MAX_VALUE.

Maybe try o.get("a")? Doesn't really say if it returns an Number object.

@Neui if the value is too large to fit in a long internally, the android json implementation will parse it into a double. this means that silent truncating or clamping will happen when coercing it to a long. additionally, some integers are not representable as doubles.

in my opinion an API that does not allow detecting loss of precision or unrepresentable values is horrible.

right now I use a wrapper that calls getDouble, does range checking and only then calls getLong.

@Neui and by the way, the android JSON implementation is a "blind" re-implementation of the org.json API (due to licensing), but with subtly different behavior. code that runs on org.json may break or silently do the wrong thing when running it on android, and vice versa, even though the API is the compatible.

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!