-
Notifications
You must be signed in to change notification settings - Fork 491
Add ToJson for DynamoDBEvent #1685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
157 changes: 157 additions & 0 deletions
157
Libraries/src/Amazon.Lambda.DynamoDBEvents/ExtensionMethods.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using static Amazon.Lambda.DynamoDBEvents.DynamoDBEvent; | ||
|
||
namespace Amazon.Lambda.DynamoDBEvents | ||
{ | ||
/// <summary> | ||
/// Extension methods for working with <see cref="DynamoDBEvent"/> | ||
/// </summary> | ||
public static class ExtensionMethods | ||
{ | ||
/// <summary> | ||
/// Converts a dictionary representing a DynamoDB item to a JSON string. | ||
/// This may be useful when casting a DynamoDB Lambda event to the AWS SDK's | ||
/// higher-level document and object persistence classes. | ||
/// </summary> | ||
/// <param name="item">Dictionary representing a DynamoDB item</param> | ||
/// <returns>Unformatted JSON string representing the DynamoDB item</returns> | ||
public static string ToJson(this Dictionary<string, AttributeValue> item) | ||
{ | ||
return ToJson(item, false); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a dictionary representing a DynamoDB item to a JSON string. | ||
/// This may be useful when casting a DynamoDB Lambda event to the AWS SDK's | ||
/// higher-level document and object persistence classes. | ||
/// </summary> | ||
/// <param name="item">Dictionary representing a DynamoDB item</param> | ||
/// <returns>Formatted JSON string representing the DynamoDB item</returns> | ||
public static string ToJsonPretty(this Dictionary<string, AttributeValue> item) | ||
{ | ||
return ToJson(item, true); | ||
} | ||
|
||
/// <summary> | ||
/// Internal entry point for converting a dictionary representing a DynamoDB item to a JSON string. | ||
/// </summary> | ||
/// <param name="item">Dictionary representing a DynamoDB item</param> | ||
/// <param name="prettyPrint">Whether the resulting JSON should be formatted</param> | ||
/// <returns>JSON string representing the DynamoDB item</returns> | ||
private static string ToJson(Dictionary<string, AttributeValue> item, bool prettyPrint) | ||
{ | ||
if (item == null || item.Count == 0) | ||
{ | ||
return "{}"; | ||
} | ||
|
||
using var stream = new MemoryStream(); | ||
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = prettyPrint}); | ||
|
||
WriteJson(writer, item); | ||
|
||
writer.Flush(); | ||
return Encoding.UTF8.GetString(stream.ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// Writes a single DynamoDB attribute as a json. May be called recursively for maps. | ||
/// </summary> | ||
/// <param name="writer">JSON writer</param> | ||
/// <param name="item">Dictionary representing a DynamoDB item, or a map within an item</param> | ||
private static void WriteJson(Utf8JsonWriter writer, Dictionary<string, AttributeValue> item) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
foreach (var attribute in item) | ||
{ | ||
writer.WritePropertyName(attribute.Key); | ||
WriteJsonValue(writer, attribute.Value); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <summary> | ||
/// Writes a single DynamoDB attribute value as a json value | ||
/// </summary> | ||
/// <param name="writer">JSON writer</param> | ||
/// <param name="attribute">DynamoDB attribute</param> | ||
private static void WriteJsonValue(Utf8JsonWriter writer, AttributeValue attribute) | ||
{ | ||
if (attribute.S != null) | ||
{ | ||
writer.WriteStringValue(attribute.S); | ||
} | ||
else if (attribute.N != null) | ||
{ | ||
#if NETCOREAPP3_1 // WriteRawValue was added in .NET 6, but we need to write out Number values without quotes | ||
using var document = JsonDocument.Parse(attribute.N); | ||
document.WriteTo(writer); | ||
#else | ||
writer.WriteRawValue(attribute.N); | ||
#endif | ||
} | ||
else if (attribute.B != null) | ||
{ | ||
writer.WriteBase64StringValue(attribute.B.ToArray()); | ||
} | ||
else if (attribute.BOOL != null) | ||
{ | ||
writer.WriteBooleanValue(attribute.BOOL.Value); | ||
} | ||
else if (attribute.NULL != null) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else if (attribute.M != null) | ||
{ | ||
WriteJson(writer, attribute.M); | ||
} | ||
else if (attribute.L != null) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in attribute.L) | ||
{ | ||
WriteJsonValue(writer, item); | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
else if (attribute.SS != null) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in attribute.SS) | ||
{ | ||
writer.WriteStringValue(item); | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
else if (attribute.NS != null) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in attribute.NS) | ||
{ | ||
#if NETCOREAPP3_1 // WriteRawValue was added in .NET 6, but we need to write out Number values without quotes | ||
using var document = JsonDocument.Parse(item); | ||
document.WriteTo(writer); | ||
#else | ||
writer.WriteRawValue(item); | ||
#endif | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
else if (attribute.BS != null) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in attribute.BS) | ||
{ | ||
writer.WriteBase64StringValue(item.ToArray()); | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.