forked from aws/aws-lambda-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AwsResolver.cs
153 lines (144 loc) · 6.36 KB
/
AwsResolver.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
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Amazon.Lambda.Serialization.Json
{
/// <summary>
/// Custom contract resolver for handling special event cases.
/// </summary>
internal class AwsResolver : DefaultContractResolver
{
private JsonToMemoryStreamDataConverter jsonToMemoryStreamDataConverter;
private JsonNumberToDateTimeDataConverter jsonNumberToDateTimeDataConverter;
private JsonToMemoryStreamListDataConverter jsonToMemoryStreamListDataConverter;
JsonToMemoryStreamDataConverter StreamDataConverter
{
get
{
if (jsonToMemoryStreamDataConverter == null)
{
jsonToMemoryStreamDataConverter = new JsonToMemoryStreamDataConverter();
}
return jsonToMemoryStreamDataConverter;
}
}
JsonToMemoryStreamListDataConverter StreamListDataConverter
{
get
{
if (jsonToMemoryStreamListDataConverter == null)
{
jsonToMemoryStreamListDataConverter = new JsonToMemoryStreamListDataConverter();
}
return jsonToMemoryStreamListDataConverter;
}
}
JsonNumberToDateTimeDataConverter DateTimeConverter
{
get
{
if (jsonNumberToDateTimeDataConverter == null)
{
jsonNumberToDateTimeDataConverter = new JsonNumberToDateTimeDataConverter();
}
return jsonNumberToDateTimeDataConverter;
}
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
// S3 events use non-standard key formatting for request IDs and need to be mapped to the correct properties
if (type.FullName.Equals("Amazon.S3.Util.S3EventNotification+ResponseElementsEntity", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("XAmzRequestId", StringComparison.Ordinal))
{
property.PropertyName = "x-amz-request-id";
}
else if (property.PropertyName.Equals("XAmzId2", StringComparison.Ordinal))
{
property.PropertyName = "x-amz-id-2";
}
}
}
else if (type.FullName.Equals("Amazon.Lambda.KinesisEvents.KinesisEvent+Record", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("Data", StringComparison.Ordinal))
{
property.MemberConverter = StreamDataConverter;
}
else if (property.PropertyName.Equals("ApproximateArrivalTimestamp", StringComparison.Ordinal))
{
property.MemberConverter = DateTimeConverter;
}
}
}
else if (type.FullName.Equals("Amazon.DynamoDBv2.Model.StreamRecord", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("ApproximateCreationDateTime", StringComparison.Ordinal))
{
property.MemberConverter = DateTimeConverter;
}
}
}
else if (type.FullName.Equals("Amazon.DynamoDBv2.Model.AttributeValue", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("B", StringComparison.Ordinal))
{
property.MemberConverter = StreamDataConverter;
}
else if (property.PropertyName.Equals("BS", StringComparison.Ordinal))
{
property.MemberConverter = StreamListDataConverter;
}
}
}
else if (type.FullName.Equals("Amazon.Lambda.SQSEvents.SQSEvent+MessageAttribute", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("BinaryValue", StringComparison.Ordinal))
{
property.MemberConverter = StreamDataConverter;
}
else if (property.PropertyName.Equals("BinaryListValues", StringComparison.Ordinal))
{
property.MemberConverter = StreamListDataConverter;
}
}
}
else if (type.FullName.StartsWith("Amazon.Lambda.CloudWatchEvents.")
&& (type.GetTypeInfo().BaseType?.FullName?.StartsWith("Amazon.Lambda.CloudWatchEvents.CloudWatchEvent`",
StringComparison.Ordinal) ?? false))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("DetailType", StringComparison.Ordinal))
{
property.PropertyName = "detail-type";
}
}
}
else if (type.FullName.Equals("Amazon.Lambda.KafkaEvents.KafkaEvent+KafkaEventRecord", StringComparison.Ordinal))
{
foreach (JsonProperty property in properties)
{
if (property.PropertyName.Equals("Value", StringComparison.Ordinal))
{
property.MemberConverter = StreamDataConverter;
}
}
}
return properties;
}
}
}