/// <summary>
        ///     Tries to convert a value to the specified type.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/>.</param>
        /// <param name="convertedValue">The converted value.</param>
        /// <returns>True if the value was converted; otherwise, false.</returns>
        protected virtual bool TryConvertValue(object?value, Type targetType, JsonSerializerOptions jsonSerializerOptions, out object?convertedValue)
        {
            var conversionResult = ConversionResultProvider.ConvertTo(value, targetType, jsonSerializerOptions);

            if (!conversionResult.CanBeConverted)
            {
                convertedValue = null;
                return(false);
            }

            convertedValue = conversionResult.ConvertedInstance;
            return(true);
        }
Exemple #2
0
        /// <summary>
        ///     Tries to convert a value.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/>.</param>
        /// <param name="convertedValue">The converted value.</param>
        /// <param name="errorMessage">An optional error message if the key could not be converted; otherwise, null.</param>
        /// <returns>True if the value was converted; otherwise, false.</returns>
        protected virtual bool TryConvertValue(object?value, JsonSerializerOptions jsonSerializerOptions, [MaybeNull] out TValue convertedValue, out string?errorMessage)
        {
            var conversionResult = ConversionResultProvider.ConvertTo(value, typeof(TValue), jsonSerializerOptions);

            if (conversionResult.CanBeConverted)
            {
                errorMessage   = null;
                convertedValue = conversionResult.ConvertedInstance != null ? (TValue)conversionResult.ConvertedInstance : default;
                return(true);
            }

            errorMessage   = $"The value '{value}' is invalid for target location.";
            convertedValue = default;
            return(false);
        }
Exemple #3
0
        /// <summary>
        ///     Tries to convert a key.
        /// </summary>
        /// <param name="key">The key to convert.</param>
        /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/>.</param>
        /// <param name="convertedKey">The converted key.</param>
        /// <param name="errorMessage">An optional error message if the key could not be converted; otherwise, null.</param>
        /// <returns>True if the key was converted; otherwise, false.</returns>
        protected virtual bool TryConvertKey(string?key, JsonSerializerOptions jsonSerializerOptions, [MaybeNullWhen(false)] out TKey convertedKey, out string?errorMessage)
        {
            var conversionResult = ConversionResultProvider.ConvertTo(key, typeof(TKey), jsonSerializerOptions);

            if (conversionResult.CanBeConverted && conversionResult.ConvertedInstance != null)
            {
                errorMessage = null;
                convertedKey = (TKey)conversionResult.ConvertedInstance;
                return(true);
            }

            errorMessage = $"The provided path segment '{key}' cannot be converted to the target type.";
            convertedKey = default;
            return(false);
        }