Amazon.DynamoDBv2.DocumentModel.JsonUtils.DecodeBase64Attributes C# (CSharp) Method

DecodeBase64Attributes() public static method

Decodes specific attributes from base64 to their binary representation.
public static DecodeBase64Attributes ( Document document ) : void
document Document
return void
        public static void DecodeBase64Attributes(Document document, params string[] attributeNames)
        {
            if (attributeNames == null || attributeNames.Length == 0)
                return;

            var decodedValues = new Dictionary<string, DynamoDBEntry>(StringComparer.Ordinal);

            // Convert, but don't alter the original yet
            foreach(var attributeName in attributeNames)
            {
                DynamoDBEntry entry;
                // If an attribute is not present, do nothing
                if (!document.TryGetValue(attributeName, out entry))
                    continue;

                DynamoDBEntry decodedEntry;
                if (!TryDecodeBase64(entry, out decodedEntry))
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                        "Unable to decode attribute {0} of type {1} in document", attributeName, entry.GetType().FullName));

                decodedValues[attributeName] = decodedEntry;
            }

            // Update document with decoded attribute values
            foreach(var kvp in decodedValues)
            {
                var attributeName = kvp.Key;
                var decodedEntry = kvp.Value;

                document[attributeName] = decodedEntry;
            }
        }

Usage Example

 /// <summary>
 /// <para>
 /// Decodes root-level Base64-encoded strings to their binary representations.
 /// Use this method if the Document was constructed from JSON that contains
 /// base64-encoded binary values, which result from calling ToJson on a Document
 /// with binary data.
 /// </para>
 /// <para>
 /// Individual strings become binary data.
 /// List and sets of Base64-encoded strings become lists and sets of binary data.
 /// </para>
 /// </summary>
 /// <param name="attributeNames">Names of root-level attributes to decode.</param>
 public void DecodeBase64Attributes(params string[] attributeNames)
 {
     JsonUtils.DecodeBase64Attributes(this, attributeNames);
 }