/// <inheritdoc/>
        protected override bool TryConvert(JsonElement source, out object?result)
        {
            if (source.ValueKind != JsonValueKind.Array)
            {
                result = null;
                return(false);
            }

            var values = new object?[source.GetArrayLength()];

            var i = 0;

            foreach (var array in source.EnumerateArray())
            {
                if (!_convertible.TryConvert(array, out var value))
                {
                    result = null;
                    return(false);
                }

                values[i] = value;
                i++;
            }

            result = values;
            return(true);
        }
        /// <inheritdoc />
        protected override bool TryConvert(JToken source, out object?result)
        {
            if (source.Type == JTokenType.Array)
            {
                result = null;
                return(false);
            }

            var array  = source.Value <JArray>();
            var values = new object?[array.Count];

            var i = 0;

            foreach (var token in array)
            {
                if (!_convertible.TryConvert(token, out var value))
                {
                    result = null;
                    return(false);
                }

                values[i] = value;
                i++;
            }

            result = values;
            return(true);
        }
        /// <summary>
        /// Try to add Action to collection.
        /// </summary>
        /// <param name="source">The <see cref="object"/> to be convert to <see cref="ThingActionInformation"/>.</param>
        /// <param name="info">The <see cref="ThingActionInformation"/> created.</param>
        /// <returns>Return true if could convert and added on collection, otherwise return false.</returns>
        public bool TryAdd(object source, [NotNullWhen(true)] out ThingActionInformation?info)
        {
            info = null;

            if (!_inputConvertible.TryConvert(source, out var inputProperty))
            {
                return(false);
            }

            if (!_inputValidation.IsValid(inputProperty))
            {
                return(false);
            }

            info = _actionInformationFactory.Convert(_convertible.Convert(inputProperty) as Dictionary <string, object?>);

            info.StatusChanged += OnStatusChange;

            return(_actions.TryAdd(info.GetId(), info));
        }
示例#4
0
        public void TryAdd_Should_ReturnFalse_When_TryConvertReturnFalse()
        {
            var source = _fixture.Create <string>();

            _jsonConvertible.TryConvert(source, out Arg.Any <object>())
            .Returns(x =>
            {
                x[1] = null;
                return(false);
            });


            _collection.TryAdd(source, out _).Should().BeFalse();

            _jsonConvertible
            .Received(1)
            .TryConvert(source, out Arg.Any <object>());
        }