Skip to content

Instantly share code, notes, and snippets.

@ivankatliarchuk
Forked from jvwing/DynamoEnumConverter
Created December 27, 2019 21:13
Show Gist options
  • Save ivankatliarchuk/3df7970d05d1c982a5f321403304dca2 to your computer and use it in GitHub Desktop.
Save ivankatliarchuk/3df7970d05d1c982a5f321403304dca2 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