1

I was wondering if anyone knew why this return is backwards with CGI::Application::Plugin::JSON

sub {
    my ($self) = @_;

    my $q = $self->query;

    return $self->json_body({ result => '1', message => 'I should be AFTER result'} );

}

The Output is as follows:

{"message":"I should be AFTER result","result":"1"}

I would assume it would format the JSON left to right from the key/value pairs, and remembering it will be backwards is okay, but I have alot of returns to handle and the validation on the client-side is done with the 'result' value so if I am just missing something I would like to have it output just like it is input.

EDIT:

Also I just notices it is not returning a JSON Boolean type object as "result":"1" will deserialize as as sting object and not a JSON Boolean. Is there a way to have it output "result":1

Thanks for any help I can get with this one.

MattSizzle
  • 3,145
  • 1
  • 22
  • 42

1 Answers1

1

I would assume it would format the JSON left to right from the key/value pairs

You're confusing the list you assigned to the hash with the hash itself. Hashes don't have a left and a right; they have an array of linked lists.

You're getting the order in which the elements are found in the hash. You can't control that order as long as you use a hash.

If you really do need to have the fields in a specific order (which would be really weird), you could try using something that looks like a hash but remembers insertion order (like Tie::IxHash).

remembering it will be backwards is okay

Not only are they not "backwards", the order isn't even predictable.

$ perl -MJSON -E'say encode_json {a=>1,b=>2,c=>3} for 1..3'
{"b":2,"c":3,"a":1}
{"c":3,"a":1,"b":2}
{"a":1,"c":3,"b":2}

Is there a way to have it output "result":1

result => 1

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I figured it was due to the randomness of hash ordering and a another module just to do that for my OCDs sack is not worth it, but it was a stumper as it always returned the same way (100 iterations or so) so I was not sure if the Module was doing something weird with it. `Tie::IxHash` was exactly what I was looking for though and `result => 1` works prefect. Thanks so much, its time to stop coding and get back to the "real" world of Los Santos now.... – MattSizzle Nov 13 '13 at 05:00
  • 1
    (It's harder to trigger variations in order before 5.18) – ikegami Nov 13 '13 at 05:27