public T Get <T>(string path)
        {
            var dynamicDictionary = Value switch
            {
                DynamicDictionary v => v,
                IDictionary <string, object> v => DynamicDictionary.Create(v),
                JsonElement e => DynamicDictionary.Create(e),
                _ => null
            };

            return(dynamicDictionary == null ? default : dynamicDictionary.Get <T>(path));
        }
        /// <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
            });