aaronolsen/StereoMorph

Use jsonlite

Ironholds opened this issue · 4 comments

If you're going to be converting strings into JSON, you might want to use the jsonlite package:

listToJSONStr(list(foo = "foo"))
[1] "{"foo":["foo"]}"

jsonlite::toJSON(list(foo = "foo"))
{"foo":["foo"]}

Apologies for the delay in responding. Could you please explain why you are suggesting use of 'jsonlite' over 'rjson'? When I install 'jsonlite' with "Install Dependencies" checked the packages 'R.rsp' and 'R.cache' are also installed. In contrast, when I install 'jsonlite' no other packages are installed. All else being equal I'd like to use the package that installs fewer packages on a user's system. Also, the speed tests reported here (https://rstudio-pubs-static.s3.amazonaws.com/31702_9c22e3d1a0c44968a4a1f9656f1800ab.html) show that 'rjson' is the fastest at generating and parsing json. So it's not clear that 'jsonlite' has a speed advantage over 'rjson'.

It's not about speed, it's about correctness; rjson doesn't map JSON data types to the R equivalent reliably - see https://arxiv.org/pdf/1403.2805.pdf or https://www.opencpu.org/posts/jsonlite-a-smarter-json-encoder/

Ah OK - thanks for the heads up. At this point I'm only converting nested lists with named components. It's not clear from these documents that rjson would incorrectly map anything for such an R object. I have not had any issues (so far) using rjson for the structures I'm converting. If I do encounter problems in the future (e.g. if I need to convert additional data types) then it seems like it would be worthwhile to switch to jsonlite. Thanks again.

That's fair!