Serverless .NET for AWS - DynamoDB Serialization Library
$ dotnet add package LambdaSharp.DynamoDB.SerializationThis package contains interfaces and classes used for serializing data structures to AttributeValue instances used by Amazon DynamoDB. The LambdaSharp.DynamoDB.Serialization package is modeled after other serialization libraries, such as System.Text.Json.JsonSerializer to streamline conversions between .NET types and DynamoDB documents.
Visit LambdaSharp.NET to learn more about building serverless .NET solutions on AWS.
Serializing a .NET type instance is straightforward using DynamoSerialize.Serialize<T>(T item).
NOTE: Serialize() will return null when the state of the type being serialized is not supported by DynamoDB. For example, an empty HashSet<string> returns null.
Example:
using LambdaSharp.DynamoDB.Serialization;
var serialized = DynamoSerializer.Serialize(new {
Active = true,
Binary = Encoding.UTF8.GetBytes("Bye"),
Name = "John Doe",
Age = 42,
MixedList = new object[] {
new {
Message = "Hello"
},
"World!"
},
Dictionary = new Dictionary<string, object> {
["Key"] = "Value"
},
StringSet = new[] { "Red", "Blue" }.ToHashSet(),
NumberSet = new[] { 123, 456 }.ToHashSet(),
BinarySet = new[] {
Encoding.UTF8.GetBytes("Good"),
Encoding.UTF8.GetBytes("Day")
}.ToHashSet(ByteArrayEqualityComparer.Instance)
});
DynamoDB Output:
{
"M": {
"Active": { "BOOL": true },
"Binary": { "B": "Qnll" },
"Name": { "S": "John Doe" },
"Age": { "N": "42" },
"MixedList": {
"L": [
{
"M": {
"Message": { "S": "Hello" }
}
},
{ "S": "World!" }
]
},
"Dictionary": {
"M": {
"Key": { "S": "Value" }
}
},
"StringSet": {
"SS": [ "Red", "Blue" ]
},
"NumberSet": {
"NS": [ "123", "456" ]
},
"BinarySet": {
"BS": [ "R29vZA==", "RGF5" ]
}
}
}
Deserializing a DynamoDB document is straightforward using DynamoSerialize.Deserialize<T>(Dictionary<string, AttributeValue> document).
NOTE: Deserialize() will return null when deserialized the NULL attribute value.
NOTE: Deserialize() may throw DynamoSerializationException when an issue occurs during deserialization.
The behavior of DynamoSerialize.Serialize<T>(...) and DynamoSerialize.Deserialize<T>(...) can be modified by supplying an instance of DynamoSerializerOptions as a second parameter.
NOTE: It is recommended to create and share a single instance DynamoSerializerOptions.
The DynamoSerializerOptions has the following properties.
ConvertersThe Converters property lists additional custom converters to use when (de)serializing values. Custom converters take precedence over default converters. Default converters can be disabled entirely by setting the UseDefaultConverters property to false.
Type: List<IDynamoAttributeConverter>
Default: empty list
IgnoreNullValuesThe IgnoreNullValues property controls if null values are serialized as DynamoDB NULL attribute values or skipped.
Type: bool
Default: true
UseDefaultConvertersThe UseDefaultConverters property controls if the default DynamoDB converters are enabled.
Type: bool
Default: true
The default converters are:
DynamoBoolConverterDynamoIntConverterDynamoLongConverterDynamoDoubleConverterDynamoDateTimeOffsetConverterDynamoFloatConverterDynamoDecimalConverterDynamoStringConverterDynamoEnumConverterDynamoByteArrayConverterDynamoISetByteArrayConverterDynamoISetStringConverterDynamoISetIntConverterDynamoISetLongConverterDynamoISetDoubleConverterDynamoISetDecimalConverterDynamoIDictionaryConverterDynamoListConverterDynamoJsonElementConverterDynamoObjectConverterBy default, all public properties of a class are (de)serialized by DynamoSerializer using the property name. However, this behavior can modified by annotating the properties with the following attributes.
DynamoPropertyIgnoreThe DynamoPropertyIgnore attribute causes DynamoSerializer to ignore a property on a class.
DynamoPropertyName(string Name)The DynamoPropertyName attribute changes the name used by DynamoSerializer when serializing the property.
class MyRecord {
//--- Properties ---
[DynamoPropertyIgnore]
public string IgnoreMe { get; set; }
[DynamoPropertyName("NewName")]
public string RenameMe { get; set; }
}
Copyright (c) 2018-2022 LambdaSharp (λ#)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.