taepper commented on code in PR #50725:
URL: https://github.com/apache/arrow/pull/50725#discussion_r3684333999


##########
cpp/src/arrow/json/json_writer_internal.cc:
##########
@@ -96,6 +98,117 @@ void JsonWriter::Double(double value) {
   needs_comma_ = true;
 }
 
+Status JsonWriter::WriteValue(sj::value value) {
+  sj::json_type type;
+  if (auto error = value.type().get(type); error != simdjson::SUCCESS) {
+    return Status::Invalid(simdjson::error_message(error));
+  }
+
+  switch (type) {

Review Comment:
   Hmm, we first determine the type and then switch on it. All subsequent 
`.get()` calls in the cases will succeed, but we still have to deal with the 
cumbersome result logic in every case statement. This will be a pattern that 
will certainly appear multiple times and we might consider adding a 
visitor-like pattern for dispatching on sj::value::type()?



##########
cpp/src/arrow/json/json_writer_internal_test.cc:
##########
@@ -16,9 +16,11 @@
 // under the License.
 
 #include <gtest/gtest.h>
+#include "arrow/testing/gtest_util.h"

Review Comment:
   This include should move down to `arrow` includes



##########
cpp/src/arrow/json/json_writer_internal.cc:
##########
@@ -96,6 +98,117 @@ void JsonWriter::Double(double value) {
   needs_comma_ = true;
 }
 
+Status JsonWriter::WriteValue(sj::value value) {
+  sj::json_type type;
+  if (auto error = value.type().get(type); error != simdjson::SUCCESS) {
+    return Status::Invalid(simdjson::error_message(error));
+  }
+
+  switch (type) {
+    case sj::json_type::object: {
+      StartObject();
+
+      sj::object object;
+      if (auto error = value.get_object().get(object); error != 
simdjson::SUCCESS) {
+        return Status::Invalid(simdjson::error_message(error));
+      }
+
+      for (auto field : object) {
+        std::string_view key;
+        if (auto error = field.unescaped_key().get(key); error != 
simdjson::SUCCESS) {
+          return Status::Invalid(simdjson::error_message(error));
+        }
+
+        Key(key);
+
+        sj::value field_value;
+        if (auto error = field.value().get(field_value); error != 
simdjson::SUCCESS) {
+          return Status::Invalid(simdjson::error_message(error));
+        }
+
+        RETURN_NOT_OK(WriteValue(field_value));
+      }
+
+      EndObject();
+      break;
+    }
+
+    case sj::json_type::array: {
+      StartArray();
+
+      sj::array array;
+      if (auto error = value.get_array().get(array); error != 
simdjson::SUCCESS) {
+        return Status::Invalid(simdjson::error_message(error));
+      }
+
+      for (auto element : array) {
+        sj::value element_value;
+        if (auto error = element.get(element_value); error != 
simdjson::SUCCESS) {
+          return Status::Invalid(simdjson::error_message(error));
+        }
+
+        RETURN_NOT_OK(WriteValue(element_value));
+      }
+
+      EndArray();
+      break;
+    }
+
+    case sj::json_type::string: {
+      std::string_view string_value;
+      if (auto error = value.get_string().get(string_value); error != 
simdjson::SUCCESS) {
+        return Status::Invalid(simdjson::error_message(error));
+      }
+
+      String(string_value);
+      break;
+    }
+
+    case sj::json_type::boolean: {
+      bool bool_value;
+      if (auto error = value.get_bool().get(bool_value); error != 
simdjson::SUCCESS) {
+        return Status::Invalid(simdjson::error_message(error));
+      }
+
+      Bool(bool_value);
+      break;
+    }
+
+    case sj::json_type::null: {
+      Null();
+      break;
+    }
+
+    case sj::json_type::number: {
+      int64_t int_value;
+      if (value.get_int64().get(int_value) == simdjson::SUCCESS) {
+        Int64(int_value);
+        break;
+      }
+
+      uint64_t uint_value;
+      if (value.get_uint64().get(uint_value) == simdjson::SUCCESS) {
+        Uint64(uint_value);
+        break;
+      }
+
+      double double_value;
+      if (value.get_double().get(double_value) == simdjson::SUCCESS) {
+        Double(double_value);
+        break;
+      }
+

Review Comment:
   It should be faster (and intended by the library designers) to first call:
   ```cpp
   sj::number number;
   value.get_number(number)...;
   ```
   
   then we can dispatch on `number.get_number_type()` (which cannot fail)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to