Skip to content

Instantly share code, notes, and snippets.

@creatigent
Forked from jvwing/DynamoEnumConverter
Created March 12, 2021 14:31
Show Gist options
  • Save creatigent/9e62ec2af627fbecf8dd077d3d1c0b3c to your computer and use it in GitHub Desktop.
Save creatigent/9e62ec2af627fbecf8dd077d3d1c0b3c to your computer and use it in GitHub Desktop.
For use with Amazon DynamoDB DataModel API for .NET. This class can be used to automagically serialize enum properties to DynamoDB, by decorating the property with the attribute [DynamoDBProperty(typeof(DynamoEnumConverter<AccountStatus>))], where "AccountStatus" is the name of the enumerated type..
public class DynamoEnumConverter<TEnum> : IPropertyConverter
{
public object FromEntry(DynamoDBEntry entry)
{
string valueAsString = entry.AsString();
TEnum valueAsEnum = (TEnum)Enum.Parse(typeof(TEnum), valueAsString);
return valueAsEnum;
}
public DynamoDBEntry ToEntry(object value)
{
string valueAsString = value.ToString();
DynamoDBEntry entry = new Primitive(valueAsString);
return entry;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment