{jsonlite}
jsonlite and yyjsonr may read and write some JSON differently due to varying assumptions, data configurations or option settings.
This document keeps a record of major differences to be aware of.
yyjsonr
3-d arrays are parsed as multiple 2-d
matrices and combined
In yyjsonr the order in which elements in an array are
serialized to JSON correspond to a JSON []
array of
row-major matrices in human-readable order.
jsonlite does things differently.
The array formats are internally consistent within each package, but not cross-compatible between them i.e. you cannot serialize an array in yyjsonr and re-create it exactly using jsonlite.
In the examples below, a simple 3d matrix is serialized with both
jsonlite
and yyjsonr
.
# A simple 3D array
mat <- array(1:12, dim = c(2,3,2))
mat
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12
# jsonlite's serialization of matrices is internally consistent and re-parses
# to the initial matrix.
str <- jsonlite::toJSON(mat, pretty = TRUE)
cat(str)
#> [
#> [
#> [1, 7],
#> [3, 9],
#> [5, 11]
#> ],
#> [
#> [2, 8],
#> [4, 10],
#> [6, 12]
#> ]
#> ]
jsonlite::fromJSON(str)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12
# yyjsonr's serialization of matrices is internally consistent and re-parses
# to the initial matrix.
# But note that it is *different* to what jsonlite does.
str <- yyjsonr::write_json_str(mat, pretty = TRUE)
cat(str)
#> [
#> [
#> [
#> 1,
#> 3,
#> 5
#> ],
#> [
#> 2,
#> 4,
#> 6
#> ]
#> ],
#> [
#> [
#> 7,
#> 9,
#> 11
#> ],
#> [
#> 8,
#> 10,
#> 12
#> ]
#> ]
#> ]
yyjsonr::read_json_str(str)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12