RJSONIO icon indicating copy to clipboard operation
RJSONIO copied to clipboard

RJSONIO does not escape all control characters in object keys/value

Open alexmac opened this issue 8 years ago • 4 comments

Here's a testcase that shows string escapes working for dictionary values, but not for the keys:

cat(RJSONIO::toJSON(list("test\"escape" = "another\"escape")))
{
 "test"escape": "another\"escape" 
}

alexmac avatar Aug 14 '17 23:08 alexmac

Thanks. Can you tell me an case where this arises in the wild, please? I'm curious. Its an odd thing to do in a key. Always love to learn what people are up to.

duncantl avatar Aug 15 '17 01:08 duncantl

Fix committed - hopefully complete. Works for

library(RJSONIO)
cat(RJSONIO::toJSON(list("test\"escape" = "another\"escape")))
cat(RJSONIO::toJSON(c("test\"escape" = TRUE)))
cat(RJSONIO::toJSON(c("test\"escape" = 1L)))
cat(RJSONIO::toJSON(c("test\"escape" = pi)))
cat(RJSONIO::toJSON(c("test\"escape" = "another\"escape")))

duncantl avatar Aug 15 '17 01:08 duncantl

Data beyond our control from real world sources - there are more cases that need to be escaped (U+0000 through U+001F) per the spec here http://www.json.org/ otherwise it will produce invalid json output if given arbitrary strings as keys (quotes are just one example)

alexmac avatar Aug 16 '17 19:08 alexmac

Here's another example of breakage in both keys and values:

cat(RJSONIO::toJSON(list("interesting chars \u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F" = "interesting chars \u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F")))
{
 "interesting chars 


: "interesting chars \b\t\n
                           \f\r 
}>

alexmac avatar Aug 16 '17 19:08 alexmac