Title: | Fast 'JSON', 'NDJSON' and 'GeoJSON' Parser and Generator |
---|---|
Description: | A fast 'JSON' parser, generator and validator which converts 'JSON', 'NDJSON' (Newline Delimited 'JSON') and 'GeoJSON' (Geographic 'JSON') data to/from R objects. The standard R data types are supported (e.g. logical, numeric, integer) with configurable handling of NULL and NA values. Data frames, atomic vectors and lists are all supported as data containers translated to/from 'JSON'. 'GeoJSON' data is read in as 'simple features' objects. This implementation wraps the 'yyjson' 'C' library which is available from <https://github.com/ibireme/yyjson>. |
Authors: | Mike Cheng [aut, cre, cph], Yao Yuan [aut, cph] (Author of bundled yyjson) |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.20.9001 |
Built: | 2024-11-07 12:23:46 UTC |
Source: | https://github.com/coolbutuseless/yyjsonr |
Options for reading in GeoJSON
opts_read_geojson( type = c("sf", "sfc"), property_promotion = c("string", "list"), property_promotion_lgl = c("integer", "string") )
opts_read_geojson( type = c("sf", "sfc"), property_promotion = c("string", "list"), property_promotion_lgl = c("integer", "string") )
type |
'sf' or 'sfc' |
property_promotion |
What is the most general container type to use when
properties differ across a FEATURECOLLECTION? E.g. if the property
exists both as a numeric and a string, should all values be promoted
to a 'string', or contained as different types in a 'list'.
Default: 'string' will behave like |
property_promotion_lgl |
when |
Named list of options specific to reading GeoJSON
# Create a set of options to use when reading geojson opts_read_geojson()
# Create a set of options to use when reading geojson opts_read_geojson()
Create named list of options for parsing R from JSON
opts_read_json( promote_num_to_string = FALSE, df_missing_list_elem = NULL, obj_of_arrs_to_df = TRUE, arr_of_objs_to_df = TRUE, str_specials = c("string", "special"), num_specials = c("special", "string"), int64 = c("string", "double", "bit64"), length1_array_asis = FALSE, yyjson_read_flag = 0L )
opts_read_json( promote_num_to_string = FALSE, df_missing_list_elem = NULL, obj_of_arrs_to_df = TRUE, arr_of_objs_to_df = TRUE, str_specials = c("string", "special"), num_specials = c("special", "string"), int64 = c("string", "double", "bit64"), length1_array_asis = FALSE, yyjson_read_flag = 0L )
promote_num_to_string |
Should numeric values be promoted to strings
when they occur within an array with other string values? Default: FALSE
means to keep numerics as numeric value and promote the container to
be a |
df_missing_list_elem |
R value to use when elements are missing in list columns in data.frames. Default: NULL |
obj_of_arrs_to_df |
logical. Should a named list of equal-length vectors be promoted to a data.frame? Default: TRUE. If FALSE, then result will be left as a list. |
arr_of_objs_to_df |
logical. Should an array or objects be promoted to a a data.frame? Default: TRUE. If FALSE, then results will be read as a list-of-lists. |
str_specials |
Should |
num_specials |
Should JSON strings 'NA'/'Inf'/'NaN' in a numeric context
be converted to the |
int64 |
how to encode large integers which do not fit into R's integer type. 'string' imports them as a character vector. 'double' will convert the integer to a double precision numeric value. 'bit64' will use the 'integer64' type from the 'bit64' package. Note that the 'integer64' type is a signed integer type, and a warning will be issued if JSON contains an unsigned integer which cannot be stored in this type. |
length1_array_asis |
logical. Should JSON arrays with length = 1 be
marked with class |
yyjson_read_flag |
integer vector of internal |
Named list of options for reading JSON
opts_read_json()
opts_read_json()
sf
object to GeoJSON
Currently no options available.
opts_write_geojson()
opts_write_geojson()
Named list of options specific to writing GeoJSON
# Create a set of options to use when writing geojson opts_write_geojson()
# Create a set of options to use when writing geojson opts_write_geojson()
Create named list of options for serializing R to JSON
opts_write_json( digits = -1, 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, yyjson_write_flag = 0L )
opts_write_json( digits = -1, 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, yyjson_write_flag = 0L )
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). |
pretty |
Logical value indicating if the created JSON string should have
whitespace for indentation and linebreaks. Default: FALSE.
Note: this option is equivalent to |
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 |
str_specials |
Should a special value of |
fast_numerics |
Does the user guarantee that there are no NA, NaN or Inf
values in the numeric vectors? Default: FALSE. If |
yyjson_write_flag |
integer vector corresponding to internal |
Named list of options for writing JSON
write_json_str(head(iris, 3), opts = opts_write_json(factor = 'integer'))
write_json_str(head(iris, 3), opts = opts_write_json(factor = 'integer'))
sf
objectLoad GeoJSON as sf
object
read_geojson_str(str, opts = list(), ..., json_opts = list()) read_geojson_file(filename, opts = list(), ..., json_opts = list())
read_geojson_str(str, opts = list(), ..., json_opts = list()) read_geojson_file(filename, opts = list(), ..., json_opts = list())
str |
Single string containing GeoJSON |
opts |
Named list of GeoJSON-specific options. Usually created
with |
... |
Any extra named options override those in GeoJSON-specific options
- |
json_opts |
Named list of vanilla JSON options as used by |
filename |
Filename |
sf
object
geojson_file <- system.file("geojson-example.json", package = 'yyjsonr') read_geojson_file(geojson_file)
geojson_file <- system.file("geojson-example.json", package = 'yyjsonr') read_geojson_file(geojson_file)
Currently, this is not very efficient as the entire contents of the connection are read into R as a string and then the JSON parsed from there.
read_json_conn(conn, opts = list(), ...)
read_json_conn(conn, opts = list(), ...)
conn |
connection object. e.g. |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
For plain text files it is faster to use
read_json_file()
.
R object
Other JSON Parsers:
read_json_file()
,
read_json_raw()
,
read_json_str()
,
read_ndjson_file()
,
read_ndjson_str()
if (interactive()) { read_json_conn(url("https://api.github.com/users/hadley/repos")) }
if (interactive()) { read_json_conn(url("https://api.github.com/users/hadley/repos")) }
Convert JSON to R
read_json_file(filename, opts = list(), ...)
read_json_file(filename, opts = list(), ...)
filename |
full path to text file containing JSON. |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
R object
Other JSON Parsers:
read_json_conn()
,
read_json_raw()
,
read_json_str()
,
read_ndjson_file()
,
read_ndjson_str()
tmp <- tempfile() write_json_file(head(iris, 3), tmp) read_json_file(tmp)
tmp <- tempfile() write_json_file(head(iris, 3), tmp) read_json_file(tmp)
Convert JSON in a raw vector to R
read_json_raw(raw_vec, opts = list(), ...)
read_json_raw(raw_vec, opts = list(), ...)
raw_vec |
raw vector |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
R object
Other JSON Parsers:
read_json_conn()
,
read_json_file()
,
read_json_str()
,
read_ndjson_file()
,
read_ndjson_str()
raw_str <- as.raw(utf8ToInt('[1, 2, 3, "four"]')) read_json_raw(raw_str)
raw_str <- as.raw(utf8ToInt('[1, 2, 3, "four"]')) read_json_raw(raw_str)
Convert JSON in a character string to R
read_json_str(str, opts = list(), ...)
read_json_str(str, opts = list(), ...)
str |
a single character string |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
R object
Other JSON Parsers:
read_json_conn()
,
read_json_file()
,
read_json_raw()
,
read_ndjson_file()
,
read_ndjson_str()
read_json_str("4294967297", opts = opts_read_json(int64 = 'string'))
read_json_str("4294967297", opts = opts_read_json(int64 = 'string'))
If reading as data.frame, each row of NDJSON becomes a row in the data.frame. If reading as a list, then each row becomes an element in the list.
read_ndjson_file( filename, type = c("df", "list"), nread = -1, nskip = 0, nprobe = 100, opts = list(), ... )
read_ndjson_file( filename, type = c("df", "list"), nread = -1, nskip = 0, nprobe = 100, opts = list(), ... )
filename |
Path to file containing NDJSON data. May e a vanilla text file or a gzipped file |
type |
The type of R object the JSON should be parsed into. Valid values are 'df' or 'list'. Default: 'df' (data.frame) |
nread |
Number of records to read. Default: -1 (reads all JSON strings) |
nskip |
Number of records to skip before starting to read. Default: 0 (skip no data) |
nprobe |
Number of lines to read to determine types for data.frame
columns. Default: 100. Use |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
If parsing NDJSON to a data.frame it is usually better if the json objects
are consistent from line-to-line. Type inference for the data.frame is done
during initialisation by reading through nprobe
lines. Warning: if
there is a type-mismatch further into the file than it is probed, then you
will get missing values in the data.frame, or JSON values not captured in
the R data.
No flattening of the namespace is done i.e. nested object remain nested.
NDJSON data read into R as list or data.frame depending
on 'type'
argument
Other JSON Parsers:
read_json_conn()
,
read_json_file()
,
read_json_raw()
,
read_json_str()
,
read_ndjson_str()
tmp <- tempfile() write_ndjson_file(head(mtcars), tmp) read_ndjson_file(tmp)
tmp <- tempfile() write_ndjson_file(head(mtcars), tmp) read_ndjson_file(tmp)
If reading as data.frame, each row of NDJSON becomes a row in the data.frame. If reading as a list, then each row becomes an element in the list.
read_ndjson_str( x, type = c("df", "list"), nread = -1, nskip = 0, nprobe = 100, opts = list(), ... )
read_ndjson_str( x, type = c("df", "list"), nread = -1, nskip = 0, nprobe = 100, opts = list(), ... )
x |
string containing NDJSON |
type |
The type of R object the JSON should be parsed into. Valid values are 'df' or 'list'. Default: 'df' (data.frame) |
nread |
Number of records to read. Default: -1 (reads all JSON strings) |
nskip |
Number of records to skip before starting to read. Default: 0 (skip no data) |
nprobe |
Number of lines to read to determine types for data.frame
columns. Default: 100. Use |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
If parsing NDJSON to a data.frame it is usually better if the json objects
are consistent from line-to-line. Type inference for the data.frame is done
during initialisation by reading through nprobe
lines. Warning: if
there is a type-mismatch further into the file than it is probed, then you
will get missing values in the data.frame, or JSON values not captured in
the R data.
No flattening of the namespace is done i.e. nested object remain nested.
NDJSON data read into R as list or data.frame depending
on 'type'
argument
Other JSON Parsers:
read_json_conn()
,
read_json_file()
,
read_json_raw()
,
read_json_str()
,
read_ndjson_file()
tmp <- tempfile() json <- write_ndjson_str(head(mtcars)) read_ndjson_str(json, type = 'list')
tmp <- tempfile() json <- write_ndjson_str(head(mtcars)) read_ndjson_str(json, type = 'list')
Validate JSON in file or string
validate_json_file(filename, verbose = FALSE, opts = list(), ...) validate_json_str(str, verbose = FALSE, opts = list(), ...)
validate_json_file(filename, verbose = FALSE, opts = list(), ...) validate_json_str(str, verbose = FALSE, opts = list(), ...)
filename |
path to file containing JSON |
verbose |
logical. If the JSON is not valid, should a warning be shown giving details? |
opts |
Named list of options for parsing. Usually created by |
... |
Other named options can be used to override any options in |
str |
character string containing JSON |
Logical value. TRUE if JSON validates as OK, otherwise FALSE
tmp <- tempfile() write_json_file(head(iris, 3), tmp) validate_json_file(tmp) str <- write_json_str(iris) validate_json_str(str)
tmp <- tempfile() write_json_file(head(iris, 3), tmp) validate_json_file(tmp) str <- write_json_str(iris) validate_json_str(str)
Write SF to GeoJSON string
write_geojson_str(x, opts = list(), ..., json_opts = list()) write_geojson_file(x, filename, opts = list(), ..., json_opts = list())
write_geojson_str(x, opts = list(), ..., json_opts = list()) write_geojson_file(x, filename, opts = list(), ..., json_opts = list())
x |
|
opts |
named list of options. Usually created with |
... |
any extra named options override those in |
json_opts |
Named list of vanilla JSON options as used by |
filename |
filename |
Character string containing GeoJSON, or NULL
if GeoJSON
written to file.
geojson_file <- system.file("geojson-example.json", package = 'yyjsonr') sf <- read_geojson_file(geojson_file) cat(write_geojson_str(sf, json_opts = opts_write_json(pretty = TRUE)))
geojson_file <- system.file("geojson-example.json", package = 'yyjsonr') sf <- read_geojson_file(geojson_file) cat(write_geojson_str(sf, json_opts = opts_write_json(pretty = TRUE)))
Convert R object to JSON file
write_json_file(x, filename, opts = list(), ...)
write_json_file(x, filename, opts = list(), ...)
x |
the object to be encoded |
filename |
filename |
opts |
Named list of serialization options. Usually created by |
... |
Other named options can be used to override any options in |
None
Other JSON Serializer:
write_json_str()
,
write_ndjson_file()
,
write_ndjson_str()
tmp <- tempfile() write_json_file(head(iris, 3), tmp) read_json_file(tmp)
tmp <- tempfile() write_json_file(head(iris, 3), tmp) read_json_file(tmp)
Convert R object to JSON string
write_json_str(x, opts = list(), ...)
write_json_str(x, opts = list(), ...)
x |
the object to be encoded |
opts |
Named list of serialization options. Usually created by |
... |
Other named options can be used to override any options in |
Single string containing JSON
Other JSON Serializer:
write_json_file()
,
write_ndjson_file()
,
write_ndjson_str()
write_json_str(head(iris, 3), pretty = TRUE)
write_json_str(head(iris, 3), pretty = TRUE)
For list
input, each element of the list is written as a single JSON string.
For data.frame
input, each row of the data.frame
is written
as aJSON string.
write_ndjson_file(x, filename, opts = list(), ...)
write_ndjson_file(x, filename, opts = list(), ...)
x |
|
filename |
JSON strings will be written to this file one-line-per-JSON string. |
opts |
Named list of serialization options. Usually created by |
... |
Other named options can be used to override any options in |
None
Other JSON Serializer:
write_json_file()
,
write_json_str()
,
write_ndjson_str()
tmp <- tempfile() write_ndjson_file(head(mtcars), tmp) read_ndjson_file(tmp)
tmp <- tempfile() write_ndjson_file(head(mtcars), tmp) read_ndjson_file(tmp)
For list
input, each element of the list is written as a single JSON string.
For data.frame
input, each row of the data.frame
is written
as aJSON string.
write_ndjson_str(x, opts = list(), ...)
write_ndjson_str(x, opts = list(), ...)
x |
|
opts |
Named list of serialization options. Usually created by |
... |
Other named options can be used to override any options in |
String containing multiple JSON strings separated by newlines.
Other JSON Serializer:
write_json_file()
,
write_json_str()
,
write_ndjson_file()
write_ndjson_str(head(mtcars))
write_ndjson_str(head(mtcars))
This is a list of integer values used for setting flags on the yyjson
code directly. This is an ADVANCED option and should be used with caution.
yyjson_read_flag
yyjson_read_flag
An object of class list
of length 9.
Some of these settings overlap and conflict with code needed to handle the translation of JSON values to R.
opts_read_json(yyjson_read_flag = c(yyjson_read_flag$x, yyjson_read_flag$y, ...))
Default option (RFC 8259 compliant):
Read positive integer as uint64_t.
Read negative integer as int64_t.
Read floating-point number as double with round-to-nearest mode.
Read integer which cannot fit in uint64_t or int64_t as double.
Report error if double number is infinity.
Report error if string contains invalid UTF-8 character or BOM.
Report error on trailing commas, comments, inf and nan literals.
Read the input data in-situ.
This option allows the reader to modify and use input data to store string
values, which can increase reading speed slightly.
The caller should hold the input data before free the document.
The input data must be padded by at least YYJSON_PADDING_SIZE
bytes.
For example: "[1,2]"
should be "[1,2]\0\0\0\0"
, input length should be 5.
Stop when done instead of issuing an error if there's additional content after a JSON document. This option may be used to parse small pieces of JSON in larger data, such as "NDJSON"
Allow single trailing comma at the end of an object or array,
such as "[1,2,3,]"
Allow C-style single line and multiple line comments (non-standard).
Allow inf/nan number and literal, case-insensitive, such as 1e999, NaN, inf, -Infinity (non-standard).
Read all numbers as raw strings (value with "YYJSON_TYPE_RAW" type), inf/nan literal is also read as raw with "ALLOW_INF_AND_NAN" flag.
Allow reading invalid unicode when parsing string values (non-standard). Invalid characters will be allowed to appear in the string values, but invalid escape sequences will still be reported as errors. This flag does not affect the performance of correctly encoded strings. WARNING: Strings in JSON values may contain incorrect encoding when this option is used, you need to handle these strings carefully to avoid security risks.
Read big numbers as raw strings. These big numbers include integers that cannot be represented by "int64_t" and "uint64_t", and floating-point numbers that cannot be represented by finite "double". The flag will be overridden by "YYJSON_READ_NUMBER_AS_RAW" flag.
read_json_str( '[12.3]', opts = opts_read_json(yyjson_read_flag = yyjson_read_flag$YYJSON_READ_ALLOW_TRAILING_COMMAS) )
read_json_str( '[12.3]', opts = opts_read_json(yyjson_read_flag = yyjson_read_flag$YYJSON_READ_ALLOW_TRAILING_COMMAS) )
Version number of 'yyjson' C library
yyjson_version()
yyjson_version()
yyjson_version()
yyjson_version()
This is a list of integer values used for setting flags on the yyjson
code directly. This is an ADVANCED option and should be used with caution.
yyjson_write_flag
yyjson_write_flag
An object of class list
of length 9.
Some of these settings overlap and conflict with code needed to handle the translation of JSON values to R.
opts_write_json(yyjson_write_flag = c(write_flag$x, write_flag$y, ...))
Default value.
Write JSON minify.
Report error on inf or nan number.
Report error on invalid UTF-8 string.
Do not escape unicode or slash.
Write JSON pretty with 4 space indent.
Escape unicode as uXXXX
, make the
output ASCII only.
Escape '/' as '\/'.
Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard).
Write inf and nan number as null literal.
This flag will override YYJSON_WRITE_ALLOW_INF_AND_NAN
flag.
Allow invalid unicode when encoding
string values (non-standard).
Invalid characters in string value will be copied byte by byte.
If YYJSON_WRITE_ESCAPE_UNICODE
flag is also set, invalid character will be
escaped as U+FFFD
(replacement character).
This flag does not affect the performance of correctly encoded strings.
Write JSON pretty with 2 space indent.
This flag will override YYJSON_WRITE_PRETTY
flag.
Adds a newline character at the end of the JSON. This can be helpful for text editors or NDJSON
write_json_str("hello/there", opts = opts_write_json( yyjson_write_flag = yyjson_write_flag$YYJSON_WRITE_ESCAPE_SLASHES ))
write_json_str("hello/there", opts = opts_write_json( yyjson_write_flag = yyjson_write_flag$YYJSON_WRITE_ESCAPE_SLASHES ))