Skip to content

Commit 899737d

Browse files
committed
Implement JSON writer
1 parent 28855e0 commit 899737d

File tree

12 files changed

+1095
-8
lines changed

12 files changed

+1095
-8
lines changed

cpp/src/arrow/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,8 @@ if(ARROW_JSON)
10001000
json/object_parser.cc
10011001
json/object_writer.cc
10021002
json/parser.cc
1003-
json/reader.cc)
1003+
json/reader.cc
1004+
json/writer.cc)
10041005
foreach(ARROW_JSON_TARGET ${ARROW_JSON_TARGETS})
10051006
target_link_libraries(${ARROW_JSON_TARGET} PRIVATE RapidJSON)
10061007
endforeach()

cpp/src/arrow/json/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_arrow_test(test
2323
from_string_test.cc
2424
parser_test.cc
2525
reader_test.cc
26+
writer_test.cc
2627
PREFIX
2728
"arrow-json"
2829
EXTRA_LINK_LIBS
@@ -33,6 +34,12 @@ add_arrow_benchmark(parser_benchmark
3334
"arrow-json"
3435
EXTRA_LINK_LIBS
3536
RapidJSON)
37+
38+
add_arrow_benchmark(writer_benchmark
39+
PREFIX
40+
"arrow-json"
41+
EXTRA_LINK_LIBS
42+
RapidJSON)
3643
arrow_install_all_headers("arrow/json")
3744

3845
# pkg-config support

cpp/src/arrow/json/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exc = executable(
2424
'from_string_test.cc',
2525
'parser_test.cc',
2626
'reader_test.cc',
27+
'writer_test.cc',
2728
],
2829
dependencies: [arrow_test_dep, rapidjson_dep],
2930
)
@@ -36,6 +37,13 @@ exc = executable(
3637
)
3738
benchmark('arrow-json-parser-benchmark', exc)
3839

40+
exc = executable(
41+
'arrow-json-writer-benchmark',
42+
sources: ['writer_benchmark.cc'],
43+
dependencies: [arrow_benchmark_dep, rapidjson_dep],
44+
)
45+
benchmark('arrow-json-writer-benchmark', exc)
46+
3947
install_headers(
4048
[
4149
'api.h',
@@ -51,6 +59,7 @@ install_headers(
5159
'reader.h',
5260
'test_common.h',
5361
'type_fwd.h',
62+
'writer.h',
5463
],
5564
subdir: 'arrow/json',
5665
)

cpp/src/arrow/json/options.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ ParseOptions ParseOptions::Defaults() { return ParseOptions(); }
2424

2525
ReadOptions ReadOptions::Defaults() { return ReadOptions(); }
2626

27+
WriteOptions WriteOptions::Defaults() { return WriteOptions(); }
28+
29+
Status WriteOptions::Validate() const {
30+
if (ARROW_PREDICT_FALSE(batch_size < 1)) {
31+
return Status::Invalid("WriteOptions: batch_size must be at least 1: ", batch_size);
32+
}
33+
return Status::OK();
34+
}
35+
2736
} // namespace json
2837
} // namespace arrow

cpp/src/arrow/json/options.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
#include <cstdint>
2121
#include <memory>
22+
#include <string>
2223

2324
#include "arrow/json/type_fwd.h"
25+
#include "arrow/status.h"
2426
#include "arrow/util/visibility.h"
2527

2628
namespace arrow {
@@ -70,5 +72,25 @@ struct ARROW_EXPORT ReadOptions {
7072
static ReadOptions Defaults();
7173
};
7274

75+
struct ARROW_EXPORT WriteOptions {
76+
/// \brief Maximum number of rows processed at a time
77+
///
78+
/// The JSON writer converts and writes data in batches of N rows.
79+
/// This number can impact performance.
80+
int32_t batch_size = 1024;
81+
82+
/// \brief Whether to emit null values in the JSON output
83+
///
84+
/// If true, null values are included as JSON null.
85+
/// If false, null values are omitted from the output entirely.
86+
bool emit_null = false;
87+
88+
/// Create write options with default values
89+
static WriteOptions Defaults();
90+
91+
/// \brief Test that all set options are valid
92+
Status Validate() const;
93+
};
94+
7395
} // namespace json
7496
} // namespace arrow

0 commit comments

Comments
 (0)