Defines functions raw_to_json

# This function deals with some uncertainty in character encoding when reading
# from files and URLs. It tries UTF8 first, but falls back on native if it is
# certainly not UTF8.
raw_to_json <- function(x){
  txt <- rawToChar(x);
  Encoding(txt) <- "UTF-8"
  isvalid <- validate(txt)
  if(!isvalid && grepl("invalid bytes in UTF8", attr(isvalid, "err"), fixed=TRUE, useBytes=TRUE)){
    warning("The json string is not valid UTF-8. Assuming native encoding.", call. = FALSE)
    Encoding(txt) <- "";

Try the jsonlite package in your browser

Any scripts or data that you put into this service are public.

jsonlite documentation built on June 22, 2024, 11:05 a.m.