CosmosLinqSerializer Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This abstract class can be implemented to allow a custom serializer (Non Json.NET serializer's) to be used by the CosmosClient for LINQ queries.
public abstract class CosmosLinqSerializer : Microsoft.Azure.Cosmos.CosmosSerializer
type CosmosLinqSerializer = class
inherit CosmosSerializer
Public MustInherit Class CosmosLinqSerializer
Inherits CosmosSerializer
- Inheritance
Examples
This example implements the CosmosLinqSerializer contract. This example custom serializer will honor System.Text.Json attributes.
class SystemTextJsonSerializer : CosmosLinqSerializer
{
private readonly JsonObjectSerializer systemTextJsonSerializer;
public SystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)
{
this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
}
public override T FromStream<T>(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
using (stream)
{
if (stream.CanSeek && stream.Length == 0)
{
return default;
}
if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)stream;
}
return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
}
}
public override Stream ToStream<T>(T input)
{
MemoryStream streamPayload = new MemoryStream();
this.systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
streamPayload.Position = 0;
return streamPayload;
}
public override string SerializeMemberName(MemberInfo memberInfo)
{
System.Text.Json.Serialization.JsonExtensionDataAttribute jsonExtensionDataAttribute =
memberInfo.GetCustomAttribute<System.Text.Json.Serialization.JsonExtensionDataAttribute>(true);
if (jsonExtensionDataAttribute != null)
{
return null;
}
JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true);
string memberName = !string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name)
? jsonPropertyNameAttribute.Name
: memberInfo.Name;
// Users must add handling for any additional attributes here
return memberName;
}
}
Constructors
CosmosLinqSerializer() |
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. (Inherited from CosmosSerializer) |
SerializeMemberName(MemberInfo) |
Convert a MemberInfo to a string for use in LINQ query translation. This must be implemented when using a custom serializer for LINQ queries. |
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. (Inherited from CosmosSerializer) |
Applies to
Azure SDK for .NET