LP#1285915: document that perl2JSON doesn't order hash keys
authorGalen Charlton <gmc@esilibrary.com>
Wed, 2 Apr 2014 22:24:06 +0000 (15:24 -0700)
committerBill Erickson <berick@esilibrary.com>
Fri, 2 May 2014 18:12:39 +0000 (14:12 -0400)
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 <gmc@esilibrary.com>
Signed-off-by: Bill Erickson <berick@esilibrary.com>

src/perl/lib/OpenSRF/Utils/JSON.pm
src/perl/t/09-Utils-JSON.t

index a83e9d1..6411870 100644 (file)
@@ -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
 
index ce94012..ff5d580 100644 (file)
@@ -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)'
+);
 
 
 #