mirror of
https://github.com/bitwarden/server
synced 2025-12-25 12:43:14 +00:00
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
// FIXME: Update this file to be null safe and then delete the line below
|
|
#nullable disable
|
|
|
|
using System.Text.Json;
|
|
using Azure.Core.Serialization;
|
|
using Microsoft.Azure.Cosmos;
|
|
|
|
namespace Bit.Core.Utilities;
|
|
|
|
// ref: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs
|
|
public class SystemTextJsonCosmosSerializer : CosmosSerializer
|
|
{
|
|
private readonly JsonObjectSerializer _systemTextJsonSerializer;
|
|
|
|
public SystemTextJsonCosmosSerializer(JsonSerializerOptions jsonSerializerOptions)
|
|
{
|
|
_systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
|
|
}
|
|
|
|
public override T FromStream<T>(Stream stream)
|
|
{
|
|
using (stream)
|
|
{
|
|
if (stream.CanSeek && stream.Length == 0)
|
|
{
|
|
return default;
|
|
}
|
|
if (typeof(Stream).IsAssignableFrom(typeof(T)))
|
|
{
|
|
return (T)(object)stream;
|
|
}
|
|
return (T)_systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
|
|
}
|
|
}
|
|
|
|
public override Stream ToStream<T>(T input)
|
|
{
|
|
var streamPayload = new MemoryStream();
|
|
_systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
|
|
streamPayload.Position = 0;
|
|
return streamPayload;
|
|
}
|
|
}
|