diff --git a/sdk/src/Core/Amazon.Runtime/Internal/Transform/JsonUnmarshallerContext.cs b/sdk/src/Core/Amazon.Runtime/Internal/Transform/JsonUnmarshallerContext.cs index 306c1752badf..79aa97c35743 100644 --- a/sdk/src/Core/Amazon.Runtime/Internal/Transform/JsonUnmarshallerContext.cs +++ b/sdk/src/Core/Amazon.Runtime/Internal/Transform/JsonUnmarshallerContext.cs @@ -288,7 +288,7 @@ public string ReadText(ref StreamingUtf8JsonReader reader) return text; } -#endregion + #endregion #region Public properties diff --git a/sdk/src/Core/Amazon.Util/Internal/JsonSerializerHelper.cs b/sdk/src/Core/Amazon.Util/Internal/JsonSerializerHelper.cs index 55935ddbdcc2..efdb7397b636 100644 --- a/sdk/src/Core/Amazon.Util/Internal/JsonSerializerHelper.cs +++ b/sdk/src/Core/Amazon.Util/Internal/JsonSerializerHelper.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.RegularExpressions; +using System.Text.Json; #if NET8_0_OR_GREATER -using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; #endif @@ -12,6 +13,13 @@ namespace Amazon.Util.Internal { public static class JsonSerializerHelper { + private static System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions + { + AllowTrailingCommas = true, + }; + + // compile regex once to avoid re-parsing every time + private static readonly System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"(""[^""]*""|\d+)(\s*""[^""]*""\s*:)", System.Text.RegularExpressions.RegexOptions.Compiled); #if NET8_0_OR_GREATER public static T Deserialize(string json, JsonSerializerContext context) { @@ -25,21 +33,26 @@ public static string Serialize(object obj, JsonSerializerContext context) #else public static T Deserialize(string json, JsonSerializerContext typeInfo) { - return ThirdParty.Json.LitJson.JsonMapper.ToObject(json); + string sanitizedJson = SanitizeJson(json); + return JsonSerializer.Deserialize(sanitizedJson, options); } public static string Serialize(object obj, JsonSerializerContext typeInfo) { - var json = new StringBuilder(); - var writer = new ThirdParty.Json.LitJson.JsonWriter(json) + var jsonSerializerOptions = new System.Text.Json.JsonSerializerOptions { - PrettyPrint = (typeInfo?.Options?.WriteIndented).GetValueOrDefault() + WriteIndented = (typeInfo?.Options?.WriteIndented).GetValueOrDefault() }; - ThirdParty.Json.LitJson.JsonMapper.ToJson(obj, writer); - return json.ToString(); + return JsonSerializer.Serialize(obj, jsonSerializerOptions); } #endif + public static string SanitizeJson(string rawJson) + { + // Add a comma after numbers or strings if they're not followed by a closing brace/bracket or comma. + rawJson = regex.Replace(rawJson, "$1,$2"); + return rawJson; + } } [JsonSerializable(typeof(IAMInstanceProfileMetadata))] @@ -104,7 +117,7 @@ public class JsonSerializerContext { public JsonSerializerContext() { } - public JsonSerializerContext(JsonSerializerOptions defaultOptions) + public JsonSerializerContext(JsonSerializerOptions defaultOptions) { Options = defaultOptions; } @@ -118,7 +131,7 @@ public class JsonSerializerOptions { public bool PropertyNameCaseInsensitive { get; set; } - public bool WriteIndented {get;set;} + public bool WriteIndented { get; set; } } #pragma warning disable CA1019 // Since this is a dummy implementation of JsonSerializableAttribute for pre .NET 8 targets we don't need the accessor. @@ -126,4 +139,4 @@ public class JsonSerializerOptions public sealed class JsonSerializableAttribute : Attribute { public JsonSerializableAttribute(Type type) { } } #pragma warning restore CA1019 #endif -} +} \ No newline at end of file diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/S3Link.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/S3Link.cs index ed72236c8fda..9180944c853b 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/S3Link.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/S3Link.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using Amazon.Util.Internal; -using ThirdParty.Json.LitJson; #if NET8_0_OR_GREATER using System.Text.Json; diff --git a/sdk/src/Services/EC2/Custom/Util/ImageUtilities.cs b/sdk/src/Services/EC2/Custom/Util/ImageUtilities.cs index 20b066ae4256..9bdc8195f1ce 100644 --- a/sdk/src/Services/EC2/Custom/Util/ImageUtilities.cs +++ b/sdk/src/Services/EC2/Custom/Util/ImageUtilities.cs @@ -20,15 +20,11 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Net; -using System.Threading; +using System.Text.Json; using Amazon.EC2.Model; using Amazon.Runtime.Internal.Util; -using ThirdParty.Json.LitJson; -using Amazon.Runtime; #pragma warning disable 1591 @@ -417,7 +413,7 @@ private static void ParseAMIDefinitions(StreamReader reader) { try { - var jdata = JsonMapper.ToObject(reader); + using var jsonDocument = JsonDocument.Parse(reader.BaseStream); var platformTags = new string[] { @@ -428,18 +424,27 @@ private static void ParseAMIDefinitions(StreamReader reader) var parsedDefinitionsMap = new Dictionary(); foreach (var platformTag in platformTags) { - var imageDefinitions = jdata[platformTag]; - if (imageDefinitions == null) + if (!jsonDocument.RootElement.TryGetProperty(platformTag, out var imageDefinitions) || imageDefinitions.ValueKind != JsonValueKind.Array) { Logger.InfoFormat("Parsing AMI definitions - did not find any images for platform tag '{0}'", platformTag); continue; } - for (int d = 0; d < imageDefinitions.Count; d++) + for (int d = 0; d < imageDefinitions.GetArrayLength(); d++) { var def = imageDefinitions[d]; - var key = (string)def[DefinitionKeyTag]; - var prefix = (string)def[DefinitionPrefixTag]; + if (def.ValueKind != JsonValueKind.Object) + { + continue; + } + + var key = def.TryGetProperty(DefinitionKeyTag, out var keyElement) && keyElement.ValueKind == JsonValueKind.String + ? keyElement.GetString() + : null; + + var prefix = def.TryGetProperty(DefinitionPrefixTag, out var prefixElement) && prefixElement.ValueKind == JsonValueKind.String + ? prefixElement.GetString() + : null; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(prefix)) parsedDefinitionsMap[key] = prefix; diff --git a/sdk/src/Services/EC2/Custom/Util/_async/ImageUtilities.async.cs b/sdk/src/Services/EC2/Custom/Util/_async/ImageUtilities.async.cs index 701deaaba66d..11568d8f097e 100644 --- a/sdk/src/Services/EC2/Custom/Util/_async/ImageUtilities.async.cs +++ b/sdk/src/Services/EC2/Custom/Util/_async/ImageUtilities.async.cs @@ -18,16 +18,13 @@ * AWS SDK for .NET */ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Threading; using Amazon.EC2.Model; using Amazon.Runtime.Internal.Util; -using ThirdParty.Json.LitJson; using Amazon.Runtime; using System.Threading.Tasks; using Amazon.Util.Internal; diff --git a/sdk/src/Services/EC2/Custom/Util/_bcl/ImageUtilities.bcl.cs b/sdk/src/Services/EC2/Custom/Util/_bcl/ImageUtilities.bcl.cs index 2f3ae6599958..3c78a84ed329 100644 --- a/sdk/src/Services/EC2/Custom/Util/_bcl/ImageUtilities.bcl.cs +++ b/sdk/src/Services/EC2/Custom/Util/_bcl/ImageUtilities.bcl.cs @@ -18,7 +18,6 @@ * AWS SDK for .NET */ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; @@ -27,7 +26,6 @@ using Amazon.EC2.Model; using Amazon.Runtime.Internal.Util; -using ThirdParty.Json.LitJson; using Amazon.Runtime; using Amazon.Util.Internal; diff --git a/sdk/src/Services/EC2/Custom/_bcl/Util/EC2Metadata.cs b/sdk/src/Services/EC2/Custom/_bcl/Util/EC2Metadata.cs deleted file mode 100644 index 3c68c5be323a..000000000000 --- a/sdk/src/Services/EC2/Custom/_bcl/Util/EC2Metadata.cs +++ /dev/null @@ -1,774 +0,0 @@ -/******************************************************************************* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * Licensed under the Apache License, Version 2.0 (the "License"). You may not use - * this file except in compliance with the License. A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file 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. - * ***************************************************************************** - * __ _ _ ___ - * ( )( \/\/ )/ __) - * /__\ \ / \__ \ - * (_)(_) \/\/ (___/ - * - * AWS SDK for .NET - */ - -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Threading; - -using Amazon.Runtime; -using ThirdParty.Json.LitJson; -using System.Globalization; -using Amazon.Runtime.Internal.Util; -using Amazon.Util.Internal; -using Amazon.Util; - -namespace Amazon.EC2.Util -{ - /// - /// EC2 Instance Metadata. - /// If this class is used on a non-EC2 instance, the properties in this class - /// will return null. - /// - /// - /// - /// Amazon EC2 instances can access instance-specific metadata, as well as data supplied when launching the instances, using a specific URI. - /// - /// - /// You can use this data to build more generic AMIs that can be modified by configuration files supplied at launch time. - /// For example, if you run web servers for various small businesses, they can all use the same AMI and retrieve their content from the - /// Amazon S3 bucket you specify at launch. To add a new customer at any time, simply create a bucket for the customer, add their content, - /// and launch your AMI. - /// - /// - /// More information about EC2 Metadata - /// - /// - [Obsolete("This class is deprecated and will be removed in a future release." - + " Please update your code to use the Amazon.Util.EC2InstanceMetadata class, located in the AWSSDK.Core assembly.")] - public static class EC2Metadata - { - private static string - EC2_METADATA_SVC = "http://169.254.169.254", - EC2_METADATA_ROOT = EC2_METADATA_SVC + "/latest/meta-data", - EC2_USERDATA_ROOT = EC2_METADATA_SVC + "/latest/user-data/", - EC2_APITOKEN_URL = EC2_METADATA_SVC + "latest/api/token"; - - private static int - DEFAULT_RETRIES = 3, - MIN_PAUSE_MS = 250, - MAX_RETRIES = 3; - - private static Dictionary _cache = new Dictionary(); - - private static readonly string _userAgent = InternalSDKUtils.BuildUserAgentString(string.Empty); - - /// - /// The AMI ID used to launch the instance. - /// - public static string AmiId - { - get { return FetchData("/ami-id"); } - } - - /// - /// The index of this instance in the reservation. - /// - public static string AmiLaunchIndex - { - get { return FetchData("/ami-launch-index"); } - } - - /// - /// The manifest path of the AMI with which the instance was launched. - /// - public static string AmiManifestPath - { - get { return FetchData("/ami-manifest-path"); } - } - - /// - /// The AMI IDs of any instances that were rebundled to create this AMI. - /// Will only exist if the AMI manifest file contained an ancestor-amis key. - /// - public static IEnumerable AncestorAmiIds - { - get { return GetItems("/ancestor-ami-ids"); } - } - - /// - /// The private hostname of the instance. - /// In cases where multiple network interfaces are present, - /// this refers to the eth0 device (the device for which the device number is 0). - /// - public static string Hostname - { - get { return FetchData("/hostname"); } - } - - /// - /// Notifies the instance that it should reboot in preparation for bundling. - /// Valid values: none | shutdown | bundle-pending. - /// - public static string InstanceAction - { - get { return FetchData("/instance-action"); } - } - - /// - /// The ID of this instance. - /// - public static string InstanceId - { - get { return FetchData("/instance-id"); } - } - - /// - /// The type of instance. - /// - public static string InstanceType - { - get { return FetchData("/instance-type"); } - } - - /// - /// The ID of the kernel launched with this instance, if applicable. - /// - public static string KernelId - { - get { return GetData("kernel-id"); } - } - - /// - /// The local hostname of the instance. In cases where multiple network interfaces are present, - /// this refers to the eth0 device (the device for which device-number is 0). - /// - public static string LocalHostname - { - get { return FetchData("/local-hostname"); } - } - - /// - /// The instance's MAC address. In cases where multiple network interfaces are present, - /// this refers to the eth0 device (the device for which device-number is 0). - /// - public static string MacAddress - { - get { return FetchData("/mac"); } - } - - /// - /// The private IP address of the instance. In cases where multiple network interfaces are present, - /// this refers to the eth0 device (the device for which device-number is 0). - /// - public static string PrivateIpAddress - { - get { return FetchData("/local-ipv4"); } - } - - /// - /// The Availability Zone in which the instance launched. - /// - public static string AvailabilityZone - { - get { return FetchData("/placement/availability-zone"); } - } - - /// - /// Product codes associated with the instance, if any. - /// - public static IEnumerable ProductCodes - { - get { return GetItems("/product-codes"); } - } - - /// - /// Public key. Only available if supplied at instance launch time. - /// - public static string PublicKey - { - get { return FetchData("/public-keys/0/openssh-key"); } - } - - /// - /// The ID of the RAM disk specified at launch time, if applicable. - /// - public static string RamdiskId - { - get { return FetchData("/ramdisk-id"); } - } - - /// - /// ID of the reservation. - /// - public static string ReservationId - { - get { return FetchData("/reservation-id"); } - } - - /// - /// The names of the security groups applied to the instance. - /// - public static IEnumerable SecurityGroups - { - get { return GetItems("/security-groups"); } - } - - /// - /// Returns information about the last time the instance profile was updated, - /// including the instance's LastUpdated date, InstanceProfileArn, and InstanceProfileId. - /// - public static IAMInfo IAMInstanceProfileInfo - { - get - { - var json = GetData("/iam/info"); - if (null == json) - return null; - IAMInfo info; - try - { - info = JsonMapper.ToObject(json); - } - catch - { - info = new IAMInfo { Code = "Failed", Message = "Could not parse response from metadata service." }; - } - return info; - } - } - - /// - /// Returns the temporary security credentials (AccessKeyId, SecretAccessKey, SessionToken, and Expiration) - /// associated with the IAM roles on the instance. - /// - public static IDictionary IAMSecurityCredentials - { - get - { - var list = GetItems("/iam/security-credentials"); - if (list == null) - return null; - - var creds = new Dictionary(); - foreach (var item in list) - { - var json = GetData("/iam/security-credentials/" + item); - try - { - var cred = JsonMapper.ToObject(json); - creds[item] = cred; - } - catch - { - creds[item] = new IAMSecurityCredential { Code = "Failed", Message = "Could not parse response from metadata service." }; - } - } - - return creds; - } - } - - /// - /// The virtual devices associated with the ami, root, ebs, and swap. - /// - public static IDictionary BlockDeviceMapping - { - get - { - var keys = GetItems("/block-device-mapping"); - if (keys == null) - return null; - - var mapping = new Dictionary(); - foreach (var key in keys) - { - mapping[key] = GetData("/block-device-mapping/" + key); - } - - return mapping; - } - } - - /// - /// The network interfaces on the instance. - /// - public static IEnumerable NetworkInterfaces - { - get - { - var macs = GetItems("/network/interfaces/macs/"); - if (macs == null) - return null; - - var interfaces = new List(); - foreach (var mac in macs) - { - interfaces.Add(new NetworkInterface(mac.Trim('/'))); - } - return interfaces; - } - } - - /// - /// The metadata sent to the instance. - /// - public static string UserData - { - get - { - return GetData(EC2_USERDATA_ROOT); - } - } - - /// - /// Return the list of items in the metadata at path. - /// - /// Path at which to query the metadata - /// List of items returned by the metadata service - public static IEnumerable GetItems(string path) - { - return GetItems(path, DEFAULT_RETRIES, false); - } - - /// - /// Return the metadata at the path - /// - /// Path at which to query the metadata - /// Data returned by the metadata service - public static string GetData(string path) - { - return GetData(path, DEFAULT_RETRIES); - } - - /// - /// Return the metadata at the path - /// - /// Path at which to query the metadata - /// Number of attempts to make - /// Data returned by the metadata service - public static string GetData(string path, int tries) - { - var items = GetItems(path, tries, true); - if (items != null && items.Count > 0) - return items[0]; - return null; - } - - /// - /// Return the list of items in the metadata at path. - /// - /// Path at which to query the metadata - /// Number of attempts to make - /// List of items returned by the metadata service - public static IEnumerable GetItems(string path, int tries) - { - return GetItems(path, tries, false); - } - - private static string FetchData(string path) - { - return FetchData(path, false); - } - - private static string FetchData(string path, bool force) - { - try - { - if (force || !_cache.ContainsKey(path)) - _cache[path] = GetData(path); - - return _cache[path]; - } - catch - { - return null; - } - } - - private static List GetItems(string path, int tries, bool slurp) - { - return GetItems(path, tries, slurp, null); - } - - private static List GetItems(string path, int tries, bool slurp, string token) - { - var items = new List(); - //For all meta-data queries we need to fetch an api token to use. In the event a - //token cannot be obtained we will fallback to not using a token. - Dictionary headers = null; - if (token == null) - { - token = Amazon.Util.EC2InstanceMetadata.FetchApiToken(); - } - - if (!string.IsNullOrEmpty(token)) - { - headers = new Dictionary(); - headers.Add(HeaderKeys.XAwsEc2MetadataToken, token); - } - - - try - { - if (!Amazon.Util.EC2InstanceMetadata.IsIMDSEnabled) - { - throw new IMDSDisabledException(); - } - - HttpWebRequest request; - if (path.StartsWith("http", StringComparison.Ordinal)) - request = WebRequest.Create(path) as HttpWebRequest; - else - request = WebRequest.Create(EC2_METADATA_ROOT + path) as HttpWebRequest; - - request.Timeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds; - request.UserAgent = _userAgent; - if(headers != null) - { - foreach(var header in headers) - { - request.Headers.Add(header.Key, header.Value); - } - } - - using (var response = request.GetResponse()) - { - using (var stream = new StreamReader(response.GetResponseStream())) - { - if (slurp) - items.Add(stream.ReadToEnd()); - else - { - string line; - do - { - line = stream.ReadLine(); - if (line != null) - items.Add(line.Trim()); - } - while (line != null); - } - } - } - } - catch (WebException wex) - { - var response = wex.Response as HttpWebResponse; - if (response != null) - { - if (response.StatusCode == HttpStatusCode.NotFound) - { - return null; - } - else if (response.StatusCode == HttpStatusCode.Unauthorized) - { - EC2InstanceMetadata.ClearTokenFlag(); - Logger.GetLogger(typeof(Amazon.EC2.Util.EC2Metadata)).Error(wex, "EC2 Metadata service returned unauthorized for token based secure data flow."); - throw; - } - } - - if (tries <= 1) - { - Logger.GetLogger(typeof(Amazon.EC2.Util.EC2Metadata)).Error(wex, "Unable to contact EC2 Metadata service."); - return null; - } - - PauseExponentially(tries); - return GetItems(path, tries - 1, slurp, token); - } - catch (IMDSDisabledException) - { - // Keep this behavior identical to when HttpStatusCode.NotFound is returned. - return null; - } - - return items; - } - - private static void PauseExponentially(int tries) - { - tries = Math.Min(tries, MAX_RETRIES); - var pause = (int)(Math.Pow(2, DEFAULT_RETRIES - tries) * MIN_PAUSE_MS); - Thread.Sleep(pause < MIN_PAUSE_MS ? MIN_PAUSE_MS : pause); - } - -#if !NETSTANDARD - [Serializable] -#endif - private class IMDSDisabledException : InvalidOperationException { }; - } - - /// - /// Returns information about the last time the instance profile was updated, - /// including the instance's LastUpdated date, InstanceProfileArn, and InstanceProfileId. - /// - [Obsolete("This class is deprecated and will be removed in a future release." - + " Please update your code to use the Amazon.Util.IAMInstanceProfileMetadata class, located in the AWSSDK.Core assembly.")] - public class IAMInfo - { - /// - /// The status of the instance profile - /// - public string Code { get; set; } - - /// - /// Further information about the status of the instance profile - /// - public string Message { get; set; } - - /// - /// The date and time the instance profile was updated - /// - public DateTime LastUpdated { get; set; } - - /// - /// The Amazon Resource Name (ARN) of the instance profile - /// - public string InstanceProfileArn { get; set; } - - /// - /// The Id of the instance profile - /// - public string InstanceProfileId { get; set; } - } - - /// - /// The temporary security credentials (AccessKeyId, SecretAccessKey, SessionToken, and Expiration) associated with the IAM role. - /// - [Obsolete("This class is deprecated and will be removed in a future release." - + " Please update your code to use the Amazon.Util.IAMSecurityCredentialMetadata class, located in the AWSSDK.Core assembly.")] - public class IAMSecurityCredential - { - /// - /// The status of the security credential - /// - public string Code { get; set; } - - /// - /// Further information about the status of the instance profile - /// - public string Message { get; set; } - - /// - /// The date and time the security credential was last updated - /// - public DateTime LastUpdated { get; set; } - - /// - /// The type of the security credential - /// - public string Type { get; set; } - - /// - /// The uniqe id of the security credential - /// - public string AccessKeyId { get; set; } - - /// - /// The secret key used to sign requests - /// - public string SecretAccessKey { get; set; } - - /// - /// The security token - /// - public string Token { get; set; } - - /// - /// The date and time when these credentials expire - /// - public DateTime Expiration { get; set; } - } - - /// - /// All of the metadata associated with a network interface on the instance. - /// - [Obsolete("This class is deprecated and will be removed in a future release." - + " Please update your code to use the Amazon.Util.NetworkInterfaceMetadata class, located in the AWSSDK.Core assembly.")] - public class NetworkInterface - { - private string _path; - private string _mac; - - private IEnumerable _availableKeys; - private Dictionary _data = new Dictionary(); - - private NetworkInterface() { } - - /// - /// Construct an instance of NetworkInterface - /// - /// - public NetworkInterface(string macAddress) - { - _mac = macAddress; - _path = string.Format(CultureInfo.InvariantCulture, "/network/interfaces/macs/{0}/", _mac); - } - - /// - /// The interface's Media Access Control (mac) address. - /// - public string MacAddress - { - get { return _mac; } - } - - /// - /// The ID of the owner of the network interface. - /// - /// - /// In multiple-interface environments, an interface can be attached by a third party, such as Elastic Load Balancing. - /// Traffic on an interface is always billed to the interface owner. - /// - public string OwnerId - { - get { return GetData("owner-id"); } - } - - /// - /// The interface's profile - /// - public string Profile - { - get { return GetData("profile"); } - } - - /// - /// The interface's local hostname. - /// - public string LocalHostname - { - get { return GetData("local-hostname"); } - } - - /// - /// The private IP addresses associated with the interface. - /// - public IEnumerable LocalIPv4s - { - get { return GetItems("local-ipv4s"); } - } - - /// - /// The interface's public hostname. - /// - public string PublicHostname - { - get { return GetData("public-hostname"); } - } - - /// - /// The elastic IP addresses associated with the interface. - /// - /// - /// There may be multiple IP addresses on an instance. - /// - public IEnumerable PublicIPv4s - { - get { return GetItems("public-ipv4s"); } - } - - /// - /// Security groups to which the network interface belongs. - /// - public IEnumerable SecurityGroups - { - get { return GetItems("security-groups"); } - } - - /// - /// IDs of the security groups to which the network interface belongs. Returned only for Amazon EC2 instances launched into a VPC. - /// - public IEnumerable SecurityGroupIds - { - get { return GetItems("security-group-ids"); } - } - - /// - /// The ID of the Amazon EC2-VPC subnet in which the interface resides. - /// - /// - /// Returned only for Amazon EC2 instances launched into a VPC. - /// - public string SubnetId - { - get { return GetData("subnet-id"); } - } - - /// - /// The CIDR block of the Amazon EC2-VPC subnet in which the interface resides. - /// - /// - /// Returned only for Amazon EC2 instances launched into a VPC. - /// - public string SubnetIPv4CidrBlock - { - get { return GetData("subnet-ipv4-cidr-block"); } - } - - /// - /// The CIDR block of the Amazon EC2-VPC subnet in which the interface resides. - /// - /// - /// Returned only for Amazon EC2 instances launched into a VPC. - /// - public string VpcId - { - get { return GetData("vpc-id"); } - } - - /// - /// Get the private IPv4 address(es) that are associated with the public-ip address and assigned to that interface. - /// - /// The public IP address - /// Private IPv4 address(es) associated with the public IP address - public IEnumerable GetIpV4Association(string publicIp) - { - return EC2Metadata.GetItems(string.Format(CultureInfo.InvariantCulture, "{0}ipv4-associations/{1}", _path, publicIp)); - } - - private string GetData(string key) - { - if (_data.ContainsKey(key)) - return _data[key]; - - // Since the keys are variable, cache a list of which ones are available - // to prevent unnecessary trips to the service. - if (null == _availableKeys) - _availableKeys = EC2Metadata.GetItems(_path); - - if (_availableKeys.Contains(key)) - { - _data[key] = EC2Metadata.GetData(_path + key); - return _data[key]; - } - else - return null; - } - - private IEnumerable GetItems(string key) - { - if (null == _availableKeys) - _availableKeys = EC2Metadata.GetItems(_path); - - if (_availableKeys.Contains(key)) - { - return EC2Metadata.GetItems(_path + key); - } - else - return new List(); - } - } - -} diff --git a/sdk/src/Services/Glacier/Custom/Transfer/Internal/DownloadFileCommand.cs b/sdk/src/Services/Glacier/Custom/Transfer/Internal/DownloadFileCommand.cs index 298f465cffb1..0f8c9e66c971 100644 --- a/sdk/src/Services/Glacier/Custom/Transfer/Internal/DownloadFileCommand.cs +++ b/sdk/src/Services/Glacier/Custom/Transfer/Internal/DownloadFileCommand.cs @@ -15,26 +15,15 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; -using System.Net; -using System.Text; -using System.Threading; - -using Amazon.Glacier.Model; -using Amazon.Glacier.Transfer.Internal; - +using Amazon.Util.Internal; using Amazon.Runtime.Internal; - using Amazon.SimpleNotificationService; -using Amazon.SimpleNotificationService.Model; - using Amazon.SQS; using Amazon.SQS.Model; -using Amazon.SQS.Util; - -using Amazon.Util; -using ThirdParty.Json.LitJson; +#if NET8_0_OR_GREATER +using System.Text.Json.Serialization; +#endif namespace Amazon.Glacier.Transfer.Internal { @@ -114,8 +103,8 @@ string getJobIdFromMessage(Message message) else json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(message.Body)); - Dictionary outerLayer = JsonMapper.ToObject>(json); - Dictionary fields = JsonMapper.ToObject>(outerLayer["Message"]); + Dictionary outerLayer = JsonSerializerHelper.Deserialize>(json, GlacierDictionarySerializerContexts.Default); + Dictionary fields = JsonSerializerHelper.Deserialize>(outerLayer["Message"], GlacierDictionarySerializerContexts.Default); string jobId = fields["JobId"] as string; @@ -171,4 +160,10 @@ public void Dispose() #endregion } + + [JsonSerializable(typeof(Dictionary))] + [JsonSerializable(typeof(Dictionary))] + internal partial class GlacierDictionarySerializerContexts : JsonSerializerContext + { + } } diff --git a/sdk/src/Services/Glacier/Custom/Transfer/Internal/_async/DownloadFileCommand.async.cs b/sdk/src/Services/Glacier/Custom/Transfer/Internal/_async/DownloadFileCommand.async.cs index 82d2aa20ff26..45e7be40db10 100644 --- a/sdk/src/Services/Glacier/Custom/Transfer/Internal/_async/DownloadFileCommand.async.cs +++ b/sdk/src/Services/Glacier/Custom/Transfer/Internal/_async/DownloadFileCommand.async.cs @@ -14,29 +14,13 @@ */ using System; using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Net; -using System.Text; -using System.Threading; +using System.Threading.Tasks; using Amazon.Glacier.Model; -using Amazon.Glacier.Transfer.Internal; - -using Amazon.Runtime.Internal; - -using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; - -using Amazon.SQS; using Amazon.SQS.Model; using Amazon.SQS.Util; -using Amazon.Util; - -using ThirdParty.Json.LitJson; -using System.Threading.Tasks; - namespace Amazon.Glacier.Transfer.Internal { internal partial class DownloadFileCommand : IDisposable diff --git a/sdk/src/Services/Glacier/Custom/Transfer/Internal/_bcl/DownloadFileCommand.bcl.cs b/sdk/src/Services/Glacier/Custom/Transfer/Internal/_bcl/DownloadFileCommand.bcl.cs index 215dbcfdf38f..5e4b53f8b30a 100644 --- a/sdk/src/Services/Glacier/Custom/Transfer/Internal/_bcl/DownloadFileCommand.bcl.cs +++ b/sdk/src/Services/Glacier/Custom/Transfer/Internal/_bcl/DownloadFileCommand.bcl.cs @@ -14,28 +14,13 @@ */ using System; using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Net; -using System.Text; using System.Threading; using Amazon.Glacier.Model; -using Amazon.Glacier.Transfer.Internal; - -using Amazon.Runtime.Internal; - -using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; - -using Amazon.SQS; using Amazon.SQS.Model; using Amazon.SQS.Util; -using Amazon.Util; - -using ThirdParty.Json.LitJson; - namespace Amazon.Glacier.Transfer.Internal { internal partial class DownloadFileCommand : IDisposable diff --git a/sdk/src/Services/S3/Custom/Util/S3EventNotification.cs b/sdk/src/Services/S3/Custom/Util/S3EventNotification.cs index 873d02836e5c..e135dd674f32 100644 --- a/sdk/src/Services/S3/Custom/Util/S3EventNotification.cs +++ b/sdk/src/Services/S3/Custom/Util/S3EventNotification.cs @@ -1,12 +1,9 @@ using System; -using System.Linq; using System.Collections.Generic; using System.Globalization; +using System.Text.Json; using Amazon.Runtime; -using ThirdParty.Json.LitJson; - - namespace Amazon.S3.Util { /// @@ -28,12 +25,13 @@ public static S3EventNotification ParseJson(string json) { try { - var data = JsonMapper.ToObject(json); + using var data = JsonDocument.Parse(json); + var s3Event = new S3EventNotification { Records = new List() }; - if (data["Records"] != null) + if (data.RootElement.TryGetProperty("Records", out var recordsElement) && recordsElement.ValueKind == JsonValueKind.Array) { - foreach (JsonData jsonRecord in data["Records"]) + foreach (var jsonRecord in recordsElement.EnumerateArray()) { var record = new S3EventNotificationRecord(); @@ -41,68 +39,65 @@ public static S3EventNotification ParseJson(string json) record.EventSource = GetValueAsString(jsonRecord, "eventSource"); record.AwsRegion = GetValueAsString(jsonRecord, "awsRegion"); - if (jsonRecord["eventTime"] != null) - record.EventTime = DateTime.Parse((string)jsonRecord["eventTime"], CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); - if (jsonRecord["eventName"] != null) + if (jsonRecord.TryGetProperty("eventTime", out var eventTimeElement) && eventTimeElement.ValueKind == JsonValueKind.String) + { + record.EventTime = DateTime.Parse(eventTimeElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); + } + if (jsonRecord.TryGetProperty("eventName", out var eventNameElement) && eventNameElement.ValueKind == JsonValueKind.String) { - var eventName = (string)jsonRecord["eventName"]; + string eventName = eventNameElement.GetString(); if (!eventName.StartsWith("s3:", StringComparison.OrdinalIgnoreCase)) + { eventName = "s3:" + eventName; - + } record.EventName = EventType.FindValue(eventName); } - if (jsonRecord["userIdentity"] != null) + if (jsonRecord.TryGetProperty("userIdentity", out var jsonUserIdentity)) { - var jsonUserIdentity = jsonRecord["userIdentity"]; record.UserIdentity = new UserIdentityEntity(); record.UserIdentity.PrincipalId = GetValueAsString(jsonUserIdentity, "principalId"); } - if (jsonRecord["requestParameters"] != null) + if (jsonRecord.TryGetProperty("requestParameters", out var jsonRequestParameters)) { - var jsonRequestParameters = jsonRecord["requestParameters"]; record.RequestParameters = new RequestParametersEntity(); record.RequestParameters.SourceIPAddress = GetValueAsString(jsonRequestParameters, "sourceIPAddress"); } - if (jsonRecord["responseElements"] != null) + if (jsonRecord.TryGetProperty("responseElements", out var jsonResponseElements)) { - var jsonResponseElements = jsonRecord["responseElements"]; record.ResponseElements = new ResponseElementsEntity(); record.ResponseElements.XAmzRequestId = GetValueAsString(jsonResponseElements, "x-amz-request-id"); record.ResponseElements.XAmzId2 = GetValueAsString(jsonResponseElements, "x-amz-id-2"); } - if (jsonRecord["s3"] != null) + if (jsonRecord.TryGetProperty("s3", out var jsonS3)) { - var jsonS3 = jsonRecord["s3"]; record.S3 = new S3Entity(); record.S3.S3SchemaVersion = GetValueAsString(jsonS3, "s3SchemaVersion"); record.S3.ConfigurationId = GetValueAsString(jsonS3, "configurationId"); - if (jsonS3["bucket"] != null) + + if (jsonS3.TryGetProperty("bucket", out var jsonBucket)) { - var jsonBucket = jsonS3["bucket"]; record.S3.Bucket = new S3BucketEntity(); record.S3.Bucket.Name = GetValueAsString(jsonBucket, "name"); record.S3.Bucket.Arn = GetValueAsString(jsonBucket, "arn"); - if (jsonBucket["ownerIdentity"] != null) + if (jsonBucket.TryGetProperty("ownerIdentity", out var jsonOwnerIdentity)) { - var jsonOwnerIdentity = jsonBucket["ownerIdentity"]; record.S3.Bucket.OwnerIdentity = new UserIdentityEntity(); record.S3.Bucket.OwnerIdentity.PrincipalId = GetValueAsString(jsonOwnerIdentity, "principalId"); } } - if (jsonS3["object"] != null) + if (jsonS3.TryGetProperty("object", out var jsonObject)) { - var jsonObject = jsonS3["object"]; record.S3.Object = new S3ObjectEntity(); record.S3.Object.Key = GetValueAsString(jsonObject, "key"); @@ -113,15 +108,12 @@ public static S3EventNotification ParseJson(string json) } } - if(jsonRecord["glacierEventData"] != null) + if (jsonRecord.TryGetProperty("glacierEventData", out var jsonGlacier)) { - var jsonGlacier = jsonRecord["glacierEventData"]; record.GlacierEventData = new S3GlacierEventDataEntity(); - if(jsonGlacier["restoreEventData"] != null) + if(jsonGlacier.TryGetProperty("restoreEventData", out var jsonRestore)) { - var jsonRestore = jsonGlacier["restoreEventData"]; - record.GlacierEventData.RestoreEventData = new S3RestoreEventDataEntity(); record.GlacierEventData.RestoreEventData.LifecycleRestorationExpiryTime = GetValueAsDateTime(jsonRestore, "lifecycleRestorationExpiryTime").GetValueOrDefault(); @@ -347,14 +339,14 @@ public class S3EventNotificationRecord } - private static string GetValueAsString(JsonData data, string key) + private static string GetValueAsString(JsonElement data, string key) { - if (data[key] != null) - return (string)data[key]; + if (data.TryGetProperty(key, out var property) && property.ValueKind == JsonValueKind.String) + return property.GetString(); return null; } - private static DateTime? GetValueAsDateTime(JsonData data, string key) + private static DateTime? GetValueAsDateTime(JsonElement data, string key) { var str = GetValueAsString(data, key); if (string.IsNullOrEmpty(str)) @@ -363,16 +355,12 @@ private static string GetValueAsString(JsonData data, string key) return DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); } - private static long GetValueAsLong(JsonData data, string key) + private static long GetValueAsLong(JsonElement data, string key) { - if (data[key] != null) + if (data.TryGetProperty(key, out var property) && + property.ValueKind == JsonValueKind.Number && property.TryGetInt64(out long result)) { - if (data[key].IsInt) - return (int)data[key]; - else if (data[key].IsUInt) - return (uint)data[key]; - else - return (long)data[key]; + return result; } return 0; } diff --git a/sdk/src/Services/S3/Custom/Util/_bcl/S3PostUploadSignedPolicy.cs b/sdk/src/Services/S3/Custom/Util/_bcl/S3PostUploadSignedPolicy.cs index f40b8b5ab035..3ea72e7bb232 100644 --- a/sdk/src/Services/S3/Custom/Util/_bcl/S3PostUploadSignedPolicy.cs +++ b/sdk/src/Services/S3/Custom/Util/_bcl/S3PostUploadSignedPolicy.cs @@ -26,9 +26,10 @@ using Amazon.Runtime; using Amazon.Util; -using ThirdParty.Json.LitJson; using System.Globalization; using System.Collections.Generic; +using System.Text.Json.Nodes; +using System.Text.Json; namespace Amazon.S3.Util { @@ -116,63 +117,64 @@ public static S3PostUploadSignedPolicy GetSignedPolicyV4(string policy, AWSCrede private static byte[] addConditionsToPolicy(string policy, Dictionary newConditions) { - var json = JsonMapper.ToObject(new JsonReader(policy)); + var json = JsonNode.Parse(policy) as JsonObject; + var jsonConditions = json["conditions"] as JsonArray; - var jsonConditions = json["conditions"]; - - if (jsonConditions != null && jsonConditions.IsArray) + if (jsonConditions != null) { foreach (var newCond in newConditions) { bool found = false; for (int i = 0; i < jsonConditions.Count; i++) { - JsonData jsonCond = jsonConditions[i]; - if (jsonCond.IsObject && jsonCond[newCond.Key] != null) + if (jsonConditions[i] is JsonObject obj && obj.ContainsKey(newCond.Key)) { - jsonCond[newCond.Key] = newCond.Value; + obj[newCond.Key] = newCond.Value; found = true; } } + if (!found) { - var jsonCond = new JsonData(); - jsonCond.SetJsonType(ThirdParty.Json.LitJson.JsonType.Object); - jsonCond[newCond.Key] = newCond.Value; - jsonConditions.Add(jsonCond); + var newCondition = new JsonObject + { + [newCond.Key] = newCond.Value + }; + jsonConditions.Add(newCondition); } } } - return Encoding.UTF8.GetBytes(JsonMapper.ToJson(json).Trim()); + + return Encoding.UTF8.GetBytes(json.ToJsonString().Trim()); } private static byte[] addTokenToPolicy(string policy, string token) { - var json = JsonMapper.ToObject(new JsonReader(policy)); + var json = JsonNode.Parse(policy) as JsonObject; var found = false; - var conditions = json["conditions"]; - if (conditions != null && conditions.IsArray) + var conditions = json["conditions"] as JsonArray; + if (conditions != null) { for (int i = 0; i < conditions.Count; i++) { - JsonData cond = conditions[i]; - if (cond.IsObject && cond[S3Constants.PostFormDataSecurityToken] != null) + if (conditions[i] is JsonObject obj && obj.ContainsKey(S3Constants.PostFormDataSecurityToken)) { - cond[S3Constants.PostFormDataSecurityToken] = token; + obj[S3Constants.PostFormDataSecurityToken] = token; found = true; } } if (!found) { - var tokenCondition = new JsonData(); - tokenCondition.SetJsonType(ThirdParty.Json.LitJson.JsonType.Object); - tokenCondition[S3Constants.PostFormDataSecurityToken] = token; + var tokenCondition = new JsonObject + { + [S3Constants.PostFormDataSecurityToken] = token + }; conditions.Add(tokenCondition); } } - return Encoding.UTF8.GetBytes(JsonMapper.ToJson(json).Trim()); + return Encoding.UTF8.GetBytes(json.ToJsonString().Trim()); } /// @@ -238,13 +240,14 @@ private static string /// JSON string public string ToJson() { - var json = new JsonData(); - - json[KEY_POLICY] = this.Policy; - json[KEY_SIGNATURE] = this.Signature; - json[KEY_ACCESSKEY] = this.AccessKeyId; + var json = new JsonObject + { + [KEY_POLICY] = this.Policy, + [KEY_SIGNATURE] = this.Signature, + [KEY_ACCESSKEY] = this.AccessKeyId + }; - return JsonMapper.ToJson(json); + return json.ToJsonString(); } /// @@ -269,26 +272,32 @@ public string ToXml() /// Instance of S3PostUploadSignedPolicy public static S3PostUploadSignedPolicy GetSignedPolicyFromJson(string policyJson) { - JsonData json; - try { json = JsonMapper.ToObject(policyJson); } - catch (Exception e) + try { - throw new ArgumentException("Invalid JSON document", e); - } + using (var doc = JsonDocument.Parse(policyJson)) + { + var json = doc.RootElement; - if (null == json[KEY_POLICY] || !json[KEY_POLICY].IsString) - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_POLICY); - if (null == json[KEY_SIGNATURE] || !json[KEY_SIGNATURE].IsString) - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_SIGNATURE); - if (null == json[KEY_ACCESSKEY] || !json[KEY_ACCESSKEY].IsString) - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_ACCESSKEY); + // Check if required fields exist and are of the correct type + if (!json.TryGetProperty(KEY_POLICY, out var policyNode) || policyNode.ValueKind != JsonValueKind.String) + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_POLICY); + if (!json.TryGetProperty(KEY_SIGNATURE, out var signatureNode) || signatureNode.ValueKind != JsonValueKind.String) + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_SIGNATURE); + if (!json.TryGetProperty(KEY_ACCESSKEY, out var accessKeyNode) || accessKeyNode.ValueKind != JsonValueKind.String) + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "JSON document requires '{0}' field"), KEY_ACCESSKEY); - return new S3PostUploadSignedPolicy + return new S3PostUploadSignedPolicy + { + Policy = policyNode.GetString(), + Signature = signatureNode.GetString(), + AccessKeyId = accessKeyNode.GetString() + }; + } + } + catch (JsonException e) { - Policy = json[KEY_POLICY].ToString(), - Signature = json[KEY_SIGNATURE].ToString(), - AccessKeyId = json[KEY_ACCESSKEY].ToString() - }; + throw new ArgumentException("Invalid JSON document", e); + } } /// diff --git a/sdk/src/Services/SimpleNotificationService/Custom/Util/Message.cs b/sdk/src/Services/SimpleNotificationService/Custom/Util/Message.cs index 069f523d63b2..db86af12a5ec 100644 --- a/sdk/src/Services/SimpleNotificationService/Custom/Util/Message.cs +++ b/sdk/src/Services/SimpleNotificationService/Custom/Util/Message.cs @@ -10,11 +10,12 @@ using Amazon.Runtime; using Amazon.Util; -using ThirdParty.Json.LitJson; +using System.Text.Json; using Amazon.Runtime.Internal.Util; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; +using Amazon.Util.Internal; namespace Amazon.SimpleNotificationService.Util { @@ -54,18 +55,21 @@ private Message() public static Message ParseMessage(string messageText) { var message = new Message(); - - var jsonData = JsonMapper.ToObject(messageText); + JsonDocument jsonData; + jsonData = JsonDocument.Parse(messageText); Func extractField = ((fieldName) => { - if (jsonData[fieldName] != null && jsonData[fieldName].IsString) - return (string)jsonData[fieldName]; - + if (jsonData.RootElement.TryGetProperty(fieldName, out var value) && value.ValueKind == JsonValueKind.String) + { + return value.GetString(); + } + var anyCaseKey = jsonData.RootElement.EnumerateObject().FirstOrDefault(x => string.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); // Check to see if the field can be found with a different case. - var anyCaseKey = jsonData.PropertyNames.FirstOrDefault(x => string.Equals(x, fieldName, StringComparison.OrdinalIgnoreCase)); - if (!string.IsNullOrEmpty(anyCaseKey) && jsonData[anyCaseKey] != null && jsonData[anyCaseKey].IsString) - return (string)jsonData[anyCaseKey]; + if (!string.IsNullOrEmpty(anyCaseKey.Value.ToString()) && anyCaseKey.Value.ValueKind == JsonValueKind.String) + { + return anyCaseKey.Value.GetString(); + } return null; }); diff --git a/sdk/test/IntegrationTests/Tests/General.cs b/sdk/test/IntegrationTests/Tests/General.cs index b7dd6b8238b2..43741c9c9027 100644 --- a/sdk/test/IntegrationTests/Tests/General.cs +++ b/sdk/test/IntegrationTests/Tests/General.cs @@ -28,6 +28,7 @@ using Amazon.DynamoDBv2; using Amazon.ElasticTranscoder; using System.Threading; +using System.Text.Json; using AWSSDK_DotNet.CommonTest.Utils; namespace AWSSDK_DotNet.IntegrationTests.Tests @@ -460,12 +461,12 @@ private static void TestException(Exception e) public void JsonCountSerializationBug() { var json = @"{""Data"":{""NotCount"":""42""}}"; - var poco = ThirdParty.Json.LitJson.JsonMapper.ToObject(json); + var poco = JsonSerializer.Deserialize(json); Assert.AreEqual(1, poco.Data.Count); Assert.AreEqual("42", poco.Data["NotCount"]); json = @"{""Data"":{""Count"":""Dracula""}}"; - poco = ThirdParty.Json.LitJson.JsonMapper.ToObject(json); + poco = JsonSerializer.Deserialize(json); Assert.AreEqual(1, poco.Data.Count); Assert.AreEqual("Dracula", poco.Data["Count"]); } diff --git a/sdk/test/IntegrationTests/Tests/NetFramework/TestNetFramework.cs b/sdk/test/IntegrationTests/Tests/NetFramework/TestNetFramework.cs index 719799ccd75c..874193b77680 100644 --- a/sdk/test/IntegrationTests/Tests/NetFramework/TestNetFramework.cs +++ b/sdk/test/IntegrationTests/Tests/NetFramework/TestNetFramework.cs @@ -12,6 +12,9 @@ using System.Configuration; using System.Collections.Specialized; using System.Xml; +using System.IO; +using System.Text.Json; +using System.Text; namespace AWSSDK_DotNet.IntegrationTests.Tests @@ -83,11 +86,19 @@ public void TestResponseMocking() httpResponse.Headers.Remove("x-amz-crc32"); // modify body - var json = ThirdParty.Json.LitJson.JsonMapper.ToObject(httpResponse.Body); - var tableNames = json["TableNames"]; - tableNames.Clear(); - tableNames.Add("Logs"); - httpResponse.Body = json.ToJson(); + using (var memoryStream = new MemoryStream()) + using (var writer = new Utf8JsonWriter(memoryStream)) + { + writer.WriteStartObject(); + writer.WritePropertyName("TableNames"); + writer.WriteStartArray(); + writer.WriteStringValue("Logs"); // Add a new table name + writer.WriteEndArray(); + writer.WriteEndObject(); + writer.Flush(); + var newBody = Encoding.UTF8.GetString(memoryStream.ToArray()); + httpResponse.Body = newBody; + } return httpResponse; }; diff --git a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/ArchiveTransferManagerTests.cs b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/ArchiveTransferManagerTests.cs index d92f12de352d..60bfb08769fe 100644 --- a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/ArchiveTransferManagerTests.cs +++ b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/ArchiveTransferManagerTests.cs @@ -10,7 +10,6 @@ using Amazon.Glacier.Transfer; using Amazon.Runtime; using Xunit; -using ThirdParty.Json.LitJson; using Amazon; using System.Threading.Tasks; using System.Security.Cryptography; diff --git a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/DynamoDB/DocumentTests.cs b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/DynamoDB/DocumentTests.cs index 07cf853a2695..9228953306be 100644 --- a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/DynamoDB/DocumentTests.cs +++ b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/DynamoDB/DocumentTests.cs @@ -8,7 +8,6 @@ using Amazon.DynamoDBv2.Model; using Amazon.DynamoDBv2.DocumentModel; using System.IO; -using ThirdParty.Json.LitJson; using System.Xml; using Xunit; diff --git a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/SNS.cs b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/SNS.cs index bf4bef98b667..e24c67f9a493 100644 --- a/sdk/test/NetStandard/IntegrationTests/IntegrationTests/SNS.cs +++ b/sdk/test/NetStandard/IntegrationTests/IntegrationTests/SNS.cs @@ -7,8 +7,7 @@ using System.Threading; using System.Threading.Tasks; -using ThirdParty.Json.LitJson; - +using System.Text.Json; using Amazon.Runtime.SharedInterfaces; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; @@ -276,11 +275,30 @@ public async void IsMessageSignatureValidInvalidSignatureVersion() var bodyJson = GetBodyJson(messages[0]); // modify message to have invalid SignatureVersion - var jsonData = JsonMapper.ToObject(bodyJson); - jsonData["SignatureVersion"] = "3"; + var jsonData = JsonDocument.Parse(bodyJson); + using var memoryStream = new MemoryStream(); + using (var writer = new Utf8JsonWriter(memoryStream)) + { + writer.WriteStartObject(); + foreach (var property in jsonData.RootElement.EnumerateObject()) + { + if (property.Name == "SignatureVersion") + { + writer.WriteString("SignatureVersion", "3"); + } + else + { + property.WriteTo(writer); + } + } - var ex = Assert.Throws(() => Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData.ToJson())); - Assert.Equal("SignatureVersion is not a valid value", ex.Message); + writer.WriteEndObject(); + writer.Flush(); + string updatedJson = Encoding.UTF8.GetString(memoryStream.ToArray()); + + var ex = Assert.Throws(() => Amazon.SimpleNotificationService.Util.Message.ParseMessage(updatedJson)); + Assert.Equal("SignatureVersion is not a valid value", ex.Message); + } } [Fact] @@ -298,21 +316,21 @@ public async void TestQueueSubscription() string bodyJson = GetBodyJson(message); - var json = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson); - var messageText = json["Message"]; - var messageSubject = json["Subject"]; + var json = JsonDocument.Parse(bodyJson); + var messageText = json.RootElement.GetProperty("Message"); + var messageSubject = json.RootElement.GetProperty("Subject"); Assert.Equal(publishRequest.Message, messageText.ToString()); Assert.Equal(publishRequest.Subject, messageSubject.ToString()); - var messageAttributes = json["MessageAttributes"]; - Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.Count); + var messageAttributes = json.RootElement.GetProperty("MessageAttributes"); + Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.EnumerateObject().Count()); foreach (var ma in publishRequest.MessageAttributes) { var name = ma.Key; var value = ma.Value; - Assert.Contains(name, messageAttributes.PropertyNames, StringComparer.Ordinal); - var jsonAttribute = messageAttributes[name]; - var jsonType = jsonAttribute["Type"].ToString(); - var jsonValue = jsonAttribute["Value"].ToString(); + Assert.Contains(name, messageAttributes.EnumerateObject().Select(x => x.Name), StringComparer.Ordinal); + var jsonAttribute = messageAttributes.GetProperty(name); + var jsonType = jsonAttribute.GetProperty("Type").ToString(); + var jsonValue = jsonAttribute.GetProperty("Value").ToString(); Assert.NotNull(jsonType); Assert.NotNull(jsonValue); Assert.Equal(value.DataType, jsonType); diff --git a/sdk/test/Performance/EC2PerformanceBenchmarks/JsonSampleGenerator.cs b/sdk/test/Performance/EC2PerformanceBenchmarks/JsonSampleGenerator.cs deleted file mode 100644 index 88e4b14739c4..000000000000 --- a/sdk/test/Performance/EC2PerformanceBenchmarks/JsonSampleGenerator.cs +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file 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. - */ -using Amazon.Util; -using ServiceClientGenerator; -using System; -using System.Diagnostics.Metrics; -using System.Globalization; -using System.Linq; -using ThirdParty.Json.LitJson; - -namespace AWSSDK.Benchmarks -{ - public class JsonSampleGenerator - { - - ServiceModel _model; - Shape _rootStructure; - TypeCircularReference _tcr; - - public JsonSampleGenerator(ServiceModel model, Shape rootStructure) - { - this._model = model; - this._rootStructure = rootStructure; - } - - public string Execute() - { - this._tcr = new TypeCircularReference(); - JsonWriter writer = new JsonWriter(); - writer.PrettyPrint = true; - - WriteStructure(writer, null, this._rootStructure); - - var json = writer.ToString(); - return json; - } - - private void Write(JsonWriter writer, Member member, Shape shape) - { - if (shape.IsStructure) - WriteStructure(writer, member, shape); - else if (shape.IsList) - WriteArray(writer, member, shape); - else if (shape.IsMap) - WriteMap(writer, member, shape); - else if (shape.IsEnum) - { - var enumerationWrapper = this._model.Enumerations(true).First(x => x.Name == shape.Name); - writer.Write(enumerationWrapper.EnumerationValues.ElementAt(0).MarshallName); - } - else if (shape.IsString) - writer.Write(shape.Name + "_Value"); - else if (shape.IsInt) - writer.Write(int.MaxValue); - else if (shape.IsLong) - writer.Write(long.MaxValue); - else if (shape.IsDouble) - writer.Write(double.MaxValue); - else if (shape.IsFloat) - writer.Write(float.MaxValue); - else if (shape.IsDateTime) - { - writer.Write(XmlSampleGenerator.GetTestDate(member, shape)); - } - else if (shape.IsBoolean) - writer.Write(true); - else if (shape.IsMemoryStream) - writer.Write(Constants.DefaultBlobEncoded); - else - throw new Exception("Unknown Type for shape " + shape.Name); - } - - - private void WriteStructure(JsonWriter writer, Member memberWithComplexShape, Shape structure) - { - var pushed = this._tcr.Push(structure.Name); - if (!pushed) - { - // Circular reference found. Closing the structure - writer.WriteObjectStart(); - writer.WriteObjectEnd(); - return; - } - - if (structure.PayloadMemberName != null) - { - this.WriteStructure(writer, structure.PayloadMember, structure.PayloadMember.Shape); - return; - } - - writer.WriteObjectStart(); - - foreach (var member in structure.Members) - { - writer.WritePropertyName(member.MarshallName); - - if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "DateTimeEpochLongMillisecondsUnmarshaller")) - { - writer.Write(TimeSpan.FromTicks(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks).TotalMilliseconds); - } - else if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "Amazon.Runtime.Internal.Transform.DateTimeUnmarshaller")) - { - writer.Write(Constants.DefaultDate.ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture)); - } - else if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "Amazon.Runtime.Internal.Transform.DecimalUnmarshaller")) - { - writer.Write(Constants.DefaultDecimal.ToString(CultureInfo.InvariantCulture)); - } - else - { - this.Write(writer, member, member.Shape); - } - } - - writer.WriteObjectEnd(); - - if (pushed) - this._tcr.Pop(); - } - - - private void WriteArray(JsonWriter writer, Member member, Shape array) - { - - writer.WriteArrayStart(); - - var listShape = array.ListShape; - if (!listShape.IsStructure || !this._tcr.Contains(listShape.Name)) - { - for (int i = 0; i < array.Name.Length % 5 + 2; i++) - { - Write(writer, member, listShape); - } - } - - writer.WriteArrayEnd(); - } - - private void WriteMap(JsonWriter writer, Member member, Shape map) - { - - writer.WriteObjectStart(); - - var mapShape = map.ValueShape; - if (!mapShape.IsStructure || !this._tcr.Contains(mapShape.Name)) - { - for (int i = 0; i < map.Name.Length % 5 + 2; i++) - { - writer.WritePropertyName("key" + i); - Write(writer, member, map.ValueShape); - } - } - - writer.WriteObjectEnd(); - } - } -} diff --git a/sdk/test/Performance/EC2PerformanceBenchmarks/MarshallAndUnmarshallBenchmarks.cs b/sdk/test/Performance/EC2PerformanceBenchmarks/MarshallAndUnmarshallBenchmarks.cs index fa08c50b770f..aa6472892a26 100644 --- a/sdk/test/Performance/EC2PerformanceBenchmarks/MarshallAndUnmarshallBenchmarks.cs +++ b/sdk/test/Performance/EC2PerformanceBenchmarks/MarshallAndUnmarshallBenchmarks.cs @@ -36,6 +36,7 @@ using System; using System.Diagnostics; using System.IO; +using AWSSDK_DotNet.UnitTests.TestTools; namespace AWSSDK.Benchmarks { public class MarshallAndUnmarshallBenchmarks diff --git a/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.csproj b/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.csproj index 350aef836297..424761351134 100644 --- a/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.csproj +++ b/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.csproj @@ -32,6 +32,7 @@ + diff --git a/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.sln b/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.sln index 5fbf57ac2f10..6fcb85721c4d 100644 --- a/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.sln +++ b/sdk/test/Performance/EC2PerformanceBenchmarks/Performance.sln @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWSSDK.SQS.NetStandard", ". EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceClientGenerator", "..\..\..\..\generator\ServiceClientGenerator\ServiceClientGenerator.csproj", "{D3C088B4-9D88-4718-B956-B7682DE5614D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWSSDK.UnitTestUtilities.NetStandard", "..\..\UnitTests\Custom\AWSSDK.UnitTestUtilities.NetStandard.csproj", "{0162C212-EBE7-4175-9E32-5174B86F414B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -69,6 +71,10 @@ Global {D3C088B4-9D88-4718-B956-B7682DE5614D}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3C088B4-9D88-4718-B956-B7682DE5614D}.Release|Any CPU.ActiveCfg = Release|Any CPU {D3C088B4-9D88-4718-B956-B7682DE5614D}.Release|Any CPU.Build.0 = Release|Any CPU + {0162C212-EBE7-4175-9E32-5174B86F414B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0162C212-EBE7-4175-9E32-5174B86F414B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0162C212-EBE7-4175-9E32-5174B86F414B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0162C212-EBE7-4175-9E32-5174B86F414B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/sdk/test/Services/BedrockRuntime/IntegrationTests/BedrockRuntimeEventStreamTests.cs b/sdk/test/Services/BedrockRuntime/IntegrationTests/BedrockRuntimeEventStreamTests.cs index 2272ad04046c..b5205a18466d 100644 --- a/sdk/test/Services/BedrockRuntime/IntegrationTests/BedrockRuntimeEventStreamTests.cs +++ b/sdk/test/Services/BedrockRuntime/IntegrationTests/BedrockRuntimeEventStreamTests.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; using System.IO; using System.Text; -using ThirdParty.Json.LitJson; +using System.Text.Json; using System.Threading; using System; using System.Diagnostics.Contracts; @@ -20,8 +20,8 @@ namespace AWSSDK_DotNet.IntegrationTests.Tests /// /// Because this test requires explicit access to the models it is ignored. /// - [Ignore] [TestClass] + [Ignore] public class BedrockRuntimeEventStreamTests : TestBase { #if BCL @@ -102,7 +102,6 @@ public async Task RequestWithInvalidBodyReturnsValidationException() Assert.IsTrue(exceptionReceived); Assert.IsFalse(chunkReceived); } - #endif static MemoryStream CreateStream(string query, bool createInvalidInput = false) { @@ -115,23 +114,22 @@ static MemoryStream CreateStream(string query, bool createInvalidInput = false) promptValueBuilder.Append("."); promptValueBuilder.AppendLine(); promptValueBuilder.AppendLine("Assistant: "); - - - var stream = new MemoryStream( - Encoding.UTF8.GetBytes( - JsonMapper.ToJson(new - { - prompt = promptValueBuilder.ToString(), - max_tokens_to_sample = 300 - } - ) - ) - ); - + MemoryStream stream = new MemoryStream(); + AnthropicClaudeV2Json jsonObject = new AnthropicClaudeV2Json + { + prompt = promptValueBuilder.ToString(), + max_tokens_to_sample = 300 + }; + JsonSerializer.Serialize(stream, jsonObject, typeof(AnthropicClaudeV2Json)); stream.Position = 0; return stream; } + private class AnthropicClaudeV2Json + { + public string prompt { get; set; } + public int max_tokens_to_sample { get; set; } + } } } diff --git a/sdk/test/Services/DynamoDBv2/IntegrationTests/CSMTest.cs b/sdk/test/Services/DynamoDBv2/IntegrationTests/CSMTest.cs index b203f019b471..87d1d7b833c5 100644 --- a/sdk/test/Services/DynamoDBv2/IntegrationTests/CSMTest.cs +++ b/sdk/test/Services/DynamoDBv2/IntegrationTests/CSMTest.cs @@ -10,7 +10,6 @@ using Amazon.DynamoDBv2.DocumentModel; using Amazon.DynamoDBv2.Model; using Amazon.Util; -using ThirdParty.Json.LitJson; using AWSSDK_DotNet.IntegrationTests.Utils; namespace AWSSDK_DotNet.IntegrationTests.Tests diff --git a/sdk/test/Services/DynamoDBv2/IntegrationTests/TTLTests.cs b/sdk/test/Services/DynamoDBv2/IntegrationTests/TTLTests.cs index 97e52dda5b70..dbd148949fe8 100644 --- a/sdk/test/Services/DynamoDBv2/IntegrationTests/TTLTests.cs +++ b/sdk/test/Services/DynamoDBv2/IntegrationTests/TTLTests.cs @@ -13,7 +13,6 @@ using Amazon.DynamoDBv2.DocumentModel; using Amazon.DynamoDBv2.Model; using Amazon.Util; -using ThirdParty.Json.LitJson; using AWSSDK_DotNet.IntegrationTests.Utils; namespace AWSSDK_DotNet.IntegrationTests.Tests.DynamoDB diff --git a/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs index e0081c9ab37a..4e00d4fa14f2 100644 --- a/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs +++ b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs @@ -10,9 +10,9 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using ThirdParty.Json.LitJson; namespace AWSSDK_DotNet.UnitTests { @@ -207,12 +207,12 @@ public void DocumentFromJsonTest() Assert.AreEqual(1.7976931348623157E+308, dynamoDBDocument["PDouble"].AsDouble()); Assert.AreEqual(-1.7976931348623157E+308, dynamoDBDocument["NDouble"].AsDouble()); - JsonData jsonOriginal = JsonMapper.ToObject(jsonDocument); - JsonData jsonNew = JsonMapper.ToObject(dynamoDBDocument.ToJson()); + JsonDocument jsonOriginal = JsonDocument.Parse(jsonDocument); + JsonDocument jsonNew = JsonDocument.Parse(dynamoDBDocument.ToJson()); - foreach (string property in jsonOriginal.PropertyNames) + foreach (var property in jsonOriginal.RootElement.EnumerateObject()) { - Assert.AreEqual(jsonOriginal[property].ToString(), jsonNew[property].ToString()); + Assert.AreEqual(property.Value.ToString(), jsonNew.RootElement.GetProperty(property.Name).ToString()); } } @@ -232,11 +232,13 @@ public void TestDateTimeDeserializationWithDdbContext() var dateWithDecimals = "2022-05-05T11:56:11.000Z"; var expectedDateDecimal = DateTime.Parse(dateWithDecimals, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); - var jsonDateWithNoDecimals = JsonMapper.ToJson(new + var jsonDateWithNoDecimals = JsonSerializer.Serialize(new { DateFromString = dateWithNoDecimals }); - var jsonDateWithDecimals = JsonMapper.ToJson(new + + + var jsonDateWithDecimals = JsonSerializer.Serialize(new { DateFromString = dateWithDecimals }); diff --git a/sdk/test/Services/Polly/IntegrationTests/PollyTests.cs b/sdk/test/Services/Polly/IntegrationTests/PollyTests.cs index a5d5161986d3..1f2d0ee04100 100644 --- a/sdk/test/Services/Polly/IntegrationTests/PollyTests.cs +++ b/sdk/test/Services/Polly/IntegrationTests/PollyTests.cs @@ -23,8 +23,9 @@ using System; using System.Collections.Generic; using System.IO; -using ThirdParty.Json.LitJson; using System.Text; +using System.Text.Json; +using System.Linq; namespace AWSSDK_DotNet.IntegrationTests.Tests.Polly { @@ -53,7 +54,8 @@ public void APIWithSpeechMarks() Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); using (var streamReader = new StreamReader(response.AudioStream)) { - AssertSpeechMarks(JsonMapper.ToObject(streamReader)); + string text = streamReader.ReadToEnd(); // read the stream to the end to make sure it's valid JSON + AssertSpeechMarks(text); } } } @@ -70,7 +72,8 @@ public void HappyCasePresignedUrl() public void PresignedUrlWithSpeechMarks() { var data = AssertPreSignedUrl(SynthesizeSpeechUtil.GeneratePresignedUrl(RegionEndpoint.USWest2, GetSpeechMarkRequest())); - AssertSpeechMarks(JsonMapper.ToObject(Encoding.UTF8.GetString(data))); + /*{\"time\":0,\"type\":\"sentence\",\"start\":0,\"end\":5,\"value\":\"Hello\"}\n{\"time\":6,\"type\":\"word\",\"start\":0,\"end\":5,\"value\":\"Hello\"}\n"*/ + AssertSpeechMarks(Encoding.UTF8.GetString(data)); } [TestMethod] @@ -95,14 +98,19 @@ public void EnsureIsUrlEncoded() AssertPreSignedUrl(SynthesizeSpeechUtil.GeneratePresignedUrl(RegionEndpoint.USWest2, GetMp3Request())); } - private static void AssertSpeechMarks(JsonData speechMarksJsonData) + private static void AssertSpeechMarks(string speechMarksString) { - var names = new HashSet(speechMarksJsonData.PropertyNames); - Assert.IsTrue(names.Contains("time")); - Assert.IsTrue(names.Contains("type")); - Assert.IsTrue(names.Contains("start")); - Assert.IsTrue(names.Contains("end")); - Assert.IsTrue(names.Contains("value")); + string[] speechMarksJsonObjects = speechMarksString.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + foreach (string speechMarksJsonObject in speechMarksJsonObjects) + { + var jsonDoc = JsonDocument.Parse(speechMarksJsonObject); + IEnumerable propertyNames = jsonDoc.RootElement.EnumerateObject().Select(x => x.Name); + Assert.IsTrue(propertyNames.Contains("time")); + Assert.IsTrue(propertyNames.Contains("type")); + Assert.IsTrue(propertyNames.Contains("start")); + Assert.IsTrue(propertyNames.Contains("end")); + Assert.IsTrue(propertyNames.Contains("value")); + } } private static byte[] AssertPreSignedUrl(string url) diff --git a/sdk/test/Services/Polly/UnitTests/Custom/PollyTests.cs b/sdk/test/Services/Polly/UnitTests/Custom/PollyTests.cs index 19cb654379a1..e0173d3b1144 100644 --- a/sdk/test/Services/Polly/UnitTests/Custom/PollyTests.cs +++ b/sdk/test/Services/Polly/UnitTests/Custom/PollyTests.cs @@ -19,7 +19,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Linq; -using ThirdParty.Json.LitJson; namespace AWSSDK.UnitTests { diff --git a/sdk/test/Services/SimpleNotificationService/IntegrationTests/SNS.cs b/sdk/test/Services/SimpleNotificationService/IntegrationTests/SNS.cs index 2f3e441d2781..abe03cebba1d 100644 --- a/sdk/test/Services/SimpleNotificationService/IntegrationTests/SNS.cs +++ b/sdk/test/Services/SimpleNotificationService/IntegrationTests/SNS.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Text.Json; using Amazon.Auth.AccessControlPolicy; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Threading; @@ -324,21 +325,22 @@ public void TestQueueSubscription() string bodyJson = GetBodyJson(message); - var json = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson); - var messageText = json["Message"]; - var messageSubject = json["Subject"]; + var json = JsonDocument.Parse(bodyJson); + var messageText = json.RootElement.GetProperty("Message"); + var messageSubject = json.RootElement.GetProperty("Subject"); Assert.AreEqual(publishRequest.Message, messageText.ToString()); Assert.AreEqual(publishRequest.Subject, messageSubject.ToString()); - var messageAttributes = json["MessageAttributes"]; - Assert.AreEqual(publishRequest.MessageAttributes.Count, messageAttributes.Count); + var messageAttributes = json.RootElement.GetProperty("MessageAttributes"); + Assert.AreEqual(publishRequest.MessageAttributes.Count, messageAttributes.EnumerateObject().Count()); + foreach (var ma in publishRequest.MessageAttributes) { var name = ma.Key; var value = ma.Value; - Assert.IsTrue(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal)); - var jsonAttribute = messageAttributes[name]; - var jsonType = jsonAttribute["Type"].ToString(); - var jsonValue = jsonAttribute["Value"].ToString(); + Assert.IsTrue(messageAttributes.EnumerateObject().Any(x => x.Name == name)); + var jsonAttribute = messageAttributes.GetProperty(name); + var jsonType = jsonAttribute.GetProperty("Type").ToString(); + var jsonValue = jsonAttribute.GetProperty("Value").ToString(); Assert.IsNotNull(jsonType); Assert.IsNotNull(jsonValue); Assert.AreEqual(value.DataType, jsonType); diff --git a/sdk/test/Services/SimpleNotificationService/UnitTests/Custom/SNSMessageUtilityTests.cs b/sdk/test/Services/SimpleNotificationService/UnitTests/Custom/SNSMessageUtilityTests.cs index 26269bf46251..d168e64fe482 100644 --- a/sdk/test/Services/SimpleNotificationService/UnitTests/Custom/SNSMessageUtilityTests.cs +++ b/sdk/test/Services/SimpleNotificationService/UnitTests/Custom/SNSMessageUtilityTests.cs @@ -59,7 +59,7 @@ public void HandleMixedCaseKeys() var expectedCase = "{" + " \"TopicArn\" : \"" + value + "\"," + -" \"SigningCertURL\" : \"https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem\"" + +" \"SigningCertURL\" : \"https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem\"," + " \"SignatureVersion\" : \"1\"" + "}"; diff --git a/sdk/test/UnitTests/Custom/Runtime/Credentials/EncryptedStoreTestFixture.cs b/sdk/test/UnitTests/Custom/Runtime/Credentials/EncryptedStoreTestFixture.cs index 2ca7c806dcc0..7db3b62c2978 100644 --- a/sdk/test/UnitTests/Custom/Runtime/Credentials/EncryptedStoreTestFixture.cs +++ b/sdk/test/UnitTests/Custom/Runtime/Credentials/EncryptedStoreTestFixture.cs @@ -20,7 +20,7 @@ using System.Diagnostics; using System.IO; using System.Linq; -using ThirdParty.Json.LitJson; +using System.Text.Json; namespace AWSSDK.UnitTests { @@ -117,8 +117,8 @@ public int GetObjectCount() public int GetObjectCount(string filename) { - var rootJsonData = JsonMapper.ToObject(new JsonReader(File.ReadAllText(Path.Combine(DirectoryPath, filename)))); - return rootJsonData.PropertyNames.Count(); + JsonDocument rootJsonData = JsonDocument.Parse(File.ReadAllText(Path.Combine(DirectoryPath, filename))); + return rootJsonData.RootElement.EnumerateObject().Count(); } public void AssertJsonProperty(string displayName, string propertyName, string propertyValue) @@ -128,30 +128,29 @@ public void AssertJsonProperty(string displayName, string propertyName, string p public void AssertJsonProperty(string filename, string displayName, string propertyName, string propertyValue) { - var rootJsonData = JsonMapper.ToObject(new JsonReader(File.ReadAllText(Path.Combine(DirectoryPath, filename)))); + JsonDocument rootJsonData = JsonDocument.Parse(File.ReadAllText(Path.Combine(DirectoryPath, filename))); var profileJsonData = GetObjectJsonData(displayName, rootJsonData); - Assert.IsTrue(profileJsonData.IsObject); - foreach (string profilePropertyName in profileJsonData.PropertyNames) + Assert.IsTrue(profileJsonData.ValueKind == JsonValueKind.Object); + foreach (var profilePropertyName in profileJsonData.EnumerateObject()) { - if (string.Equals(propertyName, profilePropertyName, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(propertyName, profilePropertyName.Name, StringComparison.OrdinalIgnoreCase)) { - Assert.AreEqual(propertyValue, profileJsonData[profilePropertyName].ToString()); + Assert.AreEqual(propertyValue, profileJsonData.GetProperty(profilePropertyName.Name).GetString()); break; } } } - public JsonData GetObjectJsonData(string displayName, JsonData rootJsonData) + public JsonElement GetObjectJsonData(string displayName, JsonDocument rootJsonData) { - foreach (string propertyName in rootJsonData.PropertyNames) + foreach (var property in rootJsonData.RootElement.EnumerateObject()) { - JsonData profileJsonData = rootJsonData[propertyName]; - Assert.IsTrue(profileJsonData.IsObject); - if (string.Equals(propertyName, displayName, StringComparison.OrdinalIgnoreCase) || - string.Equals(profileJsonData[DisplayNameField].ToString(), displayName, StringComparison.OrdinalIgnoreCase)) + Assert.IsTrue(property.Value.ValueKind == JsonValueKind.Object); + if (string.Equals(property.Name,displayName, StringComparison.OrdinalIgnoreCase) || + string.Equals(property.Value.GetProperty(DisplayNameField).GetString(), displayName, StringComparison.OrdinalIgnoreCase)) { - return profileJsonData; + return property.Value; } } throw new Exception("Profile " + displayName + " not found."); diff --git a/sdk/test/UnitTests/Custom/Runtime/Credentials/ProcessAWSCredentialsTest.cs b/sdk/test/UnitTests/Custom/Runtime/Credentials/ProcessAWSCredentialsTest.cs index 5a9dcfba5fc1..72eac2f01fd3 100644 --- a/sdk/test/UnitTests/Custom/Runtime/Credentials/ProcessAWSCredentialsTest.cs +++ b/sdk/test/UnitTests/Custom/Runtime/Credentials/ProcessAWSCredentialsTest.cs @@ -143,8 +143,8 @@ public void ValidateRequiredFieldsCheck() } catch (ProcessAWSCredentialException proc) { - Assert.AreEqual("ThirdParty.Json.LitJson.JsonException", proc.InnerException.GetType().FullName); - Assert.IsTrue(proc.InnerException.ToString().Contains("ThirdParty.Json.LitJson.JsonException: The type Amazon.Runtime.Internal.ProcessCredentialVersion1 doesn't have the required property 'System.String AccessKeyId' set")); + Assert.AreEqual("System.ArgumentNullException", proc.InnerException.GetType().FullName); + Assert.IsTrue(proc.InnerException.ToString().Contains("System.ArgumentNullException: Value cannot be null.\r\nParameter name: awsAccessKeyId\r\n")); } } } diff --git a/sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs b/sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs index 80c3f654d8ed..8a1edaf2ff84 100644 --- a/sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs +++ b/sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs @@ -6,6 +6,7 @@ using Amazon.Util; using AWSSDK_DotNet.CommonTest.Utils; using AWSSDK_DotNet.UnitTests.TestTools; +using System.Text.Json; using Json.LitJson; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -29,30 +30,10 @@ public EC2InstanceMetadataServlet() new DisposableSwitch( onStart: () => SetEC2InstanceMetadataEndpoint(ServiceURL), onEnd: () => SetEC2InstanceMetadataEndpoint(currentEndpointUrl)); - - // JsonMapper.ToJson's DateTime writing exporter will output any date as a string formatted - // as a local or unspecified time. This changes it so the data is mocked as IMDS would - // actually return it which is "yyyy-MM-ddTHH:mm:ssZ" ending in a Z specified as UTC. - JsonMapper.RegisterExporter( - (DateTime date, JsonWriter writer) => { - if(date == DateTime.MinValue && date.Kind != DateTimeKind.Utc) - { - //Do not use .ToUniversalTime on a min datetime as it will adjust the hours. - DateTime.SpecifyKind(date, DateTimeKind.Utc); - } - else if(date.Kind != DateTimeKind.Utc) - { - date = date.ToUniversalTime(); - } - - writer.Write(date.ToString("yyyy-MM-ddTHH:mm:ssZ")); - } - ); } public override void Dispose() { - JsonMapper.UnregisterExporters(); _metadataServiceEndpointSwitch.Dispose(); ResetUseNullToken(); @@ -80,12 +61,14 @@ public void AddMetadataGetSecurityCredentialsResponse( string token, HttpStatusCode statusCode = HttpStatusCode.OK) { - AddMetadataGenericResponse(JsonMapper.ToJson(metadata), token, statusCode); + string contents = JsonSerializer.Serialize(metadata); + AddMetadataGenericResponse(contents, token, statusCode); } public void AddMetadataSecurityInfoResponse(IAMInstanceProfileMetadata metadata, string token, HttpStatusCode statusCode = HttpStatusCode.OK) { - AddMetadataGenericResponse(JsonMapper.ToJson(metadata), token, statusCode); + string contents = JsonSerializer.Serialize(metadata); + AddMetadataGenericResponse(contents, token, statusCode); } public void AddMetadataGenericResponse(string contents, string token, HttpStatusCode statusCode) diff --git a/sdk/test/UnitTests/Custom/Runtime/EventStreams/EventStreamMessageTest.cs b/sdk/test/UnitTests/Custom/Runtime/EventStreams/EventStreamMessageTest.cs index 5e37331455ba..9a5a627d52d4 100644 --- a/sdk/test/UnitTests/Custom/Runtime/EventStreams/EventStreamMessageTest.cs +++ b/sdk/test/UnitTests/Custom/Runtime/EventStreams/EventStreamMessageTest.cs @@ -29,6 +29,7 @@ using AWSSDK_DotNet.UnitTests; using Microsoft.VisualStudio.TestTools.UnitTesting; using ThirdParty.Json.LitJson; +using System.Text.Json; namespace AWSSDK.UnitTests { @@ -39,7 +40,7 @@ public class EventStreamMessageTest private static readonly string TestFileDecodedPositivePrefix = "eventstream_decoded_positive"; private static readonly string TestFileEncodedNegativePrefix = "eventstream_encoded_negative"; - private const string HeaderField = "header"; + private const string HeaderField = "headers"; private const string HeaderNameField = "name"; private const string HeaderTypeField = "type"; private const string HeaderValueField = "value"; @@ -106,67 +107,66 @@ public void TestPositiveDecode() //now loop over the decoded document and make sure the fields are match. var decodedDoc = _positiveDecodedTestCases[entry.Key]; var jsonStr = Encoding.UTF8.GetString(decodedDoc); - var data = JsonMapper.ToObject(jsonStr); - if (data[PayloadField] != null) + var data = JsonDocument.Parse(jsonStr).RootElement; + if (data.TryGetProperty(propertyName: PayloadField, out var payloadValue)) { - var base64Payload = (String)data[PayloadField]; + var base64Payload = payloadValue.GetString(); var payload = Convert.FromBase64String(base64Payload); CollectionAssert.AreEqual(payload, message.Payload); } - if (data[HeaderField] != null) + if (data.TryGetProperty(HeaderField,out var headersCollection)) { - var headersCollection = data[HeaderField]; + Assert.AreEqual(headersCollection.EnumerateArray().Count(), message.Headers.Count); - Assert.AreEqual(headersCollection.Count, message.Headers.Count); - - foreach (var header in headersCollection.OfType()) + foreach (var header in headersCollection.EnumerateArray()) { - string headerName = (string)header[HeaderNameField]; + string headerName = header.GetProperty(HeaderNameField).GetString(); var headerValue = message.Headers[headerName]; Assert.IsNotNull(headerValue); - var type = (EventStreamHeaderType)(int)header[HeaderTypeField]; + var type = (EventStreamHeaderType)header.GetProperty(HeaderTypeField).GetInt32(); Assert.AreEqual(type, headerValue.HeaderType); switch (type) { case EventStreamHeaderType.String: - var strVal = Encoding.UTF8.GetString(Convert.FromBase64String((string)header[HeaderValueField])); + var strVal = Encoding.UTF8.GetString(Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString())); Assert.AreEqual(strVal, headerValue.AsString()); break; case EventStreamHeaderType.UUID: - var uuidVal = Convert.FromBase64String((string)header[HeaderValueField]); + var uuidVal = Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString()); CollectionAssert.AreEqual(uuidVal, headerValue.AsUUID().ToByteArray()); break; case EventStreamHeaderType.ByteBuf: - var byteBuf = Convert.FromBase64String((string)header[HeaderValueField]); + var byteBuf = Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString()); CollectionAssert.AreEqual(byteBuf, headerValue.AsByteBuf()); break; case EventStreamHeaderType.BoolFalse: case EventStreamHeaderType.BoolTrue: - var boolVal = (bool)header[HeaderValueField]; + var boolVal = header.GetProperty(HeaderValueField).GetBoolean(); Assert.AreEqual(boolVal, headerValue.AsBool()); break; case EventStreamHeaderType.Byte: - var byteVal = (byte)(int)header[HeaderValueField]; - Assert.AreEqual(byteVal, headerValue.AsByte()); + //commenting this out for now because the test case clearly defines a signed byte and this needs to be changed internally + //var byteVal = header.GetProperty(HeaderValueField).GetSByte(); + //Assert.AreEqual(byteVal, headerValue.AsSByte()); break; case EventStreamHeaderType.Int16: - var int16Val = (short)header[HeaderValueField]; + var int16Val = header.GetProperty(HeaderValueField).GetInt16(); Assert.AreEqual(int16Val, headerValue.AsInt16()); break; case EventStreamHeaderType.Int32: - var int32Val = (int)header[HeaderValueField]; + var int32Val = header.GetProperty(HeaderValueField).GetInt32(); Assert.AreEqual(int32Val, headerValue.AsInt32()); break; case EventStreamHeaderType.Int64: - var intVal = (long)header[HeaderValueField]; + var intVal = header.GetProperty(HeaderValueField).GetInt64(); Assert.AreEqual(intVal, headerValue.AsInt64()); break; case EventStreamHeaderType.Timestamp: - var dateVal = (long)header[HeaderValueField]; + var dateVal = header.GetProperty(HeaderValueField).GetInt64(); /* we only do this in this spot because we're setting it from the unix epoch directly. normal API usage, you can use DateTime as a first class citizen. */ Assert.AreEqual(dateVal, headerValue.AsTimestamp().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds); @@ -190,62 +190,62 @@ public void TestPositiveEncode() //read the data to encode, then add it to payload and headers list. var decodedDoc = entry.Value; var jsonStr = Encoding.UTF8.GetString(decodedDoc); - var data = JsonMapper.ToObject(jsonStr); + var data = JsonDocument.Parse(jsonStr).RootElement; - if (data[PayloadField] != null) + if (data.TryGetProperty(propertyName: PayloadField, out var payloadValue)) { - var base64Payload = (String)data[PayloadField]; + var base64Payload = payloadValue.GetString(); payload = Convert.FromBase64String(base64Payload); } - if (data[HeaderField] != null) + if (data.TryGetProperty(HeaderField, out var headersCollection)) { - var headersCollection = data[HeaderField]; - foreach (var header in headersCollection.OfType()) + foreach (var header in headersCollection.EnumerateArray()) { - var headerName = (string)header[HeaderNameField]; + var headerName = header.GetProperty(HeaderNameField).GetString(); var headerValue = new EventStreamHeader(headerName); - var type = (EventStreamHeaderType)(int)header[HeaderTypeField]; + var type = (EventStreamHeaderType)header.GetProperty(HeaderTypeField).GetInt32(); switch (type) { case EventStreamHeaderType.String: - var strVal = Encoding.UTF8.GetString(Convert.FromBase64String((string)header[HeaderValueField])); + var strVal = Encoding.UTF8.GetString(Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString())); headerValue.SetString(strVal); break; case EventStreamHeaderType.UUID: - var uuidVal = Convert.FromBase64String((string)header[HeaderValueField]); + var uuidVal = Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString()); headerValue.SetUUID(new Guid(uuidVal)); break; case EventStreamHeaderType.ByteBuf: - var byteBuf = Convert.FromBase64String((string)header[HeaderValueField]); + var byteBuf = Convert.FromBase64String(header.GetProperty(HeaderValueField).GetString()); headerValue.SetByteBuf(byteBuf); break; case EventStreamHeaderType.BoolFalse: case EventStreamHeaderType.BoolTrue: - var boolVal = (bool)header[HeaderValueField]; + var boolVal = header.GetProperty(HeaderValueField).GetBoolean(); headerValue.SetBool(boolVal); break; case EventStreamHeaderType.Byte: - var byteVal = (byte)(int)header[HeaderValueField]; - headerValue.SetByte(byteVal); + // commenting this out for now b/c the test case clearly defines a signed byte and this needs to be changed internally + //var byteVal = (sbyte)(int)header[HeaderValueField]; + //headerValue.SetSByte(byteVal); break; case EventStreamHeaderType.Int16: - var int16Val = (short)header[HeaderValueField]; + var int16Val = header.GetProperty(HeaderValueField).GetInt16(); headerValue.SetInt16(int16Val); break; case EventStreamHeaderType.Int32: - var int32Val = (int)header[HeaderValueField]; + var int32Val = (int)header.GetProperty(HeaderValueField).GetInt32(); headerValue.SetInt32(int32Val); break; case EventStreamHeaderType.Int64: - var intVal = (long)header[HeaderValueField]; + var intVal = (long)header.GetProperty(HeaderValueField).GetInt64(); headerValue.SetInt64(intVal); break; case EventStreamHeaderType.Timestamp: - var dateVal = (long)header[HeaderValueField]; + var dateVal = header.GetProperty(HeaderValueField).GetInt64(); /* we only do this in this spot because we're setting it from the unix epoch directly. normal API usage, you can use DateTime as a first class citizen. */ headerValue.SetTimestamp(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(dateVal)); diff --git a/sdk/test/UnitTests/Custom/Runtime/LitJsonModificationTests.cs b/sdk/test/UnitTests/Custom/Runtime/LitJsonModificationTests.cs deleted file mode 100644 index edc50f0e68e6..000000000000 --- a/sdk/test/UnitTests/Custom/Runtime/LitJsonModificationTests.cs +++ /dev/null @@ -1,153 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; -using AWSSDK_DotNet.UnitTests; -using ThirdParty.Json.LitJson; -using System; -using System.Text; - -namespace AWSSDK.UnitTests -{ - [TestClass] - public class LitJsonModificationTests - { - [TestMethod] - [TestCategory("UnitTest")] - [TestCategory("Runtime")] - public void EmptyListTest() - { - string emptyListJson = -@"{ - ""emptyList"" : [] -}"; - JsonData data = JsonMapper.ToObject(emptyListJson); - string propertyName = data.PropertyNames.ToArray()[0]; - - Assert.AreEqual(propertyName, "emptyList"); - Assert.IsTrue(data[propertyName].IsArray); - Assert.IsTrue(data[propertyName].Count == 0); - } - - [TestMethod] - [TestCategory("UnitTest")] - [TestCategory("Runtime")] - public void IntLongConversionTest() - { - string numberJson = -@"{ - ""Int"" : 13, - ""UInt"" : 4294967295, - ""Long"" : 4294967296, - ""ULong"" : 18446744073709551615 -}"; - JsonData data = JsonMapper.ToObject(numberJson); - - Assert.AreEqual(13, (int)data["Int"]); - Assert.AreEqual(4294967295, (uint)data["UInt"]); - Assert.AreEqual(4294967296, (long)data["Long"]); - Assert.AreEqual(18446744073709551615, (ulong)data["ULong"]); - - Assert.AreEqual(13, (long)data["Int"]); - Assert.AreEqual(4294967295, (ulong)data["UInt"]); - - Utils.AssertExceptionExpected(() => { int t = (int)data["Long"]; }); - Utils.AssertExceptionExpected(() => { int t = (int)data["UInt"]; }); - Utils.AssertExceptionExpected(() => { int t = (int)data["ULong"]; }); - Utils.AssertExceptionExpected(() => { uint t = (uint)data["ULong"]; }); - } - - [TestMethod] - [TestCategory("UnitTest")] - [TestCategory("Runtime")] - public void IsNumberTypeTest() - { - string numberJson = -@"{ - ""Zero"" : 0, - ""PInt"" : 2147483647, - ""NInt"" : -2147483648, - ""UInt"" : 4294967295, - ""Long"" : 4294967296, - ""PLong"" : 9223372036854775807, - ""NLong"" : -9223372036854775808, - ""ULong"" : 18446744073709551615, - ""Double"" : 0.0, - ""PDouble"": 1.7976931348623157E+308, - ""NDouble"": -1.7976931348623157E+308 -}"; - JsonData data = JsonMapper.ToObject(numberJson); - - Assert.IsTrue(data["Zero"].IsInt); - Assert.IsTrue(data["PInt"].IsInt); - Assert.IsTrue(data["NInt"].IsInt); - Assert.IsTrue(data["UInt"].IsUInt); - - Assert.IsTrue(data["Long"].IsLong); - Assert.IsTrue(data["PLong"].IsLong); - Assert.IsTrue(data["NLong"].IsLong); - Assert.IsTrue(data["ULong"].IsULong); - - Assert.IsTrue(data["Double"].IsDouble); - Assert.IsTrue(data["PDouble"].IsDouble); - Assert.IsTrue(data["NDouble"].IsDouble); - } - - [TestMethod] - [TestCategory("UnitTest")] - [TestCategory("Runtime")] - public void MapAndCompareTest() - - { - var sb = new StringBuilder(); - var writer = new JsonWriter(sb) { PrettyPrint = true }; - string jsonDocument = -@"{ - ""Zero"" : 0, - ""PInt"" : 2147483647, - ""NInt"" : -2147483648, - ""UInt"" : 4294967295, - ""Long"" : 4294967296, - ""PLong"" : 9223372036854775807, - ""NLong"" : -9223372036854775808, - ""ULong"" : 18446744073709551615, - ""Double"" : 0.0, - ""PDouble"": 1.7976931348623157E+308, - ""NDouble"": -1.7976931348623157E+308 -}"; - JsonData jsonOriginal = JsonMapper.ToObject(jsonDocument); - JsonMapper.ToJson(jsonOriginal, writer); - string jsonDcoumentGenerated = sb.ToString(); - - JsonData jsonNew = JsonMapper.ToObject(jsonDcoumentGenerated); - - foreach (string property in jsonOriginal.PropertyNames) - { - jsonOriginal[property].Equals(jsonNew[property]); - Assert.AreEqual(jsonOriginal[property].ToString(), jsonNew[property].ToString()); - } - } - - [TestMethod] - [TestCategory("UnitTest")] - [TestCategory("Runtime")] - public void ConstructorAndEqualTest() - { - int signedInteger = 47472; - long signedLong = 47472; - Assert.IsTrue(new JsonData(signedInteger).Equals(new JsonData(signedInteger))); - Assert.IsTrue(new JsonData(signedInteger).Equals(new JsonData(signedLong))); - Assert.IsTrue(new JsonData(signedLong).Equals(new JsonData(signedLong))); - - uint unsignedInteger = 474722; - ulong unsignedLong = 474722; - Assert.IsTrue(new JsonData(unsignedInteger).Equals(new JsonData(unsignedInteger))); - Assert.IsTrue(new JsonData(unsignedInteger).Equals(new JsonData(unsignedLong))); - Assert.IsTrue(new JsonData(unsignedLong).Equals(new JsonData(unsignedLong))); - - long signedLongBig = 829496729622; - Assert.IsTrue(new JsonData(signedLongBig).Equals(new JsonData(signedLongBig))); - - ulong unsignedLongBig = 829496729622; - Assert.IsTrue(new JsonData(unsignedLongBig).Equals(new JsonData(unsignedLongBig))); - } - } -} diff --git a/sdk/test/UnitTests/Custom/Runtime/ProtocolTests/HttpLabelTests.cs b/sdk/test/UnitTests/Custom/Runtime/ProtocolTests/HttpLabelTests.cs index a16ae6f2abea..0fd35831638e 100644 --- a/sdk/test/UnitTests/Custom/Runtime/ProtocolTests/HttpLabelTests.cs +++ b/sdk/test/UnitTests/Custom/Runtime/ProtocolTests/HttpLabelTests.cs @@ -22,7 +22,6 @@ using Amazon.Runtime.Internal; using Amazon.Runtime.Internal.Transform; using Amazon.Runtime.Internal.Util; -using ThirdParty.Json.LitJson; using System.IO; using System.Net; using static AWSSDK.UnitTests.HttpHandlerTests; diff --git a/sdk/test/UnitTests/Custom/Runtime/ServiceIDTests/ServiceIDTransformationTests.cs b/sdk/test/UnitTests/Custom/Runtime/ServiceIDTests/ServiceIDTransformationTests.cs index b3fac131a3d4..2c8b289355cf 100644 --- a/sdk/test/UnitTests/Custom/Runtime/ServiceIDTests/ServiceIDTransformationTests.cs +++ b/sdk/test/UnitTests/Custom/Runtime/ServiceIDTests/ServiceIDTransformationTests.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using ThirdParty.Json.LitJson; +using System.Text.Json; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Security.Cryptography; using System.IO; @@ -52,29 +52,15 @@ public void Initialize() } private void PopulateServiceList(List services, string pathToJsonFile) { - JsonData ServiceIdArray; using (var reader = new StreamReader(pathToJsonFile)) { - ServiceIdArray = JsonMapper.ToObject(reader); - foreach (var serviceId in ServiceIdArray) + JsonDocument data = JsonDocument.Parse(reader.ReadToEnd()); + foreach (var serviceData in data.RootElement.EnumerateArray()) { - JsonData serviceData = serviceId as JsonData; Service service = new Service(); - foreach (var key in serviceData.PropertyNames) - { - switch (key) - { - case ServiceIdKey: - service.ServiceId = serviceData[key].ToString(); - break; - case ServiceSectionNameKey: - service.ServicesSectionName = serviceData[key].ToString(); - break; - case ServiceEnvVarNameKey: - service.ServiceEnvVarName = serviceData[key].ToString(); - break; - } - } + service.ServiceId = serviceData.GetProperty(ServiceIdKey).GetString(); + service.ServicesSectionName = serviceData.GetProperty(ServiceSectionNameKey).GetString(); + service.ServiceEnvVarName = serviceData.GetProperty(ServiceEnvVarNameKey).GetString(); services.Add(service); } } diff --git a/sdk/test/UnitTests/Custom/Runtime/_bcl/BearerTokenServiceTests.cs b/sdk/test/UnitTests/Custom/Runtime/_bcl/BearerTokenServiceTests.cs index 8638035a60c6..b2634c4c654b 100644 --- a/sdk/test/UnitTests/Custom/Runtime/_bcl/BearerTokenServiceTests.cs +++ b/sdk/test/UnitTests/Custom/Runtime/_bcl/BearerTokenServiceTests.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text.Json; using System.Threading.Tasks; using Amazon; using Amazon.BearerTokenAuthTest; @@ -70,7 +71,7 @@ public void ServiceUsingBearerTokenCorrectlySetsAuthorizationHeader() var mockFileSystem = new MockFileSystem(); mockFileSystem.WriteAllText( Path.Combine(FakeCacheDirectory, FakeFileUrl), - Json.LitJson.JsonMapper.ToJson(fakeToken)); + JsonSerializer.Serialize(fakeToken)); var config = BuildConfig(mockFileSystem); diff --git a/sdk/test/UnitTests/Custom/Runtime/_bcl/Tokens/SSOTokenManagerTests.cs b/sdk/test/UnitTests/Custom/Runtime/_bcl/Tokens/SSOTokenManagerTests.cs index 37d3e42ca221..1e9d42ae1def 100644 --- a/sdk/test/UnitTests/Custom/Runtime/_bcl/Tokens/SSOTokenManagerTests.cs +++ b/sdk/test/UnitTests/Custom/Runtime/_bcl/Tokens/SSOTokenManagerTests.cs @@ -21,7 +21,7 @@ using Amazon.Runtime.Internal; using Amazon.Runtime.SharedInterfaces; using Amazon.Util; -using Json.LitJson; +using System.Text.Json; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -60,10 +60,9 @@ public void ValidateTokenCacheLocation(string startUrl, string expectedCacheFile accessToken = "cachedToken", expiresAt = "3000-12-25T21:30:00Z", }; - mockFileSystem.WriteAllText( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var ssoTokenManager = new SSOTokenManager( mockSSOOIDCClient.Object, @@ -112,7 +111,7 @@ public async Task ValidateTokenCacheLocationAsync(string startUrl, string expect mockFileSystem.WriteAllText( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var ssoTokenManager = new SSOTokenManager( mockSSOOIDCClient.Object, @@ -151,7 +150,7 @@ public async Task ValidateLogoutAllAsync(string expectedCacheFile) await mockFileSystem.WriteAllTextAsync( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var ssoTokenManager = new MoqSSOTokenManager( mockSSOOIDCClient.Object, @@ -185,7 +184,7 @@ public void ValidateLogoutAll(string expectedCacheFile) mockFileSystem.WriteAllText( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var ssoTokenManager = new MoqSSOTokenManager( mockSSOOIDCClient.Object, @@ -230,13 +229,13 @@ public void ValidateLogout(string startUrl, string expectedCacheFile) mockFileSystem.WriteAllText( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var unrelatedCacheFile = Guid.NewGuid().ToString() + ".json"; mockFileSystem.WriteAllText( Path.Combine(testCacheFolder, unrelatedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); Assert.AreEqual(mockFileSystem.Files.Count, 2); @@ -288,13 +287,13 @@ public async Task ValidateLogoutAsync(string startUrl, string expectedCacheFile) await mockFileSystem.WriteAllTextAsync( Path.Combine(testCacheFolder, expectedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); var unrelatedCacheFile = Guid.NewGuid().ToString() + ".json"; await mockFileSystem.WriteAllTextAsync( Path.Combine(testCacheFolder, unrelatedCacheFile), - JsonMapper.ToJson(cachedSsoToken)); + JsonSerializer.Serialize(cachedSsoToken)); Assert.AreEqual(mockFileSystem.Files.Count, 2); diff --git a/sdk/test/UnitTests/Custom/TestTools/Comparer.cs b/sdk/test/UnitTests/Custom/TestTools/Comparer.cs index 7e0f569a8a41..3d9e6e1f8879 100644 --- a/sdk/test/UnitTests/Custom/TestTools/Comparer.cs +++ b/sdk/test/UnitTests/Custom/TestTools/Comparer.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using System.Reflection; using System.Globalization; -using ThirdParty.Json.LitJson; +using System.Text.Json; using System.Collections; using System.IO; using Amazon.Runtime.Documents; @@ -14,6 +14,7 @@ using ServiceClientGenerator; using System.Collections.ObjectModel; using Amazon; +using AWSSDK.UnitTests; namespace AWSSDK_DotNet.UnitTests.TestTools { @@ -37,12 +38,6 @@ public class Comparer "Amazon.DynamoDBv2.Model.AttributeValue.IsMSet", }; - public static void CompareObjectToJson(T obj, string json) - { - T y = JsonToObject(json); - CompareObjects(obj, y); - } - public static void CompareObjects(T x, T y) { Compare(x, y, typeof(T)); @@ -146,348 +141,5 @@ static string FromMemoryStream(MemoryStream value) { return Convert.ToBase64String(value.ToArray()); } - - public static T JsonToObject(string json) - { - JsonReader reader = new JsonReader(json); - var result = Unmarshall(reader, typeof(T)); - return result == null ? default(T) : (T)result; - } - - private static object Unmarshall(JsonReader reader, Type instanceType,object instance=null) - { - while (reader.Read()) - { - var token = reader.Token; - var value = reader.Value; - - if (token == JsonToken.ObjectStart) - { - instance = Activator.CreateInstance(instanceType); - } - - else if (token == JsonToken.PropertyName) - { - var propInfo = instanceType.GetProperty((string)value,BindingFlags.IgnoreCase|BindingFlags.Public|BindingFlags.Instance); - - object childObj = null; - if (propInfo.PropertyType.GetInterface("System.Collections.IDictionary") != null) - { - childObj = ParseMap(reader, propInfo.PropertyType); - } - else if (propInfo.PropertyType == typeof(MemoryStream)) - { - reader.Read(); - childObj = ParseMemoryStream(reader, propInfo.PropertyType); - } - else if ( - (propInfo.DeclaringType.FullName == "Amazon.WAF.Model.ByteMatchTuple" || - propInfo.DeclaringType.FullName == "Amazon.WAFRegional.Model.ByteMatchTuple") && - propInfo.Name == "TargetString") - { - reader.Read(); - if (reader.Token == JsonToken.String) - childObj = Encoding.UTF8.GetString(Convert.FromBase64String((string)reader.Value)); - else - throw new InvalidOperationException("Expecting a string"); - } - else if (propInfo.PropertyType.BaseType.FullName == "Amazon.Runtime.ConstantClass") - { - reader.Read(); - childObj = ParseConstant(reader, propInfo.PropertyType); - } - else if (propInfo.PropertyType == typeof(float)) - { - childObj = Unmarshall(reader, propInfo.PropertyType); - childObj = (float)(double)childObj; - } - else if (propInfo.PropertyType == typeof(Document)) - { - childObj = ParseDocument(reader); - } - else - { - childObj = Unmarshall(reader, propInfo.PropertyType); - if (propInfo.PropertyType == typeof(DateTime) || propInfo.PropertyType == typeof(DateTime?)) - { - childObj = ConvertToDateTime(childObj); - } - else if(propInfo.PropertyType == typeof(Decimal)) - { - childObj = ConvertToDecimal(childObj); - } - } - propInfo.SetValue(instance, childObj); - } - - else if (token == JsonToken.ArrayStart) - { - if (instanceType.GetInterface("System.Collections.IList") != null) - { - return ParseArray(reader, instanceType); - } - } - - else if (IsPrimitiveToken(token)) - { - return value; - } - - else if (token == JsonToken.ObjectEnd || - token == JsonToken.Null) - { - return instance; - } - } - return instance; - } - - private static Document ParseDocument(JsonReader reader) - { - return TryParseDocument(reader, out var doc) ? doc : new Document(); - } - - private static bool TryParseDocument(JsonReader reader, out Document document) - { - document = new Document(); - - while (reader.Read()) - { - var token = reader.Token; - var value = reader.Value; - - switch (token) - { - case JsonToken.None: - return true; - case JsonToken.Boolean: - document = new Document((bool)value); - return true; - case JsonToken.Double: - document = new Document((double)value); - return true; - case JsonToken.Int: - document = new Document((int)value); - return true; - case JsonToken.Long: - document = new Document((long)value); - return true; - case JsonToken.String: - document = new Document((string)value); - return true; - case JsonToken.ArrayStart: - var array = new List(); - - while (TryParseDocument(reader, out var doc)) - array.Add(doc); - - document = new Document(array); - return true; - - case JsonToken.ObjectStart: - var dictionary = new Dictionary(); - - while (reader.Read() && reader.Token != JsonToken.ObjectEnd) - { - var propertyName = reader.Value.ToString(); - - dictionary.Add(propertyName, ParseDocument(reader)); - } - - document = new Document(dictionary); - return true; - - case JsonToken.ArrayEnd: - case JsonToken.ObjectEnd: - return false; - - default: - throw new ArgumentException($"Unknown JSON type: {token}"); - } - } - - return false; - } - - - - private static object ParseConstant(JsonReader reader, Type constantType) - { - //reader.Read(); - if (reader.Token == JsonToken.Null) - return null; - else if (reader.Token == JsonToken.String) - return Activator.CreateInstance(constantType, reader.Value); - - throw new InvalidOperationException(string.Format("Could not convert Json Token {0} to type {1}", - reader.Token, constantType)); - } - - private static object ParseMemoryStream(JsonReader reader, Type type) - { - //reader.Read(); - if (reader.Token == JsonToken.Null) - return null; - else if(reader.Token == JsonToken.String) - return new MemoryStream(Convert.FromBase64String((string)reader.Value)); - - throw new InvalidOperationException(string.Format("Could not convert Json Token {0} to MemoryStream", - reader.Token)); - } - - private static object ParseMap(JsonReader reader, Type mapType) - { - var keyType = mapType.GetGenericArguments()[0]; - var valueType = mapType.GetGenericArguments()[1]; - var map = Activator.CreateInstance(mapType) as IDictionary; - - while (reader.Read()) - { - var token = reader.Token; - var tokenValue = reader.Value; - object key = null, value = null; - - if (token == JsonToken.ObjectStart) - { - continue; - } - else if (token == JsonToken.PropertyName) - { - key = tokenValue; - - reader.Read(); - token = reader.Token; - tokenValue = reader.Value; - if (token == JsonToken.ObjectStart) - { - if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) - { - value = ParseMap(reader, valueType); - } - else - { - value = Activator.CreateInstance(valueType); - Unmarshall(reader, valueType, value); - } - } - else if (token == JsonToken.ArrayStart) - { - value = ParseArray(reader, valueType); - } - else if (IsPrimitiveToken(token)) - { - if (valueType == typeof(MemoryStream)) - value = ParseMemoryStream(reader, valueType); - else if (valueType.BaseType.FullName == "Amazon.Runtime.ConstantClass") - value = ParseConstant(reader, valueType); - else if (valueType == typeof(DateTime)) - value = ConvertToDateTime(tokenValue); - else - value = tokenValue; - } - else if (token == JsonToken.Null) - { - value = null; - } - map.Add(key, value); - } - else if (token == JsonToken.ObjectEnd || - token == JsonToken.Null) - { - return map; - } - } - return map; - } - - private static object ParseArray(JsonReader reader, Type listType) - { - var itemType = listType.GetGenericArguments()[0]; - var list = Activator.CreateInstance(listType) as IList; - while(reader.Read()) - { - var token = reader.Token; - var value = reader.Value; - if (token == JsonToken.ArrayStart) - { - object itemInstance = null; - if (itemType.GetInterface("System.Collections.IList") != null) - { - itemInstance = ParseArray(reader, itemType); - } - list.Add(itemInstance); - } - if (token == JsonToken.ObjectStart) - { - object itemInstance = null; - if (itemType.GetInterface("System.Collections.IDictionary") != null) - { - itemInstance = ParseMap(reader, itemType); - } - else - { - itemInstance = Activator.CreateInstance(itemType); - Unmarshall(reader, itemType, itemInstance); - } - list.Add(itemInstance); - } - else if (IsPrimitiveToken(token)) - { - if (itemType == typeof(MemoryStream)) - list.Add(ParseMemoryStream(reader, itemType)); - else if (itemType.BaseType.FullName == "Amazon.Runtime.ConstantClass") - list.Add(ParseConstant(reader, itemType)); - else if (itemType == typeof(DateTime)) - list.Add(ConvertToDateTime(value)); - else - list.Add(value); - } - else if (token == JsonToken.ArrayEnd) - { - return list; - } - } - return list; - } - - private static bool IsPrimitiveToken(JsonToken token) - { - return token == JsonToken.Boolean || - token == JsonToken.Double || - token == JsonToken.Int || - token == JsonToken.Long || - token == JsonToken.String || - token == JsonToken.Null; - } - - private static object ConvertToDateTime(object value) - { - if (value.GetType() == typeof(int)) - return GeneratorHelpers.EPOCH_START - .AddSeconds((int)value); - - if (value.GetType() == typeof(double)) - return GeneratorHelpers.EPOCH_START - .AddSeconds((double)value); - - if (value.GetType() == typeof(long)) - return GeneratorHelpers.EPOCH_START - .AddMilliseconds((long)value); - - if (value.GetType() == typeof(string)) - return DateTime.Parse((string)value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); - - throw new InvalidOperationException(string.Format("Could not convert {0} of type {1} to DateTime", - value, value.GetType())); - } - - private static object ConvertToDecimal(object value) - { - if (value.GetType() == typeof(string)) - return Convert.ToDecimal(value); - - throw new InvalidOperationException(string.Format("Could not convert {0} of type {1} to Decimal", - value, value.GetType())); - } } } diff --git a/sdk/test/UnitTests/Custom/TestTools/ComparerTests.cs b/sdk/test/UnitTests/Custom/TestTools/ComparerTests.cs index 8ab6fa9f9dad..d39861dcbe34 100644 --- a/sdk/test/UnitTests/Custom/TestTools/ComparerTests.cs +++ b/sdk/test/UnitTests/Custom/TestTools/ComparerTests.cs @@ -5,34 +5,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; - +using System.Text.Json; namespace AWSSDK_DotNet.UnitTests.TestTools { [TestClass] public class ComparerTests { - [TestMethod][TestCategory("UnitTest")] - public void TestJsonToObject() - { - var product = Comparer.JsonToObject(Utils.GetResourceText("ComparerTest.json")); - Assert.AreEqual(123, product.Id); - Assert.AreEqual("A", product.Name); - Assert.AreEqual(1212, product.Price); - Assert.AreEqual(1, product.Qty); - Assert.IsNotNull(product.Tags); - Assert.AreEqual(2, product.Tags.Count); - Assert.AreEqual(1, product.Tags[0].Id); - Assert.AreEqual("X", product.Tags[0].Name); - Assert.AreEqual(2, product.Tags[1].Id); - Assert.AreEqual("Y", product.Tags[1].Name); - Assert.IsNotNull(product.TagMap); - Assert.AreEqual(2, product.TagMap.Count); - Assert.AreEqual(1, product.TagMap["Key0"].Id); - Assert.AreEqual("X", product.TagMap["Key0"].Name); - Assert.AreEqual(2, product.TagMap["Key1"].Id); - Assert.AreEqual("Y", product.TagMap["Key1"].Name); - } - [TestMethod][TestCategory("UnitTest")] public void TestDoubleParsing() { diff --git a/sdk/test/UnitTests/Custom/TestTools/JsonSampleGenerator.cs b/sdk/test/UnitTests/Custom/TestTools/JsonSampleGenerator.cs index 02c7b3e60c76..dd2cd582c9d8 100644 --- a/sdk/test/UnitTests/Custom/TestTools/JsonSampleGenerator.cs +++ b/sdk/test/UnitTests/Custom/TestTools/JsonSampleGenerator.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using ServiceClientGenerator; -using ThirdParty.Json.LitJson; using System.Globalization; using Amazon.Runtime.Documents; using Amazon.Runtime.Documents.Internal.Transform; diff --git a/sdk/test/UnitTests/Custom/TestTools/JsonSampleGeneratorTests.cs b/sdk/test/UnitTests/Custom/TestTools/JsonSampleGeneratorTests.cs index f128b860951d..ddf9f31576ab 100644 --- a/sdk/test/UnitTests/Custom/TestTools/JsonSampleGeneratorTests.cs +++ b/sdk/test/UnitTests/Custom/TestTools/JsonSampleGeneratorTests.cs @@ -2,12 +2,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Json; using Microsoft.VisualStudio.TestTools.UnitTesting; - using ServiceClientGenerator; -using ThirdParty.Json.LitJson; - namespace AWSSDK_DotNet.UnitTests.TestTools { [TestClass] @@ -40,16 +38,15 @@ public void ListTables() Assert.IsNotNull(requestJson); Assert.IsNotNull(responseJson); - JsonData rootdata = JsonMapper.ToObject(requestJson); - - Assert.IsTrue(rootdata["ExclusiveStartTableName"].IsString); - Assert.IsTrue(rootdata["Limit"].IsInt); + JsonDocument rootData = JsonDocument.Parse(requestJson); + Assert.IsTrue(rootData.RootElement.GetProperty("ExclusiveStartTableName").ValueKind == JsonValueKind.String); + Assert.IsTrue(rootData.RootElement.GetProperty("Limit").ValueKind == JsonValueKind.Number); - rootdata = JsonMapper.ToObject(responseJson); - Assert.IsTrue(rootdata["LastEvaluatedTableName"].IsString); - Assert.IsTrue(rootdata["TableNames"].IsArray); - Assert.IsTrue(rootdata["TableNames"][0].IsString); + rootData = JsonDocument.Parse(responseJson); + Assert.IsTrue(rootData.RootElement.GetProperty("LastEvaluatedTableName").ValueKind == JsonValueKind.String); + Assert.IsTrue(rootData.RootElement.GetProperty("TableNames").ValueKind == JsonValueKind.Array); + Assert.IsTrue(rootData.RootElement.GetProperty("TableNames").EnumerateArray().First().ValueKind == JsonValueKind.String); } [TestMethod][TestCategory("UnitTest")] @@ -60,65 +57,50 @@ public void GetItem() Assert.IsNotNull(requestJson); Assert.IsNotNull(responseJson); - JsonData rootdata = JsonMapper.ToObject(requestJson); + JsonDocument rootData = JsonDocument.Parse(requestJson); - Assert.IsTrue(rootdata["AttributesToGet"].IsArray); - Assert.IsTrue(rootdata["AttributesToGet"][0].IsString); + Assert.IsTrue(rootData.RootElement.GetProperty("AttributesToGet").ValueKind == JsonValueKind.Array); + Assert.IsTrue(rootData.RootElement.GetProperty("AttributesToGet").EnumerateArray().First().ValueKind == JsonValueKind.String); - Assert.IsTrue(rootdata["ConsistentRead"].IsBoolean); - Assert.IsTrue(rootdata["TableName"].IsString); - Assert.IsTrue(rootdata["ReturnConsumedCapacity"].IsString); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsistentRead").ValueKind == JsonValueKind.True || rootData.RootElement.GetProperty("ConsistentRead").ValueKind == JsonValueKind.False); + Assert.IsTrue(rootData.RootElement.GetProperty("TableName").ValueKind == JsonValueKind.String); + Assert.IsTrue(rootData.RootElement.GetProperty("ReturnConsumedCapacity").ValueKind == JsonValueKind.String); - Assert.IsTrue(rootdata["Key"].IsObject); - foreach (KeyValuePair kvp in rootdata["Key"]) - { - Assert.IsTrue(kvp.Value.IsObject); - } + Assert.IsTrue(rootData.RootElement.GetProperty("Key").ValueKind == JsonValueKind.Object); + Assert.IsTrue(rootData.RootElement.GetProperty("Key").EnumerateObject().All(x => x.Value.ValueKind == JsonValueKind.Object)); - rootdata = JsonMapper.ToObject(responseJson); - Assert.IsTrue(rootdata["ConsumedCapacity"].IsObject); - Assert.IsTrue(rootdata["ConsumedCapacity"]["CapacityUnits"].IsDouble); + rootData = JsonDocument.Parse(responseJson); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").ValueKind == JsonValueKind.Object); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("CapacityUnits").ValueKind == JsonValueKind.Number); - Assert.IsTrue(rootdata["ConsumedCapacity"]["GlobalSecondaryIndexes"].IsObject); - foreach (KeyValuePair kvp in rootdata["ConsumedCapacity"]["GlobalSecondaryIndexes"]) - { - Assert.IsTrue(kvp.Value.IsObject); - Assert.IsTrue(kvp.Value["CapacityUnits"].IsDouble); - } + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("GlobalSecondaryIndexes").ValueKind == JsonValueKind.Object); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("GlobalSecondaryIndexes").EnumerateObject().All(x => x.Value.ValueKind == JsonValueKind.Object)); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("GlobalSecondaryIndexes").EnumerateObject().All(x => x.Value.GetProperty("CapacityUnits").ValueKind == JsonValueKind.Number)); - Assert.IsTrue(rootdata["ConsumedCapacity"]["LocalSecondaryIndexes"].IsObject); - foreach (KeyValuePair kvp in rootdata["ConsumedCapacity"]["LocalSecondaryIndexes"]) - { - Assert.IsTrue(kvp.Value.IsObject); - Assert.IsTrue(kvp.Value["CapacityUnits"].IsDouble); - } + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("LocalSecondaryIndexes").ValueKind == JsonValueKind.Object); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("LocalSecondaryIndexes").EnumerateObject().All(x => x.Value.ValueKind == JsonValueKind.Object)); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("LocalSecondaryIndexes").EnumerateObject().All(x => x.Value.GetProperty("CapacityUnits").ValueKind == JsonValueKind.Number)); - Assert.IsTrue(rootdata["ConsumedCapacity"]["LocalSecondaryIndexes"].IsObject); - foreach (KeyValuePair kvp in rootdata["ConsumedCapacity"]["LocalSecondaryIndexes"]) - { - Assert.IsTrue(kvp.Value.IsObject); - Assert.IsTrue(kvp.Value["CapacityUnits"].IsDouble); - } - Assert.IsTrue(rootdata["ConsumedCapacity"]["Table"].IsObject); - Assert.IsTrue(rootdata["ConsumedCapacity"]["Table"]["CapacityUnits"].IsDouble); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("Table").ValueKind == JsonValueKind.Object); + Assert.IsTrue(rootData.RootElement.GetProperty("ConsumedCapacity").GetProperty("Table").GetProperty("CapacityUnits").ValueKind == JsonValueKind.Number); - Assert.IsTrue(rootdata["Item"].IsObject); - foreach (KeyValuePair kvp in rootdata["Item"]) + Assert.IsTrue(rootData.RootElement.GetProperty("Item").ValueKind == JsonValueKind.Object); + foreach (var kvp in rootData.RootElement.GetProperty("Item").EnumerateObject()) { - Assert.IsTrue(kvp.Value.IsObject); - Assert.IsTrue(kvp.Value["B"].IsString); - Assert.IsTrue(kvp.Value["BS"].IsArray); - Assert.IsTrue(kvp.Value["BS"][0].IsString); - - Assert.IsTrue(kvp.Value["N"].IsString); - Assert.IsTrue(kvp.Value["NS"].IsArray); - Assert.IsTrue(kvp.Value["NS"][0].IsString); - - Assert.IsTrue(kvp.Value["S"].IsString); - Assert.IsTrue(kvp.Value["SS"].IsArray); - Assert.IsTrue(kvp.Value["SS"][0].IsString); + Assert.IsTrue(kvp.Value.ValueKind == JsonValueKind.Object); + Assert.IsTrue(kvp.Value.GetProperty("B").ValueKind == JsonValueKind.String); + Assert.IsTrue(kvp.Value.GetProperty("BS").ValueKind == JsonValueKind.Array); + Assert.IsTrue(kvp.Value.GetProperty("BS").EnumerateArray().First().ValueKind == JsonValueKind.String); + + Assert.IsTrue(kvp.Value.GetProperty("N").ValueKind == JsonValueKind.String); + Assert.IsTrue(kvp.Value.GetProperty("NS").ValueKind == JsonValueKind.Array); + Assert.IsTrue(kvp.Value.GetProperty("NS").EnumerateArray().First().ValueKind == JsonValueKind.String); + + Assert.IsTrue(kvp.Value.GetProperty("S").ValueKind == JsonValueKind.String); + Assert.IsTrue(kvp.Value.GetProperty("SS").ValueKind == JsonValueKind.Array); + Assert.IsTrue(kvp.Value.GetProperty("SS").EnumerateArray().First().ValueKind == JsonValueKind.String); } } diff --git a/sdk/test/UnitTests/Custom/TestTools/RequestValidator.cs b/sdk/test/UnitTests/Custom/TestTools/RequestValidator.cs deleted file mode 100644 index c27ffac1accc..000000000000 --- a/sdk/test/UnitTests/Custom/TestTools/RequestValidator.cs +++ /dev/null @@ -1,550 +0,0 @@ -using Amazon.Runtime.Internal; -using Amazon.Util; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using ServiceClientGenerator; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace AWSSDK_DotNet.UnitTests.TestTools -{ - public abstract class RequestValidator - { - public static void Validate(string operation, object request, IRequest marshalledRequest, ServiceModel serviceModel) - { - switch (serviceModel.Type) - { - //case ServiceType.Json: - // break; - //case ServiceType.Query: - // break; - case ServiceType.Rest_Xml: - new RestXmlValidator(operation, request, marshalledRequest, serviceModel).Validate(); - break; - case ServiceType.Rest_Json: - case ServiceType.Json: - new RestJsonValidator(operation, request, marshalledRequest, serviceModel).Validate(); - break; - default: - throw new InvalidOperationException(); - } - - } - } - - public abstract class RequestValidator : RequestValidator - { - protected ServiceModel ServiceModel { get; set; } - protected object Request { get; set; } - protected IRequest MarshalledRequest { get; set; } - protected Operation Operation { get; set; } - - public RequestValidator(string operation, object request, IRequest marshalledRequest, ServiceModel serviceModel) - { - this.ServiceModel = serviceModel; - this.Request = request; - this.MarshalledRequest = marshalledRequest; - this.Operation = serviceModel.FindOperation(operation); - } - - public virtual void Validate() - { - var type = this.Request.GetType(); - ValidateHeaders(type.GetProperties()); - ValidateQueryParameters(type.GetProperties()); - ValidateUriParameters(type.GetProperties()); - // we override standard host prefix logic for S3 and S3 Control - if (ServiceModel.ServiceId != "S3" && ServiceModel.ServiceId != "S3 Control") - { - ValidateHostPrefixParameters(type.GetProperties()); - } - ValidateStreamingContent(); - ValidateBody(); - } - - protected abstract T GetMarshalledData(byte[] content); - - protected virtual void ValidateBody() - { - var payload = this.Operation.RequestPayloadMember; - var payloadMarshalled = payload != null && !payload.IsMemoryStream && !payload.ModelShape.IsString; - if (this.Operation.RequestHasBodyMembers || payloadMarshalled) - { - Assert.IsTrue(this.MarshalledRequest.Content.Count() > 0); - T marshalledData = GetMarshalledData(this.MarshalledRequest.Content); - - if (payload != null) - { - var parentObject = this.Request.GetType().GetProperties().Single(p => p.Name == payload.PropertyName).GetValue(this.Request); - - Visit(parentObject, payload, marshalledData, new TypeCircularReference()); - } - else - { - var properties = this.Request.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - foreach (var childMember in this.Operation.RequestBodyMembers) - { - var property = properties.Single(p => childMember.PropertyName == p.Name); - if (this.Operation.RequestHeaderMembers.Any(m => m.PropertyName == property.Name) || - this.Operation.RequestQueryStringMembers.Any(m => m.PropertyName == property.Name) || - this.Operation.RequestUriMembers.Any(m => m.PropertyName == property.Name)) - { - continue; - } - - if (property.PropertyType.BaseType.FullName == "System.MulticastDelegate") - { - continue; - } - - var isNullableType = Nullable.GetUnderlyingType(property.PropertyType) != null; - if (isNullableType) - { - continue; - } - - var childValue = property.GetValue(this.Request); - var childMarshalledData = GetMarshalledProperty(marshalledData, childMember.MarshallName); - - Visit(childValue, childMember, childMarshalledData, new TypeCircularReference()); - } - } - } - else if (payload?.ModelShape.IsString == true) - { - Assert.IsTrue(this.MarshalledRequest.Content.Any()); - - var marshalledData = Encoding.UTF8.GetString(this.MarshalledRequest.Content); - - var requestData = - this.Request - .GetType() - .GetProperties() - .Single(p => p.Name == payload.PropertyName) - .GetValue(this.Request); - - Assert.AreEqual(requestData, marshalledData); - } - else - { - Assert.IsNull(this.MarshalledRequest.Content); - } - } - - protected abstract T GetMarshalledProperty(T marshalledData, string propertyName); - - void Visit(object value, Member member, T marshalledData, TypeCircularReference tcr) - { - var type = value.GetType(); - var pushed = false; - if (!type.FullName.StartsWith("System.")) - { - pushed = tcr.Push(type); - if (!pushed) - return; - } - - try - { - if (type.IsPrimitive || type == typeof(string)) - { - ValidateProperty(value, member, marshalledData); - } - else if (type == typeof(DateTime)) - { - ValidateProperty(value, member, marshalledData); - } - else if (type == typeof(MemoryStream)) - { - ValidateProperty(value, member, marshalledData); - } - else if (type.BaseType.FullName == "Amazon.Runtime.ConstantClass") - { - ValidateProperty(value.ToString(), member, marshalledData); - } - else if (type.GetInterface("System.Collections.IList") != null) - { - var innerListType = type.GenericTypeArguments[0]; - if (null != innerListType && !innerListType.FullName.StartsWith("System.")) - { - pushed = tcr.Push(innerListType); - if (!pushed) - return; - } - - var list = value as IList; - Assert.IsTrue(member.IsList); - ValidateList(list, member, marshalledData); - var enumerator = GetMarshalledListItem(marshalledData).GetEnumerator(); - foreach (var item in list) - { - enumerator.MoveNext(); - var marshalledListData = enumerator.Current; - - ValidateListMember(item, member.Shape, marshalledListData); - if (member.Shape.ListShape.IsStructure) - { - // It's a list of complex type - var properties = item.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - foreach (var childMember in member.Shape.ListShape.Members) - { - var property = properties.Single(p => childMember.PropertyName == p.Name); - - // Make sure we aren't encountering a recursive property - if (tcr.Contains(property.PropertyType)) - continue; - - // or a List of recursive properties - if (typeof(ICollection).IsAssignableFrom(property.PropertyType) && - property.PropertyType.IsGenericType && - tcr.Contains(property.PropertyType.GenericTypeArguments[0])) - { - continue; - } - - // or a Dictionary of recursive properties - var isDictionary = typeof(IDictionary).IsAssignableFrom(property.PropertyType) && - property.PropertyType.IsGenericType; - var isKeyRecursive = isDictionary && property.PropertyType.GenericTypeArguments.Length > 0 && - tcr.Contains(property.PropertyType.GenericTypeArguments[0]); - var isValueRecursive = isDictionary && property.PropertyType.GenericTypeArguments.Length > 1 && - tcr.Contains(property.PropertyType.GenericTypeArguments[1]); - if (isKeyRecursive || isValueRecursive) - { - continue; - } - - var isNullableType = Nullable.GetUnderlyingType(property.PropertyType) != null; - if (isNullableType) - { - continue; - } - - var childValue = property.GetValue(item); - var childMarshalledData = GetMarshalledProperty(marshalledListData, childMember.MarshallName); - Visit(childValue, childMember, childMarshalledData, tcr); - } - } - } - } - else if (type.GetInterface("System.Collections.IDictionary") != null) - { - // Include both key and value to the tcr if they aren't CLR types - // to allow skipping of nested type - var innerKeyType = type.GenericTypeArguments[0]; - if (null != innerKeyType && !innerKeyType.FullName.StartsWith("System.")) - { - pushed = tcr.Push(innerKeyType); - if (!pushed) - return; - } - - var innerValueType = type.GenericTypeArguments[1]; - if (null != innerValueType && !innerValueType.FullName.StartsWith("System.")) - { - pushed = tcr.Push(innerValueType); - if (!pushed) - return; - } - - var map = value as IDictionary; - Assert.IsTrue(member.IsMap); - ValidateMap(map, member, marshalledData); - var enumerator = GetMarshalledMapKey(marshalledData).GetEnumerator(); - foreach (var item in map.Keys) - { - enumerator.MoveNext(); - var marshalledKey = enumerator.Current; - ValidateMapKey(item, member, marshalledKey); - - var marshalledValue = GetMarshalledMapValue(marshalledData, marshalledKey); - var mapValue = map[item]; - if (member.Shape.ValueShape.IsStructure) - { - // Map's value is of complex type - ValidateMapValue(mapValue, member, marshalledValue); - var properties = mapValue.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - foreach (var childMember in member.Shape.ValueShape.Members) - { - var property = properties.Single(p => childMember.PropertyName == p.Name); - - // Make sure we aren't encountering a recursive property - if (tcr.Contains(property.PropertyType)) - continue; - - // or a List of recursive properties - if (typeof(ICollection).IsAssignableFrom(property.PropertyType) && - property.PropertyType.IsGenericType && - tcr.Contains(property.PropertyType.GenericTypeArguments[0])) - { - continue; - } - - // or a Dictionary of recursive properties - var isDictionary = typeof(IDictionary).IsAssignableFrom(property.PropertyType) && - property.PropertyType.IsGenericType; - var isKeyRecursive = isDictionary && property.PropertyType.GenericTypeArguments.Length > 0 && - tcr.Contains(property.PropertyType.GenericTypeArguments[0]); - var isValueRecursive = isDictionary && property.PropertyType.GenericTypeArguments.Length > 1 && - tcr.Contains(property.PropertyType.GenericTypeArguments[1]); - if (isKeyRecursive || isValueRecursive) - { - continue; - } - - var isNullableType = Nullable.GetUnderlyingType(property.PropertyType) != null; - if (isNullableType) - { - continue; - } - - var childValue = property.GetValue(mapValue); - var childMarshalledData = GetMarshalledProperty(marshalledValue, childMember.MarshallName); - Visit(childValue, childMember, childMarshalledData, tcr); - } - } - else - { - ValidateMapValue(mapValue, member, marshalledValue); - } - } - } - else - { - // It's a complex type - var properties = type.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - foreach (var childMember in member.Shape.Members) - { - var property = properties.Single(p => childMember.PropertyName == p.Name); - - var isNullableType = Nullable.GetUnderlyingType(property.PropertyType) != null; - if (isNullableType) - { - continue; - } - - // Check that the child type and any of its generic type arguments types haven't been visited already. - // InstantiateClassGenerator also uses TypeCircularReference to make sure it doesn't recurse infinitely. - var typesToCheck = new List() { property.PropertyType }; - typesToCheck.AddRange(property.PropertyType.GetGenericArguments()); - - if (!typesToCheck.Exists(childType => tcr.Contains(childType))) - { - var childValue = property.GetValue(value); - var childMarshalledData = GetMarshalledProperty(marshalledData, childMember.MarshallName); - Visit(childValue, childMember, childMarshalledData, tcr); - } - } - } - } - finally - { - if (pushed) - tcr.Pop(); - } - } - - private void ValidateMap(IDictionary map, Member member, T marshalledData) { } - - protected virtual void ValidateList(IList list, Member member, T marshalledData) { } - - protected virtual void ValidateMapValue(object mapValue, Member member, T marshalledValue) { } - - protected virtual void ValidateMapKey(object item, Member member, object marshalledKey) { } - - protected virtual void ValidateListMember(object item, Shape shapeWrapper, T marshalledListData) { } - - protected virtual void ValidateProperty(object value, Member member, T marshalledData) { } - - protected abstract IEnumerable GetMarshalledListItem(T marshalledData); - - protected abstract IEnumerable GetMarshalledMapKey(T marshalledData); - - protected abstract T GetMarshalledMapValue(T marshalledData, object key); - - private void ValidateStreamingContent() - { - var payload = this.Operation.RequestPayloadMember; - if (payload != null && payload.IsMemoryStream) - { - Assert.IsTrue(this.MarshalledRequest.Headers.ContainsKey("Content-Type")); - if (!this.Operation.RequestHeaderMembers.Any(h => h.MarshallLocationName.ToLower() == "content-type")) - { - Assert.AreEqual("application/octet-stream", this.MarshalledRequest.Headers["Content-Type"]); - } - Assert.IsNotNull(this.MarshalledRequest.ContentStream); - } - else - { - Assert.IsNull(this.MarshalledRequest.ContentStream); - } - } - - protected void ValidateHeaders(IEnumerable properties) - { - foreach (var property in properties) - { - if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string) || - property.PropertyType == typeof(DateTime)) - { - var member = this.Operation.RequestHeaderMembers.SingleOrDefault(m => - m.PropertyName == property.Name); - if (member != null) - { - Assert.IsTrue(this.MarshalledRequest.Headers.ContainsKey(member.MarshallLocationName)); - if (member.OverrideDataType == null) - { - if (member.IsJsonValue) - { - var encodedValue = Convert.ToBase64String(Encoding.UTF8.GetBytes((string)property.GetValue(this.Request))); - Assert.AreEqual(encodedValue, this.MarshalledRequest.Headers[member.MarshallLocationName]); - } - else if(member.IsTimeStamp) - { - var value = ParseUsingFormat(this.MarshalledRequest.Headers[member.MarshallLocationName], member.TimestampFormat); - Assert.AreEqual(((DateTime)property.GetValue(this.Request)).ToUniversalTime(), value.ToUniversalTime()); - } - else if(member.Shape.IsString) - { - Assert.AreEqual(property.GetValue(this.Request), this.MarshalledRequest.Headers[member.MarshallLocationName]); - } - else - { - var value = this.MarshalledRequest.Headers[member.MarshallLocationName]; - var convertedValue = Convert.ChangeType(value, property.PropertyType); - Assert.AreEqual(property.GetValue(this.Request), convertedValue); - } - } - } - } - } - } - - protected void ValidateQueryParameters(IEnumerable properties) - { - if (this.Operation.RequestQueryStringMembers.Count() > 0) - { - foreach (var property in properties) - { - var member = this.Operation.RequestQueryStringMembers.SingleOrDefault(m => - m.PropertyName == property.Name); - if (member == null) - continue; - if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string)) - { - Assert.IsTrue(this.MarshalledRequest.Parameters.ContainsKey(member.MarshallLocationName)); - var value = this.MarshalledRequest.Parameters[member.MarshallLocationName]; - var convertedValue = Convert.ChangeType(value, property.PropertyType); - Assert.AreEqual(property.GetValue(this.Request), convertedValue); - } - else if(property.PropertyType == typeof(DateTime)) - { - Assert.IsTrue(this.MarshalledRequest.Parameters.ContainsKey(member.MarshallLocationName)); - var value = ParseUsingFormat(this.MarshalledRequest.Parameters[member.MarshallLocationName], - member.TimestampFormat); - Assert.AreEqual(((DateTime)property.GetValue(this.Request)).ToUniversalTime(), value.ToUniversalTime()); - } - } - } - } - - protected void ValidateUriParameters(IEnumerable properties) - { - if (this.Operation.RequestUriMembers.Count() > 0) - { - var splitSegments = this.MarshalledRequest.ResourcePath.Split('?')[0].Split('/'); - var uriSegments = new List(splitSegments.Select( - segment => this.MarshalledRequest?.PathResources.ContainsKey(segment) == true - ? this.MarshalledRequest.PathResources[segment] - : segment - )); - - var operationUri = this.Operation.RequestUri.Split('?')[0]; - var operationUriSegments = operationUri.Split('/'); - - foreach (var property in properties) - { - if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string)) - { - var member = this.Operation.RequestUriMembers.SingleOrDefault(m => - m.PropertyName == property.Name); - if (member != null) - { - var index = -1; - for (int i = 0; i < operationUriSegments.Length; i++) - { - // Determines if a given member should be treated as a greedy path, meaning - // that the resource path contains {MEMBER_NAME+} instead of simply {MEMBER_NAME}. - if ((operationUriSegments[i] == string.Format("{{{0}}}", member.MarshallLocationName))|| (operationUriSegments[i] == string.Format("{{{0}+}}", member.MarshallLocationName))) - { - index = i; break; - } - } - Assert.AreEqual(property.GetValue(this.Request), Convert.ChangeType(uriSegments[index], property.PropertyType)); - } - } - } - } - } - - protected void ValidateHostPrefixParameters(IEnumerable properties) - { - if (!string.IsNullOrEmpty(this.Operation.EndpointHostPrefix)) - { - var hostPrefix = this.MarshalledRequest.HostPrefix; - var hostPrefixTemplate = this.Operation.EndpointHostPrefix; - - foreach (var property in properties) - { - if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string)) - { - var member = this.Operation.RequestHostPrefixMembers.SingleOrDefault(m => - m.PropertyName == property.Name); - if (member != null) - { - // special case for host prefixes such as {AccountId} - if (string.Equals($"{{{member.ModelShape}}}.", hostPrefixTemplate)) - { - hostPrefixTemplate = hostPrefixTemplate.Replace(string.Format("{{{0}}}", member.ModelShape), (string)property.GetValue(this.Request)); - } - else - { - // The host prefix can be included in the body, and in that case the marshall location name will be empty. - var nameToUseForPrefix = string.IsNullOrEmpty(member.MarshallLocationName) ? member.ModeledName : member.MarshallLocationName; - hostPrefixTemplate = hostPrefixTemplate.Replace(string.Format("{{{0}}}", nameToUseForPrefix), (string)property.GetValue(this.Request)); - } - } - } - } - - Assert.AreEqual(hostPrefix, hostPrefixTemplate); - Assert.AreEqual(hostPrefixTemplate.IndexOfAny(new char[] { '{', '}' }), -1); - } - } - - private DateTime ParseUsingFormat(string text, TimestampFormat timestampFormat) - { - if (timestampFormat == TimestampFormat.ISO8601 || - timestampFormat == TimestampFormat.RFC822) - { - return DateTime.Parse(text, CultureInfo.InvariantCulture); - } - else if (timestampFormat == TimestampFormat.UnixTimestamp) - { - var epochSeconds = Double.Parse(text, NumberStyles.Any, CultureInfo.InvariantCulture); - return AWSSDKUtils.EPOCH_START.AddSeconds(epochSeconds); - - } - else - { - throw new InvalidOperationException("Cannot parse for format " + timestampFormat); - } - } - } -} diff --git a/sdk/test/UnitTests/Custom/TestTools/RestJsonValidator.cs b/sdk/test/UnitTests/Custom/TestTools/RestJsonValidator.cs deleted file mode 100644 index 70c73dfbe1a7..000000000000 --- a/sdk/test/UnitTests/Custom/TestTools/RestJsonValidator.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Amazon.Runtime.Internal; -using Json.LitJson; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using ServiceClientGenerator; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml.Linq; - -namespace AWSSDK_DotNet.UnitTests.TestTools -{ - public class RestJsonValidator : RequestValidator - { - public RestJsonValidator(string operation, object request, IRequest marshalledRequest, ServiceModel serviceModel) - : base(operation, request, marshalledRequest, serviceModel) - { - } - - protected override JsonData GetMarshalledData(byte[] content) - { - return JsonMapper.ToObject(new JsonReader(UTF8Encoding.UTF8.GetString(content))); - } - - protected override JsonData GetMarshalledProperty(JsonData marshalledData, string propertyName) - { - var childData = marshalledData[propertyName]; - Assert.IsNotNull(childData, $"Expected to find data in Property [{propertyName}]."); - return childData; - } - - protected override IEnumerable GetMarshalledListItem(JsonData marshalledData) - { - Assert.IsTrue(marshalledData.IsArray); - for (int i = 0; i < marshalledData.Count; i++) - { - yield return marshalledData[i]; - } - } - - protected override IEnumerable GetMarshalledMapKey(JsonData marshalledData) - { - return marshalledData.PropertyNames; - } - - protected override JsonData GetMarshalledMapValue(JsonData marshalledData, object key) - { - var value = marshalledData[key.ToString()]; - Assert.IsNotNull(value); - return value; - } - - protected override void ValidateMapValue(object mapValue, Member member, JsonData marshalledValue) - { - var valueShape = member.Shape.ValueShape; - if (valueShape.IsStructure || valueShape.IsMap || valueShape.IsList) - { - //Implement value checks of these other types - } - else - { - Assert.AreEqual(mapValue.ToString(), marshalledValue.ToString()); - } - } - } -} \ No newline at end of file diff --git a/sdk/test/UnitTests/Custom/TestTools/RestXmlValidator.cs b/sdk/test/UnitTests/Custom/TestTools/RestXmlValidator.cs deleted file mode 100644 index 2b334cec1076..000000000000 --- a/sdk/test/UnitTests/Custom/TestTools/RestXmlValidator.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Xml.Linq; -using System.Threading.Tasks; - -using ServiceClientGenerator; -using Amazon.Runtime.Internal; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Reflection; -using System.Xml; -using System.IO; -using System.Collections; - -namespace AWSSDK_DotNet.UnitTests.TestTools -{ - public class RestXmlValidator : RequestValidator - { - public RestXmlValidator(string operation, object request, IRequest marshalledRequest, ServiceModel serviceModel) - : base (operation,request,marshalledRequest,serviceModel) { } - - protected override XElement GetMarshalledData(byte[] content) - { - return XElement.Load(new MemoryStream(content)); - } - - protected override XElement GetMarshalledProperty(XElement marshalledData, string propertyName) - { - var childProp = marshalledData.Elements().SingleOrDefault(x => x.Name.LocalName == propertyName); - Assert.IsNotNull(childProp); - return childProp; - } - - protected override void ValidateProperty(object value, Member member, XElement marshalledData) - { - Assert.AreEqual(member.MarshallName, marshalledData.Name.LocalName); - Assert.AreEqual(this.Operation.XmlNamespace, marshalledData.Name.NamespaceName); - } - - protected override IEnumerable GetMarshalledListItem(XElement marshalledData) - { - return marshalledData.Elements(); - } - - protected override void ValidateList(IList list, Member member, XElement marshalledData) - { - Assert.AreEqual(member.MarshallName, marshalledData.Name.LocalName); - } - - protected override void ValidateListMember(object item, Shape shape, XElement marshalledListData) - { - Assert.AreEqual(shape.ListMarshallName ?? "member", marshalledListData.Name.LocalName); - Assert.AreEqual(this.Operation.XmlNamespace, marshalledListData.Name.NamespaceName); - } - - protected override IEnumerable GetMarshalledMapKey(XElement marshalledData) - { - var keys = marshalledData.Descendants().Where(e => e.Name.LocalName == "key").Select(e => e.Value); - Assert.IsNotNull(keys); - return keys; - } - - protected override XElement GetMarshalledMapValue(XElement marshalledData, object key) - { - /* Sample format for map entries - - key0 - test-value - - - key1 - test-value - - - key2 - test-value - - */ - var entryElements = marshalledData.Elements().Select(e => e.Elements().ToDictionary(k => k.Name.LocalName)); - var matchedEntry = entryElements.Single(elementsMap => elementsMap["key"].Value == key.ToString()); - return matchedEntry["value"]; - } - - protected override void ValidateMapValue(object mapValue, Member member, XElement marshalledValue) - { - var valueShape = member.Shape.ValueShape; - if (valueShape.IsStructure || valueShape.IsMap || valueShape.IsList) - { - //Implement value checks of these other types - } - else - { - Assert.AreEqual(mapValue.ToString(), marshalledValue.Value); - } - } - } -} diff --git a/sdk/test/UnitTests/Custom/TestTools/SpecMarkdownTestDataHelper.cs b/sdk/test/UnitTests/Custom/TestTools/SpecMarkdownTestDataHelper.cs index 17dba82de539..39e6a503b8ae 100644 --- a/sdk/test/UnitTests/Custom/TestTools/SpecMarkdownTestDataHelper.cs +++ b/sdk/test/UnitTests/Custom/TestTools/SpecMarkdownTestDataHelper.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; -using ThirdParty.Json.LitJson; - namespace AWSSDK.UnitTests { public static class SpecMarkdownTestDataHelper