/
dev-npgsql
/
npgsql
Обзор
Документация
Войти
/
dev-npgsql
/
npgsql
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v6.0.11
src/Npgsql.Json.NET/Internal/JsonbHandler.cs
100 строк
4 KB
Shay Rojansky
Apply file-scoped namespaces for 6.0 (#4298)
28 янв 2022, 15:13
Не верифицирован
28 янв 2022, 15:13
2e0ad2d
Код
Авторство
О чём код?
using System; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; using Npgsql.BackendMessages; using Npgsql.Internal; using Npgsql.Internal.TypeHandling; using Npgsql.PostgresTypes; namespace Npgsql.Json.NET.Internal; class JsonbHandler : Npgsql.Internal.TypeHandlers.JsonHandler { readonly JsonSerializerSettings _settings; public JsonbHandler(PostgresType postgresType, NpgsqlConnector connector, JsonSerializerSettings settings) : base(postgresType, connector.TextEncoding, isJsonb: true) => _settings = settings; protected override async ValueTask<T> ReadCustom<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null) { if (typeof(T) == typeof(string) || typeof(T) == typeof(char[]) || typeof(T) == typeof(ArraySegment<char>) || typeof(T) == typeof(char) || typeof(T) == typeof(byte[])) { return await base.ReadCustom<T>(buf, len, async, fieldDescription); } // JSON.NET returns null if no JSON content was found. This means null may get returned even if T is a non-nullable reference // type (for value types, an exception will be thrown). return JsonConvert.DeserializeObject<T>(await base.Read<string>(buf, len, async, fieldDescription), _settings)!; } protected override int ValidateAndGetLengthCustom<T2>([DisallowNull] T2 value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter) { if (typeof(T2) == typeof(string) || typeof(T2) == typeof(char[]) || typeof(T2) == typeof(ArraySegment<char>) || typeof(T2) == typeof(char) || typeof(T2) == typeof(byte[])) { return base.ValidateAndGetLengthCustom(value, ref lengthCache, parameter); } var serialized = JsonConvert.SerializeObject(value, _settings); if (parameter != null) parameter.ConvertedValue = serialized; return base.ValidateAndGetLengthCustom(serialized, ref lengthCache, parameter); } protected override Task WriteWithLengthCustom<T2>([DisallowNull] T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default) { if (typeof(T2) == typeof(string) || typeof(T2) == typeof(char[]) || typeof(T2) == typeof(ArraySegment<char>) || typeof(T2) == typeof(char) || typeof(T2) == typeof(byte[])) { return base.WriteWithLengthCustom(value, buf, lengthCache, parameter, async, cancellationToken); } // User POCO, read serialized representation from the validation phase var serialized = parameter?.ConvertedValue != null ? (string)parameter.ConvertedValue : JsonConvert.SerializeObject(value, _settings); return base.WriteWithLengthCustom(serialized, buf, lengthCache, parameter, async, cancellationToken); } public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter) { if (value is string || value is char[] || value is ArraySegment<char> || value is char || value is byte[]) { return base.ValidateObjectAndGetLength(value, ref lengthCache, parameter); } return ValidateAndGetLengthCustom(value, ref lengthCache, parameter); } public override Task WriteObjectWithLength(object? value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default) { if (value is null || value is DBNull || value is string || value is char[] || value is ArraySegment<char> || value is char || value is byte[]) { return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async, cancellationToken); } return WriteWithLengthCustom(value, buf, lengthCache, parameter, async, cancellationToken); } }