-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPgConverters.cs
More file actions
209 lines (191 loc) · 6.24 KB
/
Copy pathPgConverters.cs
File metadata and controls
209 lines (191 loc) · 6.24 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text;
using System.Text.Json.Serialization;
namespace NpgsqlRest;
[JsonSerializable(typeof(string))]
internal partial class NpgsqlRestSerializerContext : JsonSerializerContext;
internal static class PgConverters
{
private static readonly JsonSerializerOptions plainTextSerializerOptions = new()
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
TypeInfoResolver = new NpgsqlRestSerializerContext()
};
[UnconditionalSuppressMessage("Aot", "IL2026:RequiresUnreferencedCode",
Justification = "Serializes only string type that have AOT friendly TypeInfoResolver")]
[UnconditionalSuppressMessage("Aot", "IL3050:RequiresDynamic",
Justification = "Serializes only string type that have AOT friendly TypeInfoResolver")]
public static string SerializeString(ref string value) => JsonSerializer.Serialize(value, plainTextSerializerOptions);
public static string PgUnknownToJsonArray(ref string value)
{
if (value[0] != '(' || value[^1] != ')')
{
// should never happen
return value;
}
var len = value.Length;
var result = new StringBuilder(len * 2);
result.Append('[');
var current = new StringBuilder();
bool insideQuotes = false;
bool first = true;
for (int i = 1; i < len; i++)
{
char currentChar = value[i];
if ((currentChar == ',' || (currentChar == ')' && i == len - 1)) && !insideQuotes)
{
if (!first)
{
result.Append(',');
}
else
{
first = false;
}
if (current.Length == 0)
{
result.Append("null");
}
else
{
var segment = current.ToString();
result.Append(SerializeString(ref segment));
current.Clear();
}
}
else
{
if (currentChar == '"' && i < len - 2 && value[i + 1] == '"')
{
current.Append(currentChar);
i++;
continue;
}
if (currentChar == '"')
{
insideQuotes = !insideQuotes;
}
else
{
if (currentChar == '\\' && i < len - 2 && value[i + 1] == '\\')
{
i++;
current.Append(currentChar);
}
else
{
current.Append(currentChar);
}
}
}
}
result.Append(']');
return result.ToString();
}
public static string PgArrayToJsonArray(ref string value, ref TypeDescriptor descriptor)
{
var len = value.Length;
if (string.IsNullOrWhiteSpace(value) || len < 3 || value[0] != '{' || value[^1] != '}')
{
return value;
}
var result = new StringBuilder(len * 2);
result.Append('[');
var current = new StringBuilder();
var quoted = !(descriptor.IsNumeric || descriptor.IsBoolean || descriptor.IsJson);
bool insideQuotes = false;
bool hasQuotes = false;
bool IsNull()
{
if (current.Length == 4)
{
return
current[0] == 'N' &&
current[1] == 'U' &&
current[2] == 'L' &&
current[3] == 'L';
}
return false;
}
for (int i = 1; i < len; i++)
{
char currentChar = value[i];
if (currentChar == '"' && value[i - 1] != '\\')
{
insideQuotes = !insideQuotes;
hasQuotes = true;
}
else if ((currentChar == ',' && !insideQuotes) || currentChar == '}')
{
var currentIsNull = IsNull() && !hasQuotes;
if (quoted && !currentIsNull)
{
result.Append('"');
}
if (currentIsNull)
{
result.Append("null");
}
else
{
result.Append(current);
}
if (quoted && !currentIsNull)
{
result.Append('"');
}
if (currentChar != '}')
{
result.Append(',');
}
current.Clear();
hasQuotes = false;
}
else
{
if (descriptor.IsBoolean)
{
if (currentChar == 't')
{
current.Append("true");
}
else if (currentChar == 'f')
{
current.Append("false");
}
else
{
current.Append(currentChar);
}
}
else if (descriptor.IsDateTime)
{
//json time requires T between date and time
current.Append(currentChar == ' ' ? 'T' : currentChar);
}
else
{
if (currentChar == '\n')
{
current.Append("\\n");
}
else if (currentChar == '\t')
{
current.Append("\\r");
}
else if (currentChar == '\r')
{
current.Append("\\r");
}
else
{
current.Append(currentChar);
}
}
}
}
result.Append(']');
return result.ToString();
}
}