-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented custom DictionaryLongToStringJsonConverter (System.Text.J…
…son.Serialization.JsonConverter) for non-NetStandard 2.0 target(s) for handling Kinesis and DynamoDB TimeWindowEvent State property.
- Loading branch information
1 parent
5498aa3
commit 8726a45
Showing
11 changed files
with
191 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
71 changes: 71 additions & 0 deletions
71
Libraries/src/Amazon.Lambda.DynamoDBEvents/Converters/DictionaryLongToStringJsonConverter.cs
This file contains 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,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Amazon.Lambda.DynamoDBEvents.Converters | ||
{ | ||
public class DictionaryLongToStringJsonConverter : JsonConverter<Dictionary<string, string>> | ||
{ | ||
public override Dictionary<string, string> Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException($"JsonTokenType was of type {reader.TokenType}, only objects are supported."); | ||
} | ||
|
||
var dictionary = new Dictionary<string, string>(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
return dictionary; | ||
} | ||
|
||
// Get the key. | ||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException("JsonTokenType was not PropertyName."); | ||
} | ||
|
||
var propertyName = reader.GetString(); | ||
|
||
if (string.IsNullOrWhiteSpace(propertyName)) | ||
{ | ||
throw new JsonException("Failed to get property name."); | ||
} | ||
|
||
// Get the value. | ||
reader.Read(); | ||
var keyValue = ExtractValue(ref reader, options); | ||
dictionary.Add(propertyName, keyValue); | ||
} | ||
|
||
return dictionary; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Dictionary<string, string> value, JsonSerializerOptions options) | ||
{ | ||
// Use the built-in serializer, because it can handle dictionaries with string keys. | ||
JsonSerializer.Serialize(writer, value, options); | ||
} | ||
|
||
private string ExtractValue(ref Utf8JsonReader reader, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Number: | ||
if (reader.TryGetInt64(out var result)) | ||
{ | ||
return result.ToString(); | ||
} | ||
throw new JsonException($"Unable to convert '{reader.TokenType}' to long value."); | ||
case JsonTokenType.String: // If it is string, then use as it is. | ||
return reader.GetString(); | ||
default: | ||
throw new JsonException($"'{reader.TokenType}' is not supported."); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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
71 changes: 71 additions & 0 deletions
71
Libraries/src/Amazon.Lambda.KinesisEvents/Converters/DictionaryLongToStringJsonConverter.cs
This file contains 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,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Amazon.Lambda.KinesisEvents.Converters | ||
{ | ||
public class DictionaryLongToStringJsonConverter : JsonConverter<Dictionary<string, string>> | ||
{ | ||
public override Dictionary<string, string> Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException($"JsonTokenType was of type {reader.TokenType}, only objects are supported."); | ||
} | ||
|
||
var dictionary = new Dictionary<string, string>(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
return dictionary; | ||
} | ||
|
||
// Get the key. | ||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException("JsonTokenType was not PropertyName."); | ||
} | ||
|
||
var propertyName = reader.GetString(); | ||
|
||
if (string.IsNullOrWhiteSpace(propertyName)) | ||
{ | ||
throw new JsonException("Failed to get property name."); | ||
} | ||
|
||
// Get the value. | ||
reader.Read(); | ||
var keyValue = ExtractValue(ref reader, options); | ||
dictionary.Add(propertyName, keyValue); | ||
} | ||
|
||
return dictionary; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Dictionary<string, string> value, JsonSerializerOptions options) | ||
{ | ||
// Use the built-in serializer, because it can handle dictionaries with string keys. | ||
JsonSerializer.Serialize(writer, value, options); | ||
} | ||
|
||
private string ExtractValue(ref Utf8JsonReader reader, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Number: | ||
if (reader.TryGetInt64(out var result)) | ||
{ | ||
return result.ToString(); | ||
} | ||
throw new JsonException($"Unable to convert '{reader.TokenType}' to long value."); | ||
case JsonTokenType.String: // If it is string, then use as it is. | ||
return reader.GetString(); | ||
default: | ||
throw new JsonException($"'{reader.TokenType}' is not supported."); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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
35 changes: 0 additions & 35 deletions
35
...es/src/Amazon.Lambda.Serialization.SystemTextJson/Converters/LongToStringJsonConverter.cs
This file was deleted.
Oops, something went wrong.
This file contains 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