Exemplo n.º 1
0
        private bool TryVisitNumber <T>(VisitArgs args, Func <decimal?, T> converter, out T value)
        {
            var node = ParseUntilFound(args);

            if (node is JsonUndefined)
            {
                value = default;
                return(false);
            }
            if (node.IsNull)
            {
                value = default;
                return(true);
            }

            if (node is JsonString str)
            {
                var parsedNumber = decimal.Parse(str.Value);
                value = converter.Invoke(parsedNumber);
                return(true);
            }
            if (!(node is JsonNumber number))
            {
                throw UnexpectedJsonException.Type(args.Name, node, typeof(T));
            }
            value = converter.Invoke(number.Value);
            return(true);
        }
Exemplo n.º 2
0
        public bool TryVisitValue(VisitArgs args, out bool?value)
        {
            var node = ParseUntilFound(args);

            if (node is JsonUndefined)
            {
                value = null;
                return(false);
            }
            if (node.IsNull)
            {
                value = null;
                return(true);
            }
            if (!(node is JsonBool b))
            {
                throw UnexpectedJsonException.Type(args.Name, node, typeof(bool));
            }
            value = b.Value;
            return(true);
        }
Exemplo n.º 3
0
        private bool TryVisitString <T>(VisitArgs args, Func <string, T> converter, out T value)
        {
            var node = ParseUntilFound(args);

            if (node is JsonUndefined)
            {
                value = default(T);
                return(false);
            }
            if (node.IsNull)
            {
                value = default(T);
                return(true);
            }
            if (!(node is JsonString s))
            {
                throw UnexpectedJsonException.Type(args.Name, node, typeof(T));
            }
            value = converter.Invoke(s.Value);
            return(true);
        }