diff --git a/R/write_dataset_json.R b/R/write_dataset_json.R index 448764d..3d490a7 100644 --- a/R/write_dataset_json.R +++ b/R/write_dataset_json.R @@ -34,19 +34,27 @@ #' \dontrun{ #' write_dataset_json(ds_json, "path/to/file.json") #' } -write_dataset_json <- function(x, file, pretty=FALSE, float_as_decimals=FALSE, digits=16) { +write_dataset_json <- function( + x, + file, + pretty = FALSE, + float_as_decimals = FALSE, + digits = 16 +) { stopifnot_datasetjson(x) meta <- attributes(x) # Find all date, datetime and time columns and convert to character for (i in seq_along(meta$columns)) { - - y <- meta$columns[[i]] # Make sure metadata is compliant - if (y$dataType %in% c("date", "datetime", "time") & !("targetDataType" %in% names(y))) { + if ( + y$dataType %in% + c("date", "datetime", "time") & + !("targetDataType" %in% names(y)) + ) { if (!inherits(x[[y$name]], "character")) { stop_write_error( y$name, @@ -55,35 +63,57 @@ write_dataset_json <- function(x, file, pretty=FALSE, float_as_decimals=FALSE, d } } - if(y$dataType %in% c("date", "datetime", "time") & (!is.null(y$targetDataType) && y$targetDataType == "integer")) { + if ( + y$dataType %in% + c("date", "datetime", "time") & + (!is.null(y$targetDataType) && y$targetDataType == "integer") + ) { # Convert date if (y$dataType == "date") { - x[y$name] <- format(x[[y$name]], "%Y-%m-%d", tz='UTC') + x[y$name] <- format(x[[y$name]], "%Y-%m-%d", tz = 'UTC') } # Convert datetime if (y$dataType == "datetime") { # Ensure type and timezone is right. - if (!inherits(x[[y$name]], "POSIXt") || !("UTC" %in% attr(x[[y$name]], 'tzone'))){ - stop_write_error(y$name, "Date time variable must be provided as POSIXlt type with timezone set to UTC.") + if ( + !inherits(x[[y$name]], "POSIXt") || + !("UTC" %in% attr(x[[y$name]], 'tzone')) + ) { + stop_write_error( + y$name, + "Date time variable must be provided as POSIXlt type with timezone set to UTC." + ) } - x[y$name] <- strftime(x[[y$name]], "%Y-%m-%dT%H:%M:%S", tz='UTC') + x[y$name] <- strftime(x[[y$name]], "%Y-%m-%dT%H:%M:%S", tz = 'UTC') } # Convert time if (y$dataType == "time") { - if (y$dataType == "time" & !inherits(x[[y$name]], c("Period", "difftime", "ITime"))) { + if ( + y$dataType == "time" & + !inherits(x[[y$name]], c("Period", "difftime", "ITime")) + ) { stop_write_error( y$name, "If dataType is time and targetDataType is integer, the input variable type must be a lubridate Period, an hms difftime, or a data.table ITime object" ) } - x[y$name] <- strftime(as.numeric(x[[y$name]]), "%H:%M:%S", tz='UTC') + x[y$name] <- strftime( + as.POSIXlt( + as.numeric(x[[y$name]]), + tz = 'UTC', + origin = "1970-01-01" + ), + "%H:%M:%S", + ) } - } else if (float_as_decimals && y$dataType %in% c("float", 'double', 'decimal')) { + } else if ( + float_as_decimals && y$dataType %in% c("float", 'double', 'decimal') + ) { meta$columns[[i]]['dataType'] <- "decimal" meta$columns[[i]]['targetDataType'] <- "decimal" - x[y$name] <- format(x[y$name], digits=digits) + x[y$name] <- format(x[y$name], digits = digits) } } @@ -109,8 +139,8 @@ write_dataset_json <- function(x, file, pretty=FALSE, float_as_decimals=FALSE, d "records", "name", "label", - "columns") - ] + "columns" + )] temp <- remove_nulls(temp) @@ -119,8 +149,8 @@ write_dataset_json <- function(x, file, pretty=FALSE, float_as_decimals=FALSE, d if (!missing(file)) { # Make sure the output path exists - if(!dir.exists(dirname(file))) { - stop("Folder supplied to `file` does not exist", call.=FALSE) + if (!dir.exists(dirname(file))) { + stop("Folder supplied to `file` does not exist", call. = FALSE) } } @@ -146,12 +176,15 @@ write_dataset_json <- function(x, file, pretty=FALSE, float_as_decimals=FALSE, d } } -stop_write_error <- function(varname, msg){ +stop_write_error <- function(varname, msg) { stop( - sprintf(paste( - "Please check the variable %s.", - msg, - sep="\n "), - varname) + sprintf( + paste( + "Please check the variable %s.", + msg, + sep = "\n " + ), + varname + ) ) }