/src/duckdb/extension/json/json_functions/json_pretty.cpp
Line | Count | Source |
1 | | #include "json_executors.hpp" |
2 | | |
3 | | namespace duckdb { |
4 | | |
5 | | //! Pretty Print a given JSON Document |
6 | 0 | optional<string_t> PrettyPrint(yyjson_val *val, yyjson_alc *alc, Vector &) { |
7 | 0 | D_ASSERT(alc); |
8 | 0 | size_t len_size_t; |
9 | 0 | auto data = yyjson_val_write_opts(val, JSONCommon::WRITE_PRETTY_FLAG, alc, &len_size_t, nullptr); |
10 | 0 | idx_t len = len_size_t; |
11 | 0 | return string_t(data, len); |
12 | 0 | } |
13 | | |
14 | 0 | static void PrettyPrintFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
15 | 0 | auto json_type = args.data[0].GetType(); |
16 | 0 | D_ASSERT(json_type == LogicalType::VARCHAR || json_type == LogicalType::JSON()); |
17 | |
|
18 | 0 | JSONExecutors::UnaryExecute<string_t>(args, state, result, PrettyPrint); |
19 | 0 | } |
20 | | |
21 | 7.23k | static void GetPrettyPrintFunctionInternal(ScalarFunctionSet &set, const LogicalType &json) { |
22 | 7.23k | set.AddFunction(ScalarFunction("json_pretty", {json}, LogicalType::VARCHAR, PrettyPrintFunction, nullptr, nullptr, |
23 | 7.23k | JSONFunctionLocalState::Init)); |
24 | 7.23k | } |
25 | | |
26 | 7.23k | ScalarFunctionSet JSONFunctions::GetPrettyPrintFunction() { |
27 | 7.23k | ScalarFunctionSet set("json_pretty"); |
28 | 7.23k | GetPrettyPrintFunctionInternal(set, LogicalType::JSON()); |
29 | 7.23k | return set; |
30 | 7.23k | } |
31 | | |
32 | | } // namespace duckdb |