MattSizzle
MattSizzle

Reputation: 3175

CGI::Application::Plugin::JSON - json_body returns backwards

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.

Upvotes: 1

Views: 113

Answers (1)

ikegami
ikegami

Reputation: 386706

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

Upvotes: 1

Related Questions