Skip to content

Instantly share code, notes, and snippets.

@jvwing
Created May 30, 2014 19:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jvwing/ac549044411b7953367e to your computer and use it in GitHub Desktop.
Save jvwing/ac549044411b7953367e 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