-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJsonValueFormatter.cs
More file actions
96 lines (91 loc) · 3.39 KB
/
Copy pathJsonValueFormatter.cs
File metadata and controls
96 lines (91 loc) · 3.39 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
using System.Text;
using NpgsqlTypes;
namespace NpgsqlRest;
/// <summary>
/// Shared JSON value formatting for column values.
/// Used by both single-command and multi-command rendering paths
/// to guarantee identical output for the same column types.
/// </summary>
internal static class JsonValueFormatter
{
/// <summary>
/// Format a single column value into the output buffer as JSON.
/// Handles: null, arrays, array-of-composites, numeric, boolean, JSON, datetime, text with escaping.
/// </summary>
/// <param name="raw">Raw string value from PostgreSQL (via AllResultTypesAreUnknown)</param>
/// <param name="value">The original object value (for DBNull check)</param>
/// <param name="descriptor">Type descriptor for this column</param>
/// <param name="outputBuffer">StringBuilder to write formatted value to</param>
/// <param name="routineArrayCompositeInfo">Array composite dictionary from Routine, or null</param>
/// <param name="columnIndex">Column index for array composite lookup</param>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static void FormatValue(
ReadOnlySpan<char> raw,
object value,
TypeDescriptor descriptor,
StringBuilder outputBuffer,
Dictionary<int, (string[] FieldNames, TypeDescriptor[] FieldDescriptors)>? routineArrayCompositeInfo,
int columnIndex)
{
if (value == DBNull.Value)
{
outputBuffer.Append(Consts.Null);
return;
}
if (descriptor.IsArray)
{
// Check if this is an array of composite types
if (routineArrayCompositeInfo is not null &&
routineArrayCompositeInfo.TryGetValue(columnIndex, out var arrayCompositeInfo))
{
outputBuffer.Append(PgCompositeArrayToJsonArray(raw, arrayCompositeInfo.FieldNames, arrayCompositeInfo.FieldDescriptors));
}
else
{
outputBuffer.Append(PgArrayToJsonArray(raw, descriptor));
}
return;
}
if ((descriptor.Category & (TypeCategory.Numeric | TypeCategory.Boolean | TypeCategory.Json)) != 0)
{
if ((descriptor.Category & TypeCategory.Boolean) != 0)
{
if (raw.Length == 1 && raw[0] == 't')
{
outputBuffer.Append(Consts.True);
}
else if (raw.Length == 1 && raw[0] == 'f')
{
outputBuffer.Append(Consts.False);
}
else
{
outputBuffer.Append(raw);
}
}
else
{
// numeric and json passthrough
outputBuffer.Append(raw);
}
return;
}
// Text and other types
if (descriptor.ActualDbType == NpgsqlDbType.Unknown)
{
outputBuffer.Append(PgUnknownToJsonArray(ref raw));
}
else if (descriptor.NeedsEscape)
{
outputBuffer.Append(SerializeString(ref raw));
}
else if ((descriptor.Category & TypeCategory.DateTime) != 0)
{
outputBuffer.Append(QuoteDateTime(ref raw));
}
else
{
outputBuffer.Append(Quote(ref raw));
}
}
}