File: raw_to_json.R

package info (click to toggle)
r-cran-jsonlite 1.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: ansic: 3,792; sh: 9; makefile: 6
file content (13 lines) | stat: -rw-r--r-- 520 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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) <- "";
  }
  return(txt)
}