using System; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; using System.Collections.Generic; namespace Amazon.Lambda.Serialization.SystemTextJson.Converters { /// /// JsonConvert to handle the AWS SDK for .NET custom enum classes that derive from the class called ConstantClass. /// /// /// Because this package can not take a dependency on AWSSDK.Core we need to use name heuristics and reflection to determine if the type /// extends from ConstantClass. /// public class ConstantClassConverter : JsonConverter { private const string CONSTANT_CLASS_NAME = "Amazon.Runtime.ConstantClass"; private readonly static HashSet ConstantClassNames = new HashSet { "Amazon.S3.EventType", "Amazon.DynamoDBv2.OperationType", "Amazon.DynamoDBv2.StreamViewType" }; /// /// Check to see if the type is derived from ConstantClass. /// /// /// public override bool CanConvert(Type typeToConvert) { return ConstantClassNames.Contains(typeToConvert.FullName); } /// /// Called when a JSON document is being reading and a property is being converted to a ConstantClass type. /// /// /// /// /// public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var value = reader.GetString(); return Activator.CreateInstance(typeToConvert, new object[] {value}); } /// /// Called when writing the ConstantClass out to the JSON document. /// /// /// /// public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString()); } } }