RJSONIO icon indicating copy to clipboard operation
RJSONIO copied to clipboard

Bug in toJSON with lists containing vectors of length zero

Open kevinushey opened this issue 11 years ago • 4 comments

@yihui and I are seeing the following behavior:

> cat( toJSON( list(x=1, y=character(0)) ) )
{
 "x":      1,
"y":      1 
}

This is because unlist is used to collect results, but because the y element is empty, it is dropped and then the element 1 is recycled.

kevinushey avatar May 03 '14 19:05 kevinushey

Thanks. I see the problem and I'll think about how best to fix it.

duncantl avatar May 03 '14 19:05 duncantl

This should be fixed now in the github source. We'll see if it breaks anything before pushing it onto CRAN. Thanks.

duncantl avatar May 03 '14 19:05 duncantl

I think problem still persists. For instance:

cat(RJSONIO::toJSON(list(C=as.numeric(NULL))))

produces:

{
 "C":  
}

Which is invalid json. I would expect:

{
 "C": null 
}

Other than this, excellent package.

apontejosea avatar Aug 09 '18 15:08 apontejosea

FWIW this only affects numeric vectors:

> cat(RJSONIO::toJSON(list(C = numeric(0))))
{
 "C":  
}> cat(RJSONIO::toJSON(list(C = integer(0))))
{
 "C": [] 
}> cat(RJSONIO::toJSON(list(C = character(0))))
{
 "C": [ ] 
}> cat(RJSONIO::toJSON(list(C = logical(0))))
{
 "C": [] 
}

(aside: some whitespace is emitted within the brackets for character vectors; not a showstopper but it'd be nice if empty vectors were handled consistently)

kevinushey avatar Aug 09 '18 15:08 kevinushey