forked from kenjiuno/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonHandler.cs
More file actions
111 lines (96 loc) · 4.76 KB
/
JsonHandler.cs
File metadata and controls
111 lines (96 loc) · 4.76 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
using System;
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 JsonHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
readonly JsonSerializerSettings _settings;
public JsonHandlerFactory(JsonSerializerSettings? settings = null)
=> _settings = settings ?? new JsonSerializerSettings();
public override NpgsqlTypeHandler<string> Create(PostgresType postgresType, NpgsqlConnection conn)
=> new JsonHandler(postgresType, conn, _settings);
}
class JsonHandler : Npgsql.Internal.TypeHandlers.JsonHandler
{
readonly JsonSerializerSettings _settings;
public JsonHandler(PostgresType postgresType, NpgsqlConnection connection, JsonSerializerSettings settings)
: base(postgresType, connection, isJsonb: false) => _settings = settings;
protected override async ValueTask<T> Read<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.Read<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 ValidateAndGetLength<T2>(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.ValidateAndGetLength(value, ref lengthCache, parameter);
}
var serialized = JsonConvert.SerializeObject(value, _settings);
if (parameter != null)
parameter.ConvertedValue = serialized;
return base.ValidateAndGetLength(serialized, ref lengthCache, parameter);
}
protected override Task WriteWithLength<T2>(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.WriteWithLength(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.WriteWithLength(serialized, buf, lengthCache, parameter, async, cancellationToken);
}
public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{
if (value is DBNull ||
value is string ||
value is char[] ||
value is ArraySegment<char> ||
value is char ||
value is byte[])
{
return base.ValidateObjectAndGetLength(value, ref lengthCache, parameter);
}
return ValidateAndGetLength(value, ref lengthCache, parameter);
}
public override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
{
if (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 WriteWithLength(value, buf, lengthCache, parameter, async, cancellationToken);
}
}
}