std.json API improvement - Request for code review
sybrandy
sybrandy at gmail.com
Tue Sep 14 14:48:25 PDT 2010
> Unfortunately, the above code is horribly broken. Here's how to read a
> number correctly:
>
> real x;
> if(json["vector"]["x"].type == JSON_TYPE.INTEGER) {
> x = json["vector"]["x"].integer;
> } else if(json["vector"]["x"].type == JSON_TYPE.FLOAT) {
> x = json["vector"]["x"].floating;
> } else {
> enforceEx!(JSONException)(false);
> }
>
> You'll notice that before any access you must check to ensure the JSON
> type is what you think it should be. As noted above, JSON does not
> differentiate between integers and reals, so you have to test both on
> access.
Understood. Even though this probably isn't the way it will end up
based on some previous discussion, I like the way the indices are used
to access the elements. Perhaps this means I'm more of a C guy than a D
guy in some respects. Definitely not a Java guy.
As for the integers vs. floats, does the API always treat a number as a
float even if it is an integer? If so, then checking for an integer vs.
a float may not be a big deal in many cases.
Casey
More information about the Digitalmars-d
mailing list