分类:
2011-04-13 11:53:21
$json = $json->sort_by($function_name) $json = $json->sort_by($subroutine_ref)
If $function_name or $subroutine_ref are set, its sort routine are used in encoding JSON objects.
$js = $pc->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b })->encode($obj); # is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|); $js = $pc->sort_by('own_sort')->encode($obj); # is($js, q|{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}|); sub JSON::PP::own_sort { $JSON::PP::a cmp $JSON::PP::b }
As the sorting routine runs in the JSON::PP scope, the given subroutine name and the special variables $a
, $b
will begin 'JSON::PP::'.