Casting JSONValues arrays to native arrays ... ??? ...
james.p.leblanc
james.p.leblanc at gmail.com
Thu Sep 23 18:20:06 UTC 2021
Dear D-ers,
In attempting to cast JSONValues that hold arrays to "native"
array types,
I have hit some issues. Example code:
```d
import std.stdio;
import std.json;
void main(){
JSONValue jj;
jj["d"] = [ 1.234 ]; // a "dummy" double value
jj["ba"] = [ true, false, true]; // "ba" boolean array
writeln("typeid(jj): ", typeid(jj), ", jj: ", jj );
// various things that I thought might work, but do NOT
auto z1 = cast(bool) jj["ba"]; // attempt #1
auto z2 = cast(bool[]) jj["ba"]; // attempt #2
auto z3 = cast(bool) jj["ba"].array; // attempt #3
auto z4 = cast(bool[]) jj["ba"].array; // attempt #4
writeln("typeid(z4): ", typeid(z4), ", z4: ", z4 );
}
```
Attempts 1,2, and 3 yield compilation errors (which I somewhat
understand):
question.d(12): Error: cannot cast expression `jj.opIndex("ba")`
of type `JSONValue` to `bool`
question.d(13): Error: cannot cast expression `jj.opIndex("ba")`
of type `JSONValue` to `bool[]`
question.d(14): Error: cannot cast expression
`jj.opIndex("ba").array()` of type `JSONValue[]` to `bool`
However, if I comment out the offending attempts (1, 2, and 3),
then it
compiles, and can run ... but produces a result which I very much
do NOT
understand:
typeid(jj): std.json.JSONValue, jj:
{"ba":[true,false,true],"d":[1.23399999999999999]}
typeid(z4): bool[], z4: [false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, true, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, true,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, true, false, false, false,
false, false, false, false]
Hmmmm... is there a standard way to push these JSONValues into
nice native
array types? (The real code is eventually going to be using
traits and mixins
... but I do not think this should pose additional problems).
All help and illumination thankfully received.
Best Regards,
James
More information about the Digitalmars-d-learn
mailing list