Table of Contents

Class CosmosSystemTextJsonSerializer

Namespace
FoundationaLLM.Vectorization.Serializers
Assembly
FoundationaLLM.Vectorization.Engine.dll

This class provides a default implementation of System.Text.Json Cosmos Linq Serializer.

public class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
Inheritance
CosmosSystemTextJsonSerializer
Inherited Members
Extension Methods

Constructors

CosmosSystemTextJsonSerializer(JsonSerializerOptions)

Creates an instance of CosmosSystemTextJsonSerializer with the default values for the Cosmos SDK

public CosmosSystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)

Parameters

jsonSerializerOptions JsonSerializerOptions

An instance of JsonSerializerOptions containing the json serialization options.

Methods

FromStream<T>(Stream)

Convert a Stream of JSON to an object. The implementation is responsible for Disposing of the stream, including when an exception is thrown, to avoid memory leaks.

public override T FromStream<T>(Stream stream)

Parameters

stream Stream

The Stream response containing JSON from Cosmos DB.

Returns

T

The object deserialized from the stream.

Type Parameters

T

Any type passed to Container.

SerializeMemberName(MemberInfo)

Convert a MemberInfo to a string for use in LINQ query translation.

public override string? SerializeMemberName(MemberInfo memberInfo)

Parameters

memberInfo MemberInfo

Any MemberInfo used in the query.

Returns

string

A serialized representation of the member.

Remarks

Note that this is just a default implementation which handles the basic scenarios. Any JsonSerializerOptions passed in here are not going to be reflected in SerializeMemberName(). For example, if customers passed in a JsonSerializerOption such as below

JsonSerializerOptions options = new()
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}

This would not be honored by SerializeMemberName() unless it included special handling for this, for example.

public override string SerializeMemberName(MemberInfo memberInfo)
{
    JsonExtensionDataAttribute jsonExtensionDataAttribute =
        memberInfo.GetCustomAttribute<JsonExtensionDataAttribute>(true);
    if (jsonExtensionDataAttribute != null)
    {
        return null;
    }
    JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true);
    if (!string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name))
    {
        return jsonPropertyNameAttribute.Name;
    }
    return System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(memberInfo.Name);
}

To handle such scenarios, please create a custom serializer which inherits from the CosmosSystemTextJsonSerializer and overrides the SerializeMemberName to add any special handling.

ToStream<T>(T)

Convert the object to a Stream. The caller will take ownership of the stream and ensure it is correctly disposed of. Stream.CanRead must be true.

public override Stream ToStream<T>(T input)

Parameters

input T

Any type passed to Container.

Returns

Stream

A readable Stream containing JSON of the serialized object.

Type Parameters

T