R version 4.5.0 alpha (2025-03-25 r88054 ucrt) Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(RJSONIO) > > roundTrip = + # compare can be all.equal + function(x, y = x, simplify = TRUE, asIs = NA, compare = identical) { + ans <- fromJSON(toJSON(x, asIs = asIs), simplify = simplify) + compare(ans, y) + } > > stopifnot(roundTrip(c(TRUE, FALSE))) > stopifnot(roundTrip(TRUE)) > try(stopifnot(roundTrip(1L))) # fails since 1L becomes a numeric Error : roundTrip(1L) is not TRUE > stopifnot(roundTrip(1)) > stopifnot(roundTrip("xyz")) > > stopifnot(roundTrip(c(TRUE, FALSE))) > stopifnot(roundTrip(1:2, as.numeric(1:2))) > stopifnot(roundTrip(c(1, 2, 3))) > stopifnot(roundTrip(c("abc", "xyz"))) > > # with names > stopifnot(roundTrip(c(a = TRUE))) > stopifnot(roundTrip(c(a = 1))) > stopifnot(roundTrip(c(a = "xyz"))) > stopifnot(roundTrip(c(a = 1L), c(a = 1))) > > > # lists > > stopifnot(roundTrip(list(1L), asIs = FALSE, simplify = FALSE, list(1))) > > # > stopifnot(roundTrip(list(1, 2), asIs = FALSE, simplify = FALSE, list(1, 2))) > stopifnot(roundTrip(list(1, 2), asIs = TRUE, simplify = FALSE, list(list(1), list(2)))) > > stopifnot(roundTrip(list(a= 1, b = 2), asIs = TRUE, simplify = FALSE, list(a = list(1), b = list(2)))) > > > # > tmp = list(a = 1, b = c(1, 2), c = list(1:3, x = c(TRUE, FALSE, FALSE), list(c("a", "b", "c", "d")))) > tmp1 = fromJSON(toJSON(tmp)) > all.equal(tmp, tmp1) [1] TRUE > > roundTrip(tmp, compare = all.equal) [1] TRUE > > proc.time() user system elapsed 0.18 0.12 0.31