jeroen/jsonlite

toJSON(auto_unbox=TRUE) can generate invalid JSON

Closed this issue · 1 comments

wch commented

I just switched Shiny to jsonlite and in some rare test cases ran into this bug. (We previously switched to jsonlite but backed it out for a maintenance release.)

toJSON('foo', auto_unbox=TRUE)
# "foo"

I think it makes sense to special-case this so that it returns ["foo"].

After off-list discussion we decided to keep things the way they are.

When calling toJSON on a primitive with auto_unbox = TRUE, you get what you ask for which is simply the serialized primitive. The JSON.stringify function in JavaScript shows the same behavior. Although this is technically not valid JSON, it is valid JavaScript and can be used as such.

To ensure that the output is always valid JSON, simply wrap your object in I() which does exactly this: toJSON(I(pi), auto_unbox = TRUE).