Bug in toJSON with lists containing vectors of length zero
@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.
Thanks. I see the problem and I'll think about how best to fix it.
This should be fixed now in the github source. We'll see if it breaks anything before pushing it onto CRAN. Thanks.
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.
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)