-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathExtensionMethods.cs
161 lines (151 loc) · 5.81 KB
/
ExtensionMethods.cs
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
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using static Amazon.Lambda.DynamoDBEvents.DynamoDBEvent;
namespace Amazon.Lambda.DynamoDBEvents
{
/// <summary>
/// Extension methods for working with <see cref="DynamoDBEvent"/>
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Converts a dictionary representing a DynamoDB item to a JSON string.
/// This may be useful when casting a DynamoDB Lambda event to the AWS SDK's
/// higher-level document and object persistence classes.
/// </summary>
/// <param name="item">Dictionary representing a DynamoDB item</param>
/// <returns>Unformatted JSON string representing the DynamoDB item</returns>
public static string ToJson(this Dictionary<string, AttributeValue> item)
{
return ToJson(item, false);
}
/// <summary>
/// Converts a dictionary representing a DynamoDB item to a JSON string.
/// This may be useful when casting a DynamoDB Lambda event to the AWS SDK's
/// higher-level document and object persistence classes.
/// </summary>
/// <param name="item">Dictionary representing a DynamoDB item</param>
/// <returns>Formatted JSON string representing the DynamoDB item</returns>
public static string ToJsonPretty(this Dictionary<string, AttributeValue> item)
{
return ToJson(item, true);
}
/// <summary>
/// Internal entry point for converting a dictionary representing a DynamoDB item to a JSON string.
/// </summary>
/// <param name="item">Dictionary representing a DynamoDB item</param>
/// <param name="prettyPrint">Whether the resulting JSON should be formatted</param>
/// <returns>JSON string representing the DynamoDB item</returns>
private static string ToJson(Dictionary<string, AttributeValue> item, bool prettyPrint)
{
if (item == null || item.Count == 0)
{
return "{}";
}
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = prettyPrint});
WriteJson(writer, item);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
/// <summary>
/// Writes a single DynamoDB attribute as a json. May be called recursively for maps.
/// </summary>
/// <param name="writer">JSON writer</param>
/// <param name="item">Dictionary representing a DynamoDB item, or a map within an item</param>
private static void WriteJson(Utf8JsonWriter writer, Dictionary<string, AttributeValue> item)
{
writer.WriteStartObject();
foreach (var attribute in item)
{
writer.WritePropertyName(attribute.Key);
WriteJsonValue(writer, attribute.Value);
}
writer.WriteEndObject();
}
/// <summary>
/// Writes a single DynamoDB attribute value as a json value
/// </summary>
/// <param name="writer">JSON writer</param>
/// <param name="attribute">DynamoDB attribute</param>
private static void WriteJsonValue(Utf8JsonWriter writer, AttributeValue attribute)
{
if (attribute.S != null)
{
writer.WriteStringValue(attribute.S);
}
else if (attribute.N != null)
{
#if NETCOREAPP3_1 // WriteRawValue was added in .NET 6, but we need to write out Number values without quotes
using var document = JsonDocument.Parse(attribute.N);
document.WriteTo(writer);
#else
writer.WriteRawValue(attribute.N);
#endif
}
else if (attribute.B != null)
{
writer.WriteBase64StringValue(attribute.B.ToArray());
}
else if (attribute.BOOL != null)
{
writer.WriteBooleanValue(attribute.BOOL.Value);
}
else if (attribute.NULL != null)
{
writer.WriteNullValue();
}
else if (attribute.M != null)
{
WriteJson(writer, attribute.M);
}
else if (attribute.L != null)
{
writer.WriteStartArray();
foreach (var item in attribute.L)
{
WriteJsonValue(writer, item);
}
writer.WriteEndArray();
}
else if (attribute.SS != null)
{
writer.WriteStartArray();
foreach (var item in attribute.SS)
{
writer.WriteStringValue(item);
}
writer.WriteEndArray();
}
else if (attribute.NS != null)
{
writer.WriteStartArray();
foreach (var item in attribute.NS)
{
#if NETCOREAPP3_1 // WriteRawValue was added in .NET 6, but we need to write out Number values without quotes
using var document = JsonDocument.Parse(item);
document.WriteTo(writer);
#else
writer.WriteRawValue(item);
#endif
}
writer.WriteEndArray();
}
else if (attribute.BS != null)
{
writer.WriteStartArray();
foreach (var item in attribute.BS)
{
writer.WriteBase64StringValue(item.ToArray());
}
writer.WriteEndArray();
}
else
{
writer.WriteNullValue();
}
}
}
}