pitrou commented on code in PR #321:
URL: https://github.com/apache/arrow-nanoarrow/pull/321#discussion_r1403599114


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -42,6 +42,100 @@ namespace testing {
 /// \brief Writer for the Arrow integration testing JSON format
 class TestingJSONWriter {
  public:
+  /// \brief Write a schema to out
+  ///
+  /// Creates output like `{"fields": [...], "metadata": [...]}`.
+  ArrowErrorCode WriteSchema(std::ostream& out, const ArrowSchema* schema) {
+    // Make sure we have a struct
+    if (std::string(schema->format) != "+s") {
+      return EINVAL;
+    }
+
+    out << "{";
+
+    // Write fields
+    out << R"("fields": )";
+    if (schema->n_children == 0) {
+      out << "[]";
+    } else {
+      out << "[";
+      NANOARROW_RETURN_NOT_OK(WriteField(out, schema->children[0]));
+      for (int64_t i = 1; i < schema->n_children; i++) {
+        out << ", ";
+        NANOARROW_RETURN_NOT_OK(WriteField(out, schema->children[i]));
+      }
+      out << "]";
+    }
+
+    // Write metadata
+    out << R"(, "metadata": )";
+    NANOARROW_RETURN_NOT_OK(WriteMetadata(out, schema->metadata));
+
+    out << "}";
+    return NANOARROW_OK;
+  }
+
+  /// \brief Write a field to out
+  ///
+  /// Creates output like `{"name" : "col", "type": {...}, ...}`
+  ArrowErrorCode WriteField(std::ostream& out, const ArrowSchema* field) {
+    ArrowSchemaView view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&view, (ArrowSchema*)field, 
nullptr));
+
+    out << "{";
+
+    // Write schema->name (may be null)
+    if (field->name == nullptr) {
+      out << R"("name": null)";
+    } else {
+      out << R"("name": ")" << field->name << R"(")";

Review Comment:
   Ideally we should escape reserved characters in the field name...



-- 
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