public static object ConsumeJsonElement()

in src/Elastic.Transport/Responses/Dynamic/DynamicValue.cs [498:538]


	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
		};
	}