/// <summary>
        /// Returns the value as a dictionary if the current value represents an object.
        /// Otherwise returns null.
        /// </summary>
        public IDictionary <string, DynamicValue> ToDictionary()
        {
            if (_value is IDictionary <string, object> dict)
            {
                return(DynamicDictionary.Create(dict));
            }
            else if (_value is JsonElement e && e.ValueKind == JsonValueKind.Object)
            {
                var d = e.EnumerateObject()
                        .Aggregate(new Dictionary <string, object>(), (dictionary, je) =>
                {
                    dictionary.Add(je.Name, je.Value);
                    return(dictionary);
                });
                return(DynamicDictionary.Create(d));
            }

            return(null);
        }
        public static object ConsumeJsonElement(Type targetReturnType, JsonElement e)
        {
            // ReSharper disable once HeapView.BoxingAllocation
            object ParseNumber(JsonElement el)
            {
                if (el.TryGetInt64(out var l))
                {
                    return(l);
                }

                return(el.GetDouble());
            }

            return(targetReturnType switch
            {
                _ when targetReturnType == typeof(bool) => e.GetBoolean(),
                _ when targetReturnType == typeof(byte) => e.GetByte(),
                _ when targetReturnType == typeof(decimal) => e.GetDecimal(),
                _ when targetReturnType == typeof(double) => e.GetDouble(),
                _ when targetReturnType == typeof(Guid) => e.GetGuid(),
                _ when targetReturnType == typeof(short) => e.GetInt16(),
                _ when targetReturnType == typeof(int) => e.GetInt32(),
                _ when targetReturnType == typeof(long) => e.GetInt64(),
                _ when targetReturnType == typeof(float) => e.GetSingle(),
                _ when targetReturnType == typeof(string) => e.GetString(),
                _ when targetReturnType == typeof(DateTime) => e.GetDateTime(),
                _ when targetReturnType == typeof(DateTimeOffset) => e.GetDateTimeOffset(),
                _ when targetReturnType == typeof(ushort) => e.GetUInt16(),
                _ when targetReturnType == typeof(uint) => e.GetUInt32(),
                _ when targetReturnType == typeof(ulong) => e.GetUInt64(),
                _ when targetReturnType == typeof(sbyte) => e.GetSByte(),
                _ when targetReturnType == typeof(DynamicDictionary) => DynamicDictionary.Create(e),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Array =>
                e.EnumerateArray().Select(je => ConsumeJsonElement(targetReturnType, je)).ToArray(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Object => e.ToDictionary(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.True => true,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.False => false,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Null => null,
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.String => e.GetString(),
                _ when targetReturnType == typeof(object) && e.ValueKind == JsonValueKind.Number => ParseNumber(e),
                _ => null
            });
        private static bool SetSpecialTypes <TResponse>(string mimeType, byte[] bytes, IMemoryStreamFactory memoryStreamFactory, out TResponse cs)
            where TResponse : class, ITransportResponse, new()
        {
            cs = null;
            var responseType = typeof(TResponse);

            if (!SpecialTypes.Contains(responseType))
            {
                return(false);
            }

            if (responseType == typeof(StringResponse))
            {
                cs = new StringResponse(bytes.Utf8String()) as TResponse;
            }
            else if (responseType == typeof(BytesResponse))
            {
                cs = new BytesResponse(bytes) as TResponse;
            }
            else if (responseType == typeof(VoidResponse))
            {
                cs = VoidResponse.Default as TResponse;
            }
            else if (responseType == typeof(DynamicResponse))
            {
                //if not json store the result under "body"
                if (mimeType == null || !mimeType.StartsWith(RequestData.MimeType))
                {
                    var dictionary = new DynamicDictionary();
                    dictionary["body"] = new DynamicValue(bytes.Utf8String());
                    cs = new DynamicResponse(dictionary) as TResponse;
                }
                else
                {
                    using var ms = memoryStreamFactory.Create(bytes);
                    var body = LowLevelRequestResponseSerializer.Instance.Deserialize <DynamicDictionary>(ms);
                    cs = new DynamicResponse(body) as TResponse;
                }
            }
            return(cs != null);
        }