|
| 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 | +// This example shows how to use some of the *FromJSONString helpers. |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +#include <arrow/api.h> |
| 24 | +#include <arrow/array/array_base.h> |
| 25 | +#include <arrow/json/from_string.h> |
| 26 | +#include <arrow/status.h> |
| 27 | + |
| 28 | +using arrow::json::ArrayFromJSONString; |
| 29 | +using arrow::json::ChunkedArrayFromJSONString; |
| 30 | +using arrow::json::DictArrayFromJSONString; |
| 31 | + |
| 32 | +/** |
| 33 | + * \brief Run Example |
| 34 | + * |
| 35 | + * ./debug/from-json-string-example |
| 36 | + */ |
| 37 | +arrow::Status RunExample() { |
| 38 | + // Simple types |
| 39 | + ARROW_ASSIGN_OR_RAISE(auto int32_array, |
| 40 | + ArrayFromJSONString(arrow::int32(), "[1, 2, 3]")); |
| 41 | + ARROW_ASSIGN_OR_RAISE(auto float64_array, |
| 42 | + ArrayFromJSONString(arrow::float64(), "[4.0, 5.0, 6.0]")); |
| 43 | + ARROW_ASSIGN_OR_RAISE(auto bool_array, |
| 44 | + ArrayFromJSONString(arrow::boolean(), "[true, false, true]")); |
| 45 | + ARROW_ASSIGN_OR_RAISE( |
| 46 | + auto string_array, |
| 47 | + ArrayFromJSONString(arrow::utf8(), R"(["Hello", "World", null])")); |
| 48 | + |
| 49 | + // Timestamps can be created from string representations |
| 50 | + ARROW_ASSIGN_OR_RAISE( |
| 51 | + auto ts_array, |
| 52 | + ArrayFromJSONString(timestamp(arrow::TimeUnit::SECOND), |
| 53 | + R"(["1970-01-01", "2000-02-29","3989-07-14","1900-02-28"])")); |
| 54 | + |
| 55 | + // List, Map, Struct |
| 56 | + ARROW_ASSIGN_OR_RAISE( |
| 57 | + auto list_array, |
| 58 | + ArrayFromJSONString(list(arrow::int64()), |
| 59 | + "[[null], [], null, [4, 5, 6, 7, 8], [2, 3]]")); |
| 60 | + ARROW_ASSIGN_OR_RAISE( |
| 61 | + auto map_array, |
| 62 | + ArrayFromJSONString(map(arrow::utf8(), arrow::int32()), |
| 63 | + R"([[["joe", 0], ["mark", null]], null, [["cap", 8]], []])")); |
| 64 | + ARROW_ASSIGN_OR_RAISE( |
| 65 | + auto struct_array, |
| 66 | + ArrayFromJSONString( |
| 67 | + arrow::struct_({field("one", arrow::int32()), field("two", arrow::int32())}), |
| 68 | + "[[11, 22], null, [null, 33]]")); |
| 69 | + |
| 70 | + // ChunkedArrayFromJSONString |
| 71 | + std::shared_ptr<arrow::ChunkedArray> chunked_array; |
| 72 | + ARROW_RETURN_NOT_OK(ChunkedArrayFromJSONString( |
| 73 | + arrow::int32(), {"[5, 10]", "[null]", "[16]"}, &chunked_array)); |
| 74 | + |
| 75 | + // DictArrayFromJSONString |
| 76 | + std::shared_ptr<arrow::Array> dict_array; |
| 77 | + ARROW_RETURN_NOT_OK(DictArrayFromJSONString( |
| 78 | + dictionary(arrow::int32(), arrow::utf8()), "[0, 1, 0, 2, 0, 3]", |
| 79 | + R"(["k1", "k2", "k3", "k4"])", &dict_array)); |
| 80 | + |
| 81 | + return arrow::Status::OK(); |
| 82 | +} |
| 83 | + |
| 84 | +int main(int argc, char** argv) { |
| 85 | + auto status = RunExample(); |
| 86 | + if (!status.ok()) { |
| 87 | + std::cerr << status.ToString() << std::endl; |
| 88 | + return EXIT_FAILURE; |
| 89 | + } |
| 90 | + return EXIT_SUCCESS; |
| 91 | +} |
0 commit comments