Last active
October 31, 2019 09:08
-
-
Save chivandikwa/5c1080f0cf61dffd0ccf30754c2df6a3 to your computer and use it in GitHub Desktop.
DynamoDB nullable date property converter
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
using System; | |
using Amazon.DynamoDBv2.DataModel; | |
using Amazon.DynamoDBv2.DocumentModel; | |
public class NullableDatePropertyConverter : IPropertyConverter | |
{ | |
public DynamoDBEntry ToEntry(object value) | |
{ | |
DynamoDBEntry entry = new Primitive { | |
Value = null | |
}; | |
if (value != null) | |
entry = new Primitive { Value = ((DateTime)value).ToString("o") }; | |
return entry; | |
} | |
public object FromEntry(DynamoDBEntry entry) | |
{ | |
var dateTimeString = (entry as Primitive)?.Value.ToString(); | |
return DateTime.Parse(dateTimeString, null, System.Globalization.DateTimeStyles.RoundtripKind); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment