Skip to content

Commit 0db51c9

Browse files
committed
Remove jsonlite dependency
1 parent 0c5318a commit 0db51c9

4 files changed

Lines changed: 31 additions & 17 deletions

File tree

DESCRIPTION

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ VignetteBuilder: quarto
2626
Config/testthat/edition: 3
2727
URL: https://hugogruson.fr/grumpy/, https://github.com/Bisaloo/grumpy
2828
BugReports: https://github.com/Bisaloo/grumpy/issues
29-
Imports:
30-
jsonlite
3129
Config/Needs/website:
3230
Huber-group-EMBL/Rarr
3331
Config/roxygen2/version: 8.0.0

R/read_npy.R

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ parse_npy_descr <- function(bytes) {
8888
# We just ignore it, at least for now.
8989
header <- bytes |>
9090
rawToChar() |>
91-
convert_py_dict_to_json() |>
92-
jsonlite::fromJSON(simplifyMatrix = FALSE)
91+
py_dict_to_r_list()
9392

9493
parsed_descr <- parse_npy_datatype(header$descr)
9594

@@ -251,13 +250,30 @@ convert_bytes_to_array <- function(bytes, what, shape, size, endian) {
251250
return(res)
252251
}
253252

254-
convert_py_dict_to_json <- function(dict_str) {
255-
dict_str |>
256-
gsub("'", '"', x = _, fixed = TRUE) |>
257-
gsub("None", "null", x = _, fixed = TRUE) |>
258-
gsub("True", "true", x = _, fixed = TRUE) |>
259-
gsub("False", "false", x = _, fixed = TRUE) |>
260-
gsub("(", "[", x = _, fixed = TRUE) |>
261-
gsub(")", "]", x = _, fixed = TRUE) |>
262-
gsub(",\\s*(}|\\])", "\\1", x = _) # trailing commas
253+
py_dict_to_r_list <- function(x) {
254+
x |>
255+
# Logicals
256+
gsub("\\bNone\\b", "NA", x = _) |>
257+
gsub("\\bTrue\\b", "TRUE", x = _) |>
258+
gsub("\\bFalse\\b", "FALSE", x = _) |>
259+
# Trailing commas (before closing brackets/braces)
260+
gsub(",\\s*([]})])", "\\1", x = _) |>
261+
# Dict (single-quoted) keys to list names.
262+
# Keys are composed of:
263+
# - anything that is not the quote itself or a backslash,
264+
# - a backslash followed by any character (to allow for escaped quotes).
265+
gsub("'(([^'\\\\]|\\\\.)*)'\\s*:", "'\\1' =", x = _) |>
266+
# Dict (double-quoted) keys to list names.
267+
gsub('"(([^"\\\\]|\\\\.)*)"\\s*:', '"\\1" =', x = _) |>
268+
# Tuples to c(...)
269+
gsub("(", "c(", x = _, fixed = TRUE) |>
270+
# Lists to list(...)
271+
gsub("[", "list(", x = _, fixed = TRUE) |>
272+
gsub("{", "list(", x = _, fixed = TRUE) |>
273+
# Closing brackets/braces
274+
gsub("]", ")", x = _, fixed = TRUE) |>
275+
gsub("}", ")", x = _, fixed = TRUE) |>
276+
# Evaluate the resulting R expression
277+
parse(text = _) |>
278+
eval()
263279
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ As a file format generated by a Python package, `.npy` files are prime
1818
candidates for using the `{reticulate}` package to read them into R.
1919
However, this comes with downsides in terms of performance, flexibility,
2020
and robustness of the R package infrastructure. `{grumpy}`, on the other
21-
hand, is a pure R package with a single dependency (`{jsonlite}`), and
22-
is performant, flexible. Overall, it is designed to be used deep in the
23-
dependency graph of other packages.
21+
hand, is a pure R package with no dependency and is performant,
22+
flexible. Overall, it is designed to be used deep in the dependency
23+
graph of other packages.
2424

2525
For more details on the motivation and design principles underpinning
2626
`{grumpy}`, see the dedicated vignette:

README.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The `{grumpy}` R package provides a way to read NumPy's `.npy` files into R. It
2727

2828
As a file format generated by a Python package, `.npy` files are prime candidates for using the `{reticulate}` package to read them into R.
2929
However, this comes with downsides in terms of performance, flexibility, and robustness of the R package infrastructure.
30-
`{grumpy}`, on the other hand, is a pure R package with a single dependency (`{jsonlite}`), and is performant, flexible. Overall, it is designed to be used deep in the dependency graph of other packages.
30+
`{grumpy}`, on the other hand, is a pure R package with no dependency and is performant, flexible. Overall, it is designed to be used deep in the dependency graph of other packages.
3131

3232
For more details on the motivation and design principles underpinning `{grumpy}`, see the dedicated vignette: `vignette("design", package = "grumpy")`.
3333

0 commit comments

Comments
 (0)