Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save zllvm/01b8e84c1dbb68ccf0b78e74b99f080a to your computer and use it in GitHub Desktop.

Select an option

Save zllvm/01b8e84c1dbb68ccf0b78e74b99f080a to your computer and use it in GitHub Desktop.
Serialize MessageAttributes for SNS/SQS event
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