## ------------------------------------------------
## Method `jsonString$new`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$prettyPrint
jstring
jstring$prettyPrint <- FALSE
jstring
jstring <- "[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
jsonString$new(jstring)
## ------------------------------------------------
## Method `jsonString$print`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring
jstring$prettyPrint <- FALSE
jstring
## ------------------------------------------------
## Method `jsonString$asString`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
cat(jstring$asString())
cat(jstring$asString(pretty = TRUE))
## ------------------------------------------------
## Method `jsonString$at`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$at(1)
jstring$at(2, "x")
## ------------------------------------------------
## Method `jsonString$hasKey`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$hasKey("x")
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$hasKey("x")
## ------------------------------------------------
## Method `jsonString$keys`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$keys()
## ------------------------------------------------
## Method `jsonString$addProperty`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
ppty <- jsonString$new("[9, 99]")
jstring$addProperty("c", ppty)
jstring
jstring$addProperty("d", "null")
jstring
## ------------------------------------------------
## Method `jsonString$erase`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$erase("b")
jstring
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring$erase(2)
jstring
## ------------------------------------------------
## Method `jsonString$size`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$size()
## ------------------------------------------------
## Method `jsonString$update`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$update(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$merge`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$merge(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$patch`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jspatch <- "[
{\"op\": \"remove\", \"path\": \"/a\"},
{\"op\": \"replace\", \"path\": \"/b\", \"value\": null}
]"
jstring$patch(jspatch)
## ------------------------------------------------
## Method `jsonString$push`
## ------------------------------------------------
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring2 <- jsonString$new(
"{\"a\":[4,5,6],\"c\":\"goodbye\"}"
)
jstring$push(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$is`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$is("object")
jstring$is("array")
jstring <- jsonString$new("999")
jstring$is("integer")
jstring$is("number")
jstring$is("float")
## ------------------------------------------------
## Method `jsonString$type`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$type()
jstring <- jsonString$new("999")
jstring$type()
## ------------------------------------------------
## Method `jsonString$flatten`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":{\"x\":\"hello\",\"y\":\"hi\"}}"
)
jstring$flatten()
## ------------------------------------------------
## Method `jsonString$unflatten`
## ------------------------------------------------
folder <- system.file(package = "jsonStrings")
files <- list.files(folder, recursive = TRUE)
sizes <- file.size(file.path(folder, files))
files <- sprintf('"%s"', paste0("/", files))
string <- sprintf("{%s}", paste0(files, ":", sizes, collapse = ","))
jstring <- jsonString$new(string)
jstring$unflatten()
## ------------------------------------------------
## Method `jsonString$writeFile`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jsonfile <- tempfile(fileext = ".json")
jstring$writeFile(jsonfile)
cat(readLines(jsonfile), sep = "\n")
jsonString$new(jsonfile)
## ------------------------------------------------
## Method `jsonString$copy`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
copy <- jstring$copy()
copy$erase("b")
jstring
naive_copy <- jstring
naive_copy$erase("b")
jstring
Run the code above in your browser using DataLab