From: Galen Charlton Date: Wed, 2 Apr 2014 22:24:06 +0000 (-0700) Subject: LP#1285915: document that perl2JSON doesn't order hash keys X-Git-Tag: osrf_rel_2_4_0-alpha~54 X-Git-Url: http://git.equinoxoli.org/?p=opensrf-equinox.git;a=commitdiff_plain;h=8fb4405a2768dd752743dab71d6e2e8a50ee6cd5 LP#1285915: document that perl2JSON doesn't order hash keys This patch documents that JSON strings returned by OpenSRF::Utils::JSON->perl2JSON() should not be expected to have hash keys sorted in any particular order. It also adjusts a corresponding test case to consistently pass under Perl 5.18, which introduces hash order randomization. Forcing JSON output to be in canonical form is another option, and easily done by JSON::XS, but would add overhead. Signed-off-by: Galen Charlton Signed-off-by: Bill Erickson --- diff --git a/src/perl/lib/OpenSRF/Utils/JSON.pm b/src/perl/lib/OpenSRF/Utils/JSON.pm index a83e9d1..6411870 100644 --- a/src/perl/lib/OpenSRF/Utils/JSON.pm +++ b/src/perl/lib/OpenSRF/Utils/JSON.pm @@ -80,7 +80,8 @@ sub JSON2perl { =head2 perl2JSON Given a Perl object, returns a JSON stringified representation of that -object. +object. Callers should not expect that the JSON string has hash keys +sorted in any particular order. =cut diff --git a/src/perl/t/09-Utils-JSON.t b/src/perl/t/09-Utils-JSON.t index ce94012..ff5d580 100644 --- a/src/perl/t/09-Utils-JSON.t +++ b/src/perl/t/09-Utils-JSON.t @@ -109,7 +109,11 @@ is_deeply ($jsonobj, { __c => 'osrfException', __p => { foo => 'bar' } }, # # perl2JSON my $jsonstr = OpenSRF::Utils::JSON->perl2JSON($fakeobj); -is ($jsonstr, '{"__c":"osrfException","__p":{"foo":"bar"}}'); +ok ( + ($jsonstr eq '{"__c":"osrfException","__p":{"foo":"bar"}}' || + $jsonstr eq '{"__p":{"foo":"bar"},"__c":"osrfException"}'), + 'JSON corresponds to Perl object (though hash key order by vary)' +); #