EnricoMi commented on code in PR #46180:
URL: https://github.com/apache/arrow/pull/46180#discussion_r2081150922


##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -381,31 +381,30 @@ void AssertDatumsApproxEqual(const Datum& expected, const 
Datum& actual, bool ve
 
 std::shared_ptr<Array> ArrayFromJSON(const std::shared_ptr<DataType>& type,
                                      std::string_view json) {
-  EXPECT_OK_AND_ASSIGN(auto out, ipc::internal::json::ArrayFromJSON(type, 
json));
+  EXPECT_OK_AND_ASSIGN(auto out, json::ArrayFromJSONString(type, json));
   return out;
 }
 
 std::shared_ptr<Array> DictArrayFromJSON(const std::shared_ptr<DataType>& type,
                                          std::string_view indices_json,
                                          std::string_view dictionary_json) {
   std::shared_ptr<Array> out;
-  ABORT_NOT_OK(
-      ipc::internal::json::DictArrayFromJSON(type, indices_json, 
dictionary_json, &out));
+  ABORT_NOT_OK(json::DictArrayFromJSONString(type, indices_json, 
dictionary_json, &out));
   return out;
 }
 
 std::shared_ptr<ChunkedArray> ChunkedArrayFromJSON(const 
std::shared_ptr<DataType>& type,
                                                    const 
std::vector<std::string>& json) {
   std::shared_ptr<ChunkedArray> out;
-  ABORT_NOT_OK(ipc::internal::json::ChunkedArrayFromJSON(type, json, &out));
+  ABORT_NOT_OK(json::ChunkedArrayFromJSONString(type, json, &out));
   return out;
 }
 
 std::shared_ptr<RecordBatch> RecordBatchFromJSON(const 
std::shared_ptr<Schema>& schema,
                                                  std::string_view json) {
   // Parse as a StructArray
   auto struct_type = struct_(schema->fields());
-  std::shared_ptr<Array> struct_array = ArrayFromJSON(struct_type, json);
+  std::shared_ptr<Array> struct_array = arrow::ArrayFromJSON(struct_type, 
json);

Review Comment:
   ```suggestion
     std::shared_ptr<Array> struct_array = 
json::ArrayFromJSONString(struct_type, json);
   ```



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -469,7 +467,7 @@ std::shared_ptr<Tensor> TensorFromJSON(const 
std::shared_ptr<DataType>& type,
                                        const std::vector<int64_t>& shape,
                                        const std::vector<int64_t>& strides,
                                        const std::vector<std::string>& 
dim_names) {
-  std::shared_ptr<Array> array = ArrayFromJSON(type, data);
+  std::shared_ptr<Array> array = arrow::ArrayFromJSON(type, data);

Review Comment:
   ```suggestion
     std::shared_ptr<Array> array = json::ArrayFromJSONString(type, data);
   ```



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -1020,19 +1018,19 @@ std::shared_ptr<Array> MakeComplex128(const 
std::shared_ptr<Array>& real,
 }
 
 std::shared_ptr<Array> ExampleUuid() {
-  auto arr = ArrayFromJSON(
+  auto arr = arrow::ArrayFromJSON(

Review Comment:
   ```suggestion
     auto arr = json::ArrayFromJSONString(
   ```



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -1020,19 +1018,19 @@ std::shared_ptr<Array> MakeComplex128(const 
std::shared_ptr<Array>& real,
 }
 
 std::shared_ptr<Array> ExampleUuid() {
-  auto arr = ArrayFromJSON(
+  auto arr = arrow::ArrayFromJSON(
       fixed_size_binary(16),
       "[null, \"abcdefghijklmno0\", \"abcdefghijklmno1\", 
\"abcdefghijklmno2\"]");
   return ExtensionType::WrapArray(uuid(), arr);
 }
 
 std::shared_ptr<Array> ExampleSmallint() {
-  auto arr = ArrayFromJSON(int16(), "[-32768, null, 1, 2, 3, 4, 32767]");
+  auto arr = arrow::ArrayFromJSON(int16(), "[-32768, null, 1, 2, 3, 4, 
32767]");

Review Comment:
   ```suggestion
     auto arr = json::ArrayFromJSONString(int16(), "[-32768, null, 1, 2, 3, 4, 
32767]");
   ```



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -1043,8 +1041,8 @@ std::shared_ptr<Array> ExampleDictExtension() {
 }

Review Comment:
   The `DictArrayFromJSON` in `ExampleDictExtension` above could also be moved 
to `json::DictArrayFromJSONString`.



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -1020,19 +1018,19 @@ std::shared_ptr<Array> MakeComplex128(const 
std::shared_ptr<Array>& real,
 }
 
 std::shared_ptr<Array> ExampleUuid() {
-  auto arr = ArrayFromJSON(
+  auto arr = arrow::ArrayFromJSON(
       fixed_size_binary(16),
       "[null, \"abcdefghijklmno0\", \"abcdefghijklmno1\", 
\"abcdefghijklmno2\"]");
   return ExtensionType::WrapArray(uuid(), arr);
 }
 
 std::shared_ptr<Array> ExampleSmallint() {
-  auto arr = ArrayFromJSON(int16(), "[-32768, null, 1, 2, 3, 4, 32767]");
+  auto arr = arrow::ArrayFromJSON(int16(), "[-32768, null, 1, 2, 3, 4, 
32767]");
   return ExtensionType::WrapArray(smallint(), arr);
 }
 
 std::shared_ptr<Array> ExampleTinyint() {
-  auto arr = ArrayFromJSON(int8(), "[-128, null, 1, 2, 3, 4, 127]");
+  auto arr = arrow::ArrayFromJSON(int8(), "[-128, null, 1, 2, 3, 4, 127]");

Review Comment:
   ```suggestion
     auto arr = json::ArrayFromJSONString(int8(), "[-128, null, 1, 2, 3, 4, 
127]");
   ```



##########
cpp/src/arrow/testing/gtest_util.cc:
##########
@@ -1043,8 +1041,8 @@ std::shared_ptr<Array> ExampleDictExtension() {
 }
 
 std::shared_ptr<Array> ExampleComplex128() {
-  auto arr = ArrayFromJSON(struct_({field("", float64()), field("", 
float64())}),
-                           "[[1.0, -2.5], null, [3.0, -4.5]]");
+  auto arr = arrow::ArrayFromJSON(struct_({field("", float64()), field("", 
float64())}),
+                                  "[[1.0, -2.5], null, [3.0, -4.5]]");

Review Comment:
   ```suggestion
     auto arr = json::ArrayFromJSONString(struct_({field("", float64()), 
field("", float64())}),
                                          "[[1.0, -2.5], null, [3.0, -4.5]]");
   ```



##########
cpp/examples/arrow/from_json_string_example.cc:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// This example shows how to use some of the *FromJSONString helpers.
+
+#include <iostream>
+#include <memory>
+
+#include <arrow/api.h>
+#include <arrow/array/array_base.h>
+#include <arrow/json/from_string.h>
+#include <arrow/status.h>
+
+using arrow::json::ArrayFromJSONString;
+using arrow::json::ChunkedArrayFromJSONString;
+using arrow::json::DictArrayFromJSONString;
+
+/**
+ * \brief Run Example
+ *
+ * ./debug/from-json-string-example
+ */
+arrow::Status RunExample() {
+  // Simple types
+  ARROW_ASSIGN_OR_RAISE(auto int32_array,
+                        ArrayFromJSONString(arrow::int32(), "[1, 2, 3]"));
+  ARROW_ASSIGN_OR_RAISE(auto float64_array,
+                        ArrayFromJSONString(arrow::float64(), "[4.0, 5.0, 
6.0]"));
+  ARROW_ASSIGN_OR_RAISE(auto bool_array,
+                        ArrayFromJSONString(arrow::boolean(), "[true, false, 
true]"));
+  ARROW_ASSIGN_OR_RAISE(
+      auto string_array,
+      ArrayFromJSONString(arrow::utf8(), R"(["Hello", "World", null])"));
+
+  // Timestamps can be created from string representations
+  ARROW_ASSIGN_OR_RAISE(
+      auto ts_array,
+      ArrayFromJSONString(timestamp(arrow::TimeUnit::SECOND),
+                          R"(["1970-01-01", 
"2000-02-29","3989-07-14","1900-02-28"])"));
+
+  // List, Map, Struct
+  ARROW_ASSIGN_OR_RAISE(
+      auto list_array,
+      ArrayFromJSONString(list(arrow::int64()),
+                          "[[null], [], null, [4, 5, 6, 7, 8], [2, 3]]"));
+  ARROW_ASSIGN_OR_RAISE(
+      auto map_array,
+      ArrayFromJSONString(map(arrow::utf8(), arrow::int32()),
+                          R"([[["joe", 0], ["mark", null]], null, [["cap", 
8]], []])"));
+  ARROW_ASSIGN_OR_RAISE(
+      auto struct_array,
+      ArrayFromJSONString(
+          arrow::struct_({field("one", arrow::int32()), field("two", 
arrow::int32())}),
+          "[[11, 22], null, [null, 33]]"));
+
+  // ChunkedArrayFromJSONString
+  std::shared_ptr<arrow::ChunkedArray> chunked_array;
+  ARROW_RETURN_NOT_OK(ChunkedArrayFromJSONString(
+      arrow::int32(), {R"([5, 10])", R"([null])", R"([16])"}, &chunked_array));

Review Comment:
   ```suggestion
         arrow::int32(), {"[5, 10]", "[null]", "[16]"}, &chunked_array));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to