Skip Menu |

This queue is for tickets about the JSON CPAN distribution.

Report information
The Basics
Id: 79368
Status: rejected
Priority: 0/
Queue: JSON

People
Owner: Nobody in particular
Requestors: smosin [...] mail.ru
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: Incorrect behaviour when some elements of array are missing
use strict; use strict; use warnings; use JSON; my $ta = []; # test array $ta->[0] = 0; #$ta->[1] = 1; # we skip this element so it doesn't exists $ta->[2] = undef; # but this element will exists with undefined value $ta->[3] = 3; for my $i (0 .. 3) { printf ("existence of element $i %s\n", exists($ta->[$i]) ); } print JSON->new->utf8(1)->encode( $ta ), "\n"; #### got existence of element 0 1 existence of element 1 existence of element 2 1 existence of element 3 1 [0,null,null,3] ### IMHO expected existence of element 0 1 existence of element 1 existence of element 2 1 existence of element 3 1 [0,,null,3] ########### Undefined and missing elements are not the same
It works correctly. Your undefined $ta->[1] is undef. So it is encoded to "null". On 2012-9月-03 月 09:00:10, smosin wrote: Show quoted text
> use strict; > use strict; > use warnings; > use JSON; > > my $ta = []; # test array > > $ta->[0] = 0; > #$ta->[1] = 1; # we skip this element so it doesn't exists > $ta->[2] = undef; # but this element will exists with undefined value > $ta->[3] = 3; > > for my $i (0 .. 3) { > printf ("existence of element $i %s\n", exists($ta->[$i]) ); > } > > print JSON->new->utf8(1)->encode( $ta ), "\n"; > #### > got > existence of element 0 1 > existence of element 1 > existence of element 2 1 > existence of element 3 1 > [0,null,null,3] > ### IMHO expected > existence of element 0 1 > existence of element 1 > existence of element 2 1 > existence of element 3 1 > [0,,null,3] > ########### > Undefined and missing elements are not the same
From: smosin [...] mail.ru
Sorry, undefined is $t->[2] while $t->[1] just doesn' exists Втр Сен 04 05:35:20 2012, MAKAMAKA писал: Show quoted text
> It works correctly. > Your undefined $ta->[1] is undef. > So it is encoded to "null". > > > On 2012-9月-03 月 09:00:10, smosin wrote:
> > use strict; > > use strict; > > use warnings; > > use JSON; > > > > my $ta = []; # test array > > > > $ta->[0] = 0; > > #$ta->[1] = 1; # we skip this element so it doesn't exists > > $ta->[2] = undef; # but this element will exists with undefined
value Show quoted text
> > $ta->[3] = 3; > > > > for my $i (0 .. 3) { > > printf ("existence of element $i %s\n", exists($ta->[$i]) ); > > } > > > > print JSON->new->utf8(1)->encode( $ta ), "\n"; > > #### > > got > > existence of element 0 1 > > existence of element 1 > > existence of element 2 1 > > existence of element 3 1 > > [0,null,null,3] > > ### IMHO expected > > existence of element 0 1 > > existence of element 1 > > existence of element 2 1 > > existence of element 3 1 > > [0,,null,3] > > ########### > > Undefined and missing elements are not the same
> >
On Tue Sep 04 08:04:30 2012, smosin wrote: Show quoted text
> Sorry, > undefined is $t->[2] while $t->[1] just doesn' exists
Whereas both Perl and JavaScript support sparse arrays, JSON as defined by RFC 4627 does not.
From: smosin [...] mail.ru
I was wrong. You are absolutely right. In firefox JSON.stringify([0,1,2,,4,5]) gives [0,1,2,null,4,5] while JSON.parse('[0,1,2,,4,5]') gives an error On Tue Sep 04 08:57:53 2012, SPROUT wrote: Show quoted text
> On Tue Sep 04 08:04:30 2012, smosin wrote:
> > Sorry, > > undefined is $t->[2] while $t->[1] just doesn' exists
> > Whereas both Perl and JavaScript support sparse arrays, JSON as > defined by RFC 4627 does not.
Marked as rejected. Thanks.