associative arrays: to sort or not to sort?

Mario Kroeplin kroeplin.d at googlemail.com
Wed Jul 21 13:33:47 PDT 2010


> >> the correct solution is to make the check order independent. For
> >> example:
> >> string s = CallReturningJSON();
> >> s = replace(s,`"a":23.54`, `X`);
> >> s = replace(s,`"b":0.0012`, `X`);
> >> s = replace(s,`{"nested":{X,X}}`, `X`);
> >> s = replace(s,`"goodbye":[true,"or",false,["test",42,X]]`, `X`);
> >> s = replace(s,`"array":[12,null,{}]`, `Y`);
> >> s = replace(s,"is\n\ngreat", `Z`);
> >> s = replace(s,`"json":Z`, `Y`);
> >> s = replace(s,`"hello":{Y,Y}`, `X`);
> >> assert(s == `{X,X}`);.
> >
> > But this is lengthy and seems to lack additional assert checks.
> >
> It's a little wordy but it has one line per things it is checking. And adjusting it to give more asserts is trivial.

The order of name/value pairs in JSON objects is as unspecified as the order of foreach iteration for associative arrays.
So, agreed, you would have to unittest a serialization against all permutations.
But then, JSON has a jew more unspecified gaps like "whitespace can be inserted between any pair of tokens".
Shall we rely on the fact that the implementation currently does not insert whitespace between tokens?
This would work for the unittest of std.json, but what's with the unittest of an implementation using std.json?
Or, shall we implement a tokenizer in the unittest to get rid of extra whitespace between tokens?

> > Until then, broken unit tests pass...
> >
> Spot on. The unittest is broken. Not the thing it's testing and not foreach.

Maybe, the thing under test is not broken.
But there seems to be something wrong with the thing when it forces you to examine the unspecified result of toString in a unittest.
I mean, it would have been easy for the author of the code to provide toCanonicalJSON, and a lot easier for users of the code.

And, of course, foreach is not broken.
But D removes a lot of unspecified gaps that make life hard in C or C++.
And you have to provide opCmp in order to put your own keys into an associative array, so why don't you get them out in order?

However, let's get things done: can the attached unittest be an acceptatble replacement for the broken one of std.json?
begin 644 json-unittest.d
M=6YI='1E<W0@>PH@("`@+R\@06X@;W9E<FQY('-I;7!L92!T97-T('-U:71E
M+"!I9B!I="!C86X@<&%R<V4 at 82!S97)I86QI>F5D('-T<FEN9R!A;F0*("`@
M("\O('1H96X@=7-E('1H92!R97-U;'1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5L;&\B.GLB87)R87DB.ELQ
M,BQN=6QL+'M]72PB:G-O;B(Z(FES(&=R96%T(GTL8`H@("`@("`@("`@("`@
M("`@("`@(&`B9V]O9&)Y92(Z6W1R=64L(F]R(BQF86QS92Q;(G1E<W0B+#0R
M+'LB;F5S=&5D(CI[(F$B.C(S+C4T+")B(CHP+C`P,3)]?5U=?6`I.PI]"@``
`
end


More information about the Digitalmars-d-learn mailing list