Create named list of options for serializing R to JSON
Usage
opts_write_json(
digits = -1L,
digits_secs = 0L,
digits_signif = -1L,
pretty = FALSE,
auto_unbox = FALSE,
dataframe = c("rows", "columns"),
factor = c("string", "integer"),
name_repair = c("none", "minimal"),
num_specials = c("null", "string"),
str_specials = c("null", "string"),
fast_numerics = FALSE,
json_verbatim = FALSE,
null = c("null", "empty_array"),
yyjson_write_flag = 0L
)Arguments
- digits
decimal places to keep for floating point numbers. Default: -1. Positive values specify number of decimal places. Using zero will write the numeric value as an integer. Values less than zero mean that the floating point value should be written as-is (the default). This argument is ignored if
digits_signifis greater than zero.- digits_secs
decimal places for fractional seconds when converting times to a string representation. Default: 0. Valid range: 0 to 6
- digits_signif
significant decimal places to store in floating point numbers. Default: -1 means to output the number as-is (while respecting the
digits) argument. Values above 0 will produce rounding to the given number of places and thedigitsargument will be ignored.- pretty
Logical value indicating if the created JSON string should have whitespace for indentation and linebreaks. Default: FALSE. Note: this option is equivalent to
yyjson_write_flag = write_flag$YYJSON_WRITE_PRETTY- auto_unbox
automatically unbox all atomic vectors of length 1 such that they appear as atomic elements in JSON rather than arrays of length 1.
- dataframe
how to encode data.frame objects. Options 'rows' or columns'. Default: 'rows'
- factor
how to encode factor objects: must be one of 'string' or 'integer' Default: 'string'
- name_repair
How should unnamed items in a partially named list be handled? 'none' means to leave their names blank in JSON (which may not be valid JSON). 'minimal' means to use the integer position index of the item as its name if it is missing. Default: 'none'
- num_specials
Should special numeric values (i.e. NA, NaN, Inf) be converted to a JSON
nullvalue or converted to a string representation e.g. "NA"/"NaN" etc. Default: 'null'- str_specials
Should a special value of
NAin a character vector be converted to a JSONnullvalue, or converted to a string "NA"? Default: 'null'- fast_numerics
Does the user guarantee that there are no NA, NaN or Inf values in the numeric vectors? Default: FALSE. If
TRUEthen numeric and integer vectors will be written to JSON using a faster method. Note: if there are NA, NaN or Inf values, an error will be thrown. Expert users are invited to also consider theYYJSON_WRITE_ALLOW_INF_AND_NANandYYJSON_WRITE_INF_AND_NAN_AS_NULLoptions foryyjson_write_flagsand should consult theyyjsonAPI documentation for further details.- json_verbatim
Write strings with class 'json' directly into the result? Default: FALSE. If
json_verbatim = TRUEand a string has the class"json", then it will be written verbatim into the output.- null
How should R
NULLvalues be written? Default: "null" means to write as a JSON "null" string. Other valid value is "empty_array".- yyjson_write_flag
integer vector corresponding to internal
yyjsonoptions. Seeyyjson_write_flagin this package, and read the yyjson API documentation for more information. This is considered an advanced option.
Examples
write_json_str(head(iris, 3), opts = opts_write_json(factor = 'integer'))
#> [1] "[{\"Sepal.Length\":5.1,\"Sepal.Width\":3.5,\"Petal.Length\":1.4,\"Petal.Width\":0.2,\"Species\":1},{\"Sepal.Length\":4.9,\"Sepal.Width\":3.0,\"Petal.Length\":1.4,\"Petal.Width\":0.2,\"Species\":1},{\"Sepal.Length\":4.7,\"Sepal.Width\":3.2,\"Petal.Length\":1.3,\"Petal.Width\":0.2,\"Species\":1}]"
