adamreeve commented on code in PR #275:
URL: https://github.com/apache/arrow-dotnet/pull/275#discussion_r2876675022


##########
src/Apache.Arrow.Variant/Json/VariantJsonWriter.cs:
##########
@@ -0,0 +1,260 @@
+// 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.
+
+using System;
+#if NET8_0_OR_GREATER
+using System.Buffers;
+#else
+using System.IO;
+#endif
+using System.Data.SqlTypes;
+using System.Text;
+using System.Text.Json;
+
+namespace Apache.Arrow.Variant.Json
+{
+    /// <summary>
+    /// Writes variant binary data directly to JSON without creating 
intermediate
+    /// <see cref="VariantValue"/> objects.
+    /// </summary>
+    public static class VariantJsonWriter
+    {
+        /// <summary>
+        /// Converts variant binary data to a JSON string.
+        /// </summary>
+        /// <param name="metadata">The variant metadata bytes.</param>
+        /// <param name="value">The variant value bytes.</param>
+        /// <param name="indented">Whether to produce indented 
(pretty-printed) JSON.</param>
+        /// <returns>The JSON string representation.</returns>
+        public static string ToJson(byte[] metadata, byte[] value, bool 
indented = false)
+        {
+            JsonWriterOptions writerOptions = new JsonWriterOptions { Indented 
= indented };
+#if NET8_0_OR_GREATER
+            ArrayBufferWriter<byte> buffer = new ArrayBufferWriter<byte>();
+            using (Utf8JsonWriter writer = new Utf8JsonWriter(buffer, 
writerOptions))
+            {
+                VariantReader reader = new VariantReader(metadata, value);
+                WriteReader(writer, reader);
+            }
+            return Encoding.UTF8.GetString(buffer.WrittenSpan);
+#else
+            using (MemoryStream ms = new MemoryStream())
+            {
+                using (Utf8JsonWriter writer = new Utf8JsonWriter(ms, 
writerOptions))
+                {
+                    VariantReader reader = new VariantReader(metadata, value);
+                    WriteReader(writer, reader);
+                }
+                return Encoding.UTF8.GetString(ms.GetBuffer(), 0, 
(int)ms.Position);

Review Comment:
   Should we use checked here and in `ToJson` to catch overflows?



##########
src/Apache.Arrow.Variant/VariantReader.cs:
##########
@@ -0,0 +1,686 @@
+// 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.
+
+using System;
+using System.Buffers.Binary;
+using System.Collections.Generic;
+using System.Data.SqlTypes;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace Apache.Arrow.Variant
+{
+    /// <summary>
+    /// Zero-copy reader for a single variant value. Provides type inspection
+    /// and typed accessors for primitives, short strings, objects, and arrays.
+    /// </summary>
+    public ref struct VariantReader
+    {
+        private readonly ReadOnlySpan<byte> _metadata;
+        private readonly ReadOnlySpan<byte> _value;
+
+        /// <summary>
+        /// Creates a reader over a variant value.
+        /// </summary>
+        /// <param name="metadata">The variant metadata buffer (shared across 
all values in a column).</param>
+        /// <param name="value">The variant value buffer for this specific 
value.</param>
+        public VariantReader(ReadOnlySpan<byte> metadata, ReadOnlySpan<byte> 
value)
+        {
+            _metadata = metadata;
+            _value = value;
+        }
+
+        /// <summary>Gets the full metadata span.</summary>
+        public ReadOnlySpan<byte> Metadata => _metadata;
+
+        /// <summary>Gets the full value span.</summary>
+        public ReadOnlySpan<byte> Value => _value;
+
+        // ---------------------------------------------------------------
+        // Type inspection
+        // ---------------------------------------------------------------
+
+        /// <summary>Gets the header byte of this value.</summary>
+        public byte Header => _value[0];
+
+        /// <summary>Gets the basic type of this value.</summary>
+        public VariantBasicType BasicType => 
VariantEncodingHelper.GetBasicType(Header);
+
+        /// <summary>
+        /// Gets the primitive type when <see cref="BasicType"/> is <see 
cref="VariantBasicType.Primitive"/>.
+        /// </summary>
+        public VariantPrimitiveType PrimitiveType => 
VariantEncodingHelper.GetPrimitiveType(Header);

Review Comment:
   Should throw InvalidOperationException if BasicType isn't Primitive?



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