-
Notifications
You must be signed in to change notification settings - Fork 9
/
README.Rmd
173 lines (112 loc) · 5.24 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
title: yyjsonr
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.height = 3,
out.width = "100%"
)
library(tidyr)
library(yyjsonr)
if (FALSE) {
covr::report(covr::package_coverage(
line_exclusions = list('src/yyjson.c', 'src/yyjson.h')
))
}
if (FALSE) {
pkgdown::build_site(override = list(destination = "../coolbutuseless.github.io/package/yyjsonr"))
}
# Makevars options to do some deep testing for CRAN
# Type conversions are sane
# PKG_FLAG=-Wconversion
# Pointer overflow checks i.e. dodgy pointer arithmetic
# PKG_CFLAGS+=-fsanitize=pointer-overflow -fsanitize-trap=pointer-overflow
# Then run in the debugger:
# R -d lldb
# run
# testthat::test_local()
```
# yyjsonr <img src="man/figures/logo.png" align="right" width = "20%"/>
<!-- badges: start -->
![](https://img.shields.io/badge/cool-useless-green.svg)
[![CRAN](http://www.r-pkg.org/badges/version/yyjsonr)](https://cran.r-project.org/package=yyjsonr)
[![R-CMD-check](https://github.com/coolbutuseless/yyjsonr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/yyjsonr/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
`{yyjsonr}` is a fast JSON parser/serializer, which converts R data to/from JSON.
In most cases it is around 2x to 10x faster than `{jsonlite}` at both reading and writing JSON.
It is a wrapper for the [`yyjson`](https://github.com/ibireme/yyjson) C library (v0.9.0 April 8, 2024).
`yyjson` is MIT licensed - see `LICENSE-yyjson.txt` in this package for more details -
`readLines(system.file('LICENSE-yyjson.txt', package = 'yyjsonr', mustWork = TRUE))`
### What's in the box
This package contains specialized functions for each type of operation (read/write/validate) and the
storage location of the JSON (string/file/raw vector/connection).
#### Vanilla JSON
| | string | file | raw | conn | options |
|----------|---------------------|----------------------|-----------------|------------------|----------------------|
| read | read_json_str() | read_json_file() | read_json_raw() | read_json_conn() | opts_read_json() |
| write | write_json_str() | write_json_file() | | | opts_write_json() |
| validate | validate_json_str() | validate_json_file() | | | |
#### NDJSON
| | string | file | raw | conn | options |
|----------|---------------------|----------------------|-----------------|------------------|----------------------|
| read | read_ndjson_str() | read_ndjson_file() | | | opts_read_json() |
| write | write_ndjson_str() | write_ndjson_file() | | | opts_write_json() |
#### GeoJSON
| | string | file | raw | conn | options |
|----------|---------------------|----------------------|-----------------|------------------|----------------------|
| read | read_geojson_str() | read_geojson_file() | | | opts_read_geojson() |
| write | write_geojson_str() | write_geojson_file() | | | opts_write_geojson() |
### Speed
In the following plots, bigger is better, with `yyjsonr` results in blue.
#### JSON
<img src="man/figures/benchmark-summary.png">
#### NDJSON
<img src="man/figures/benchmark-ndjson.png" width="75%">
#### GeoJSON
<img src="man/figures/benchmark-geojson.png" width="75%">
Note: Benchmarks were run on Apple M2 Mac. See files `man/benchmark/benchmark*.Rmd` for details.
## Installation
This package can be installed from CRAN
``` r
install.packages('yyjsonr')
```
You can install the latest development version from [GitHub](https://github.com/coolbutuseless/yyjsonr) with:
``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/yyjsonr')
```
Pre-built source/binary versions can also be installed from [R-universe](https://r-universe.dev)
``` r
install.packages('yyjsonr', repos = c('https://coolbutuseless.r-universe.dev', 'https://cloud.r-project.org'))
```
Simple JSON example
==============================================================================
```{r}
library(yyjsonr)
str <- write_json_str(head(iris, 3), pretty = TRUE)
cat(str)
read_json_str(str)
```
Simple GeoJSON example
==============================================================================
When parsing GeoJSON, `{yyjsonr}` returns an `sf` object.
```{r}
read_geojson_str(geojsonsf::geo_melbourne) |>
head() |>
sf:::print.sf()
```
## Limitations
* Some datatypes not currently supported. Please file an issue on GitHub if
these types are critical for you. Providing test cases also appreciated!:
* Complex numbers
* POSIXlt
* Matrices of POSIXct / Date
## Acknowledgements
* R Core for developing and maintaining the language.
* CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining
the repository