forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonbHandler.cs
More file actions
112 lines (97 loc) · 4.92 KB
/
JsonbHandler.cs
File metadata and controls
112 lines (97 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
{
public class JsonbHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
readonly JsonSerializerSettings _settings;
public JsonbHandlerFactory(JsonSerializerSettings? settings = null)
=> _settings = settings ?? new JsonSerializerSettings();
public override NpgsqlTypeHandler<string> Create(PostgresType postgresType, NpgsqlConnector conn)
=> new JsonbHandler(postgresType, conn, _settings);
}
class JsonbHandler : Npgsql.Internal.TypeHandlers.JsonHandler
{
readonly JsonSerializerSettings _settings;
public JsonbHandler(PostgresType postgresType, NpgsqlConnector connector, JsonSerializerSettings settings)
: base(postgresType, connector, 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);
}
}
}