/// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            var bytes = value as byte[];

            if (bytes == null)
            {
                return(false);
            }

            //Enforce limit on size of array
            if (_config.ByteArrayLimit > 0 && bytes.Length > _config.ByteArrayLimit)
            {
                string hexValue    = string.Concat(bytes.Take(16).Select(b => b.ToString("X2")));
                string description = $"0x: {hexValue}... ({bytes.Length} bytes)";
                result = new ScalarToken(description);
                return(true);
            }

            result = new ScalarToken(bytes.ToArray());
            return(true);
        }
        /// <summary>
        /// Formats content of <see cref="ScalarToken"/>.
        /// </summary>
        /// <param name="token">Token to be formatted.</param>
        public void Format(ScalarToken token)
        {
            if (token.Value == null)
            {
                _output.Write("null");
                return;
            }

            string text = token.Value as string;

            if (text != null)
            {
                _output.Write("\"");
                _output.Write(text.Replace("\"", "\\\""));
                _output.Write("\"");
                return;
            }

            //If object has custom formatter then use that one
            var custom = (ICustomFormatter)_formatProvider.GetFormat(typeof(ICustomFormatter));

            if (custom != null)
            {
                _output.Write(custom.Format(null, token.Value, _formatProvider));
                return;
            }

            // Use defaults for all other formatting.
            var f = token.Value as IFormattable;

            _output.Write(f?.ToString(null, _formatProvider) ?? token.Value.ToString());
        }
        internal Boolean AllowScalar(
            Boolean expand,
            out ScalarToken scalar)
        {
            m_memory.IncrementEvents();

            if (expand)
            {
                Unravel(expand: true);
            }

            if (m_current?.Value is ScalarToken scalarToken)
            {
                scalar = scalarToken;

                // Add bytes before they are emitted to the caller (so the caller doesn't have to track bytes)
                m_memory.AddBytes(scalar);

                MoveNext();
                return(true);
            }

            scalar = null;
            return(false);
        }
Example #4
0
        public void ScalarToken_ScalarType_ScalarContainsInputValue()
        {
            string value = "Hello World!";

            var scalar = new ScalarToken(value);

            Assert.Equal(scalar.Value, value);
        }
        public void Format_NullScalarToken_IsFormatedAsNullString()
        {
            var output   = new StringWriter();
            var formater = new JsonPropertyFormatter(output);
            var token    = new ScalarToken(null);

            token.Render(formater);

            Assert.Equal("null", output.ToString());
        }
        public void Format_StringScalarToken_SpecialCharactersAreEscaped()
        {
            var output   = new StringWriter();
            var formater = new JsonPropertyFormatter(output);
            var token    = new ScalarToken(" \" ");

            token.Render(formater);

            Assert.Equal("\" \\\" \"", output.ToString());
        }
        public void Format_IntScalarToken_IsFormatedAsNumber()
        {
            var     output   = new StringWriter();
            var     formater = new JsonPropertyFormatter(output);
            decimal value    = Some.Int();
            var     token    = new ScalarToken(value);

            token.Render(formater);

            Assert.Equal(value.ToString(CultureInfo.InvariantCulture), output.ToString());
        }
Example #8
0
        public void Render_SomeFormatter_FormatterIsCalled()
        {
            var formatterMock = new Mock <IPropertyFormatter>();

            var formatter = formatterMock.Object;
            var scalar    = new ScalarToken(null);

            scalar.Render(formatter);

            formatterMock.Verify(m => m.Format(scalar), Times.Once);
        }
        public void Format_NonFormattableScalarToken_IsFormatedFromToString()
        {
            var output   = new StringWriter();
            var formater = new JsonPropertyFormatter(output);
            var value    = new Nonformatable();
            var token    = new ScalarToken(value);

            token.Render(formater);

            Assert.Equal(value.ToString(), output.ToString());
        }
        public void Format_DecimalScalarToken_IsFormatedUsingSpecificCulture()
        {
            var     formatProvider = new CultureInfo("cs-CZ");
            var     output         = new StringWriter();
            var     formater       = new JsonPropertyFormatter(output, formatProvider);
            decimal value          = Some.Decimal();
            var     token          = new ScalarToken(value);

            token.Render(formater);

            Assert.Equal(value.ToString(formatProvider), output.ToString());
        }
Example #11
0
        private void Validate(
            ref ScalarToken scalar,
            DefinitionInfo definition)
        {
            switch (scalar.Type)
            {
            case TokenType.Null:
            case TokenType.Boolean:
            case TokenType.Number:
            case TokenType.String:
                var literal = scalar as LiteralToken;

                // Legal
                if (definition.Get <ScalarDefinition>().Any(x => x.IsMatch(literal)))
                {
                    return;
                }

                // Not a string, convert
                if (literal.Type != TokenType.String)
                {
                    literal = new StringToken(literal.FileId, literal.Line, literal.Column, literal.ToString());

                    // Legal
                    if (definition.Get <StringDefinition>().Any(x => x.IsMatch(literal)))
                    {
                        scalar = literal;
                        return;
                    }
                }

                // Illegal
                m_context.Error(literal, TemplateStrings.UnexpectedValue(literal));
                break;

            case TokenType.BasicExpression:

                // Illegal
                if (definition.AllowedContext.Length == 0)
                {
                    m_context.Error(scalar, TemplateStrings.ExpressionNotAllowed());
                }

                break;

            default:
                m_context.Error(scalar, TemplateStrings.UnexpectedValue(scalar));
                break;
            }
        }
Example #12
0
        /// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            var del = value as Delegate;

            if (del == null)
            {
                return(false);
            }

            result = new ScalarToken(del.GetMethodInfo().ToString());
            return(true);
        }
        /// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            // These types and their subclasses are property-laden and deep
            // Most of targets will convert them to strings
            if (!(value is Type) && !(value is MemberInfo))
            {
                return(false);
            }

            result = new ScalarToken(value.ToString());
            return(true);
        }
Example #14
0
        /// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            if (value == null)
            {
                return(false);
            }
            if (!value.GetType().GetTypeInfo().IsEnum)
            {
                return(false);
            }

            result = new ScalarToken(value);
            return(true);
        }
        /// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            string text = value as string;

            if (text == null)
            {
                return(false);
            }

            if (_config.StringLimit > 0 && text.Length > _config.StringLimit)
            {
                text = text.Substring(0, _config.StringLimit) + "…";
            }

            result = new ScalarToken(text);
            return(true);
        }
        /// <summary>
        /// If supported, convert the provided value into a <see cref="IPropertyToken"/>.
        /// </summary>
        /// <param name="converter">Converter for conversion of additional values.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">Value converted to <see cref="IPropertyToken"/> if conversion was successful.</param>
        /// <returns><c>true</c> if the value could be converted under this policy; <c>false</c> otherwise.</returns>
        public bool TryConvert(IPropertyConverter converter, object value, out IPropertyToken result)
        {
            result = null;

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            if (value == null)
            {
                result = new ScalarToken(null);
                return(true);
            }

#if NET45 || NET46
            if (value is DBNull)
            {
                result = new ScalarToken(null);
                return(true);
            }
#endif
            return(false);
        }
Example #17
0
        public void Render_NullFormatter_ThrowsArgumentNullException()
        {
            var scalar = new ScalarToken(null);

            Assert.Throws <ArgumentNullException>(() => scalar.Render(null));
        }