From 8fb4405a2768dd752743dab71d6e2e8a50ee6cd5 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Wed, 2 Apr 2014 15:24:06 -0700 Subject: [PATCH] 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 --- src/perl/lib/OpenSRF/Utils/JSON.pm | 3 ++- src/perl/t/09-Utils-JSON.t | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) 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)' +); # -- 2.43.2