1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18#' Read a JSON file
19#'
20#' Using [JsonTableReader]
21#'
22#' @inheritParams read_delim_arrow
23#' @param ... Additional options passed to `JsonTableReader$create()`
24#'
25#' @return A `data.frame`, or a Table if `as_data_frame = FALSE`.
26#' @export
27#' @examples
28#' \donttest{
29#'   tf <- tempfile()
30#'   on.exit(unlink(tf))
31#'   writeLines('
32#'     { "hello": 3.5, "world": false, "yo": "thing" }
33#'     { "hello": 3.25, "world": null }
34#'     { "hello": 0.0, "world": true, "yo": null }
35#'   ', tf, useBytes=TRUE)
36#'   df <- read_json_arrow(tf)
37#' }
38read_json_arrow <- function(file,
39                            col_select = NULL,
40                            as_data_frame = TRUE,
41                            ...) {
42  if (!inherits(file, "InputStream")) {
43    file <- make_readable_file(file)
44    on.exit(file$close())
45  }
46  tab <- JsonTableReader$create(file, ...)$Read()
47
48  col_select <- enquo(col_select)
49  if (!quo_is_null(col_select)) {
50    tab <- tab[vars_select(names(tab), !!col_select)]
51  }
52
53  if (isTRUE(as_data_frame)) {
54    tab <- as.data.frame(tab)
55  }
56  tab
57}
58
59#' @include arrow-package.R
60#' @rdname CsvTableReader
61#' @usage NULL
62#' @format NULL
63#' @docType class
64#' @export
65JsonTableReader <- R6Class("JsonTableReader", inherit = ArrowObject,
66  public = list(
67    Read = function() json___TableReader__Read(self)
68  )
69)
70JsonTableReader$create <- function(file,
71                                   read_options = JsonReadOptions$create(),
72                                   parse_options = JsonParseOptions$create(),
73                                   ...) {
74  assert_is(file, "InputStream")
75  json___TableReader__Make(file, read_options, parse_options)
76}
77
78#' @rdname CsvReadOptions
79#' @usage NULL
80#' @format NULL
81#' @docType class
82#' @export
83JsonReadOptions <- R6Class("JsonReadOptions", inherit = ArrowObject)
84JsonReadOptions$create <- function(use_threads = option_use_threads(), block_size = 1048576L) {
85  json___ReadOptions__initialize(use_threads, block_size)
86}
87
88#' @rdname CsvReadOptions
89#' @usage NULL
90#' @format NULL
91#' @docType class
92#' @export
93JsonParseOptions <- R6Class("JsonParseOptions", inherit = ArrowObject)
94JsonParseOptions$create <- function(newlines_in_values = FALSE) {
95  json___ParseOptions__initialize(newlines_in_values)
96}
97