Reconstructs R objects from JSON data that was created with to_mcp_json
.
Preserves type information including dates, factors, matrices, and other
special R types.
Details
This function reverses the conversion done by to_mcp_json
, reconstructing:
Special numeric values (Inf, -Inf, NaN)
Date and POSIXct objects with timezones
Factors with original levels
Matrices and arrays with dimensions
Data frames
S3 objects with class information
Complex numbers
Raw vectors from base64
Formulas and language objects
Note: Environments cannot be reconstructed and are replaced with marker objects.
Examples
# Simple JSON string
json_str <- '{"a": 1, "b": ["hello", "world"]}'
from_mcp_json(json_str)
#> $a
#> [1] 1
#>
#> $b
#> $b[[1]]
#> [1] "hello"
#>
#> $b[[2]]
#> [1] "world"
#>
#>
# Round-trip conversion
original <- list(
date = Sys.Date(),
values = c(1, 2, Inf),
factor = factor(c("a", "b", "a"))
)
json <- mcp_serialize(original)
reconstructed <- from_mcp_json(json)