Last active
June 18, 2019 06:05
-
-
Save zllvm/01b8e84c1dbb68ccf0b78e74b99f080a to your computer and use it in GitHub Desktop.
Serialize MessageAttributes for SNS/SQS event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MessageAttributesConverterSerializeContractResolver : JsonConverter<Dictionary<string, SQSEvent.MessageAttribute>> | |
| { | |
| public override bool CanRead => false; | |
| public override void WriteJson(JsonWriter writer, Dictionary<string, SQSEvent.MessageAttribute> value, JsonSerializer serializer) | |
| { | |
| var o = new JObject(); | |
| foreach (var key in value.Keys) | |
| { | |
| value.TryGetValue(key, out var messageAttribute); | |
| if (messageAttribute != null) | |
| { | |
| var p = new JObject(); | |
| if (messageAttribute.BinaryListValues != null && messageAttribute.BinaryListValues.Count > 0) | |
| { | |
| var list = messageAttribute.BinaryListValues.Select(StringUtils.FromMemoryStream); | |
| p.Add(new JProperty("BinaryListValues", new JArray(list))); | |
| } | |
| if (messageAttribute.BinaryValue != null) | |
| { | |
| p.Add(new JProperty("BinaryValue", StringUtils.FromMemoryStream(messageAttribute.BinaryValue))); | |
| } | |
| if (messageAttribute.DataType != null) | |
| { | |
| p.Add(new JProperty("DataType", messageAttribute.DataType)); | |
| } | |
| if (messageAttribute.StringListValues != null && messageAttribute.StringListValues.Count > 0) | |
| { | |
| p.Add(new JProperty("StringListValues", new JArray(messageAttribute.StringListValues))); | |
| } | |
| if (messageAttribute.StringValue != null) | |
| { | |
| p.Add(new JProperty("StringValue", messageAttribute.StringValue)); | |
| } | |
| o.Add(key, p); | |
| } | |
| } | |
| o.WriteTo(writer); | |
| } | |
| public override Dictionary<string, SQSEvent.MessageAttribute> ReadJson(JsonReader reader, Type objectType, Dictionary<string, SQSEvent.MessageAttribute> existingValue, bool hasExistingValue, JsonSerializer serializer) | |
| { | |
| throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment