Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/DataStax.AstraDB.DataApi/Core/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ internal string Serialize<T>(T input, JsonSerializerOptions serializeOptions = n
if (commandOptions.InputConverter != null)
{
serializeOptions.Converters.Add(commandOptions.InputConverter);
// For untyped Row serialization, add TableDictionaryConverter to handle nested dictionaries
if (commandOptions.InputConverter is SimpleDictionaryConverter)
{
serializeOptions.Converters.Add(new TableDictionaryConverter());
}
}
if (log)
{
Expand Down
14 changes: 11 additions & 3 deletions src/DataStax.AstraDB.DataApi/SerDes/SimpleDictionaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ private static void WriteValue(Utf8JsonWriter writer, object? value, JsonSeriali
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
var keyType = type.GetGenericArguments()[0];
if (keyType != typeof(string))
var dict = (System.Collections.IDictionary)value;

// Empty dictionaries remain as {}
if (dict.Count == 0)
{
writer.WriteStartObject();
writer.WriteEndObject();
}
else
{
// Non-empty: [[k1,v1],[k2,v2],...]
var valueType = type.GetGenericArguments()[1];
writer.WriteStartArray();
var dict = (System.Collections.IDictionary)value;
foreach (var key in dict.Keys)
{
writer.WriteStartArray();
Expand All @@ -117,8 +125,8 @@ private static void WriteValue(Utf8JsonWriter writer, object? value, JsonSeriali
writer.WriteEndArray();
}
writer.WriteEndArray();
return;
}
return;
}
}
JsonSerializer.Serialize(writer, value, options);
Expand Down
87 changes: 87 additions & 0 deletions src/DataStax.AstraDB.DataApi/SerDes/TableDictionaryConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright DataStax, Inc.
*
* Licensed 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.
*/

namespace DataStax.AstraDB.DataApi.SerDes;

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

/// <summary>
/// Converter factory for serializing dictionaries in table operations.
/// Non-empty dictionaries are serialized as [[k1,v1],[k2,v2],...] format.
/// Empty dictionaries are serialized as {}.
/// </summary>
internal class TableDictionaryConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
if (!typeToConvert.IsGenericType)
return false;

return typeToConvert.GetGenericTypeDefinition() == typeof(Dictionary<,>);
}

public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
Type keyType = typeToConvert.GetGenericArguments()[0];
Type valueType = typeToConvert.GetGenericArguments()[1];

JsonConverter converter = (JsonConverter)Activator.CreateInstance(
typeof(TableDictionaryConverterInner<,>).MakeGenericType(keyType, valueType),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: null,
culture: null)!;

return converter;
}

private class TableDictionaryConverterInner<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>>
{
public override Dictionary<TKey, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// For table operations, we primarily need write support
// Read support can be added if needed for deserialization
throw new NotImplementedException("TableDictionaryConverter is designed for serialization only");
}

public override void Write(Utf8JsonWriter writer, Dictionary<TKey, TValue> value, JsonSerializerOptions options)
{
if (value.Count == 0)
{
// Empty dictionaries remain as {}
writer.WriteStartObject();
writer.WriteEndObject();
}
else
{
// Non-empty dictionaries: [[k1,v1],[k2,v2],...]
writer.WriteStartArray();
foreach (var kvp in value)
{
writer.WriteStartArray();
JsonSerializer.Serialize(writer, kvp.Key, options);
JsonSerializer.Serialize(writer, kvp.Value, options);
writer.WriteEndArray();
}
writer.WriteEndArray();
}
}
}
}
5 changes: 5 additions & 0 deletions src/DataStax.AstraDB.DataApi/Tables/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,10 @@
/// <summary>
/// Find rows in the table.
///
/// The Find() methods return a <see cref="FindEnumerator{T,T,TableSortBuilder{T}}"/> object that can be used to further structure the query

Check warning on line 708 in src/DataStax.AstraDB.DataApi/Tables/Table.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has syntactically incorrect cref attribute 'FindEnumerator{T,T,TableSortBuilder{T}}'

Check warning on line 708 in src/DataStax.AstraDB.DataApi/Tables/Table.cs

View workflow job for this annotation

GitHub Actions / test

Type parameter declaration must be an identifier not a type. See also error CS0081.

Check warning on line 708 in src/DataStax.AstraDB.DataApi/Tables/Table.cs

View workflow job for this annotation

GitHub Actions / test

XML comment has syntactically incorrect cref attribute 'FindEnumerator{T,T,TableSortBuilder{T}}'
/// by adding Sort, Projection, Skip, Limit, etc. to affect the final results.
///
/// The <see cref="FindEnumerator{T,T,TableSortBuilder{T}}"/> object can be directly enumerated both synchronously and asynchronously.

Check warning on line 711 in src/DataStax.AstraDB.DataApi/Tables/Table.cs

View workflow job for this annotation

GitHub Actions / test

XML comment has syntactically incorrect cref attribute 'FindEnumerator{T,T,TableSortBuilder{T}}'
/// Secondarily, the results can be paged through manually by using the results of <see cref="FindEnumerator{T,T,TableSortBuilder{T}}.ToCursor()"/>.
/// </summary>
/// <returns></returns>
Expand Down Expand Up @@ -1057,6 +1057,11 @@
commandOptions.SerializeDateAsDollarDate = false;
if (typeof(TResult) == typeof(Row))
{
// Register SimpleDictionaryConverter for untyped Row serialization
if (isInsert && typeof(T) == typeof(Row))
{
commandOptions.InputConverter = new SimpleDictionaryConverter();
}
return commandOptions;
}
if (isInsert)
Expand Down
Loading