File: serializers-json.R

package info (click to toggle)
r-cran-vcr 0.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,360 kB
  • sloc: cpp: 15; sh: 13; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# ww <- JSON$new(path = "stuff3")
# ww
# ww$file_extension
# fun <- ww$serialize()
# fun(list(http_interactions = list(response = list(body = "bar"))),
#   path = ww$path, bytes = FALSE)
# ww$deserialize()

#' @title The JSON serializer
#' @description class with methods for serializing via \pkg{jsonlite}
#' @keywords internal
JSON <- R6::R6Class("JSON",
  inherit = Serializer,
  public = list(
    #' @description Create a new `JSON` object
    #' @param path (character) full path to the yaml file
    #' @return A new `JSON` object
    initialize = function(path = NULL) {
      super$initialize(".json", path)
    },

    #' @description Serializes the given hash using internal fxn write_json
    #' @param x (list) the object to serialize
    #' @param path (character) the file path
    #' @param bytes (logical) whether to preserve exact body bytes or not
    #' @return (character) the json string to write to disk
    serialize = function(x, path, bytes) {
      function(x, path, bytes) {
        write_json(x, path, bytes)
      }
    },

    #' @description Deserializes the content at the file path using
    #' jsonlite::fromJSON
    #' @return (list) the deserialized object, an R list
    deserialize = function() {
      str <- sensitive_put_back(readLines(self$path))
      tmp <- jsonlite::fromJSON(str, FALSE)
      private$process_body(tmp)
    }
  )
)