protected override AnyValue ConsumeNext(ValueType?typeHint, ref JsonElement current)
            {
                var element = current;

                switch (element.ValueKind)
                {
                case JsonValueKind.True:
                    return(AnyValue.Bool(true));

                case JsonValueKind.False:
                    return(AnyValue.Bool(false));

                case JsonValueKind.Number:
                    return(AnyValue.Number(element.GetDouble()));

                case JsonValueKind.String:
                    return(AnyValue.String(element.GetString()));

                case JsonValueKind.Array:
                    PushContext(new ParentContext {
                        ArrayEnumerator = element.EnumerateArray()
                    });
                    return(AnyValue.Array(new ArrayReader(true)));

                case JsonValueKind.Object:
                    PushContext(new ParentContext {
                        ObjectEnumerator = element.EnumerateObject()
                    });
                    return(AnyValue.Object(new ObjectReader(true, null)));

                default:
                    return(AnyValue.Null());
                }
            }
 private AnyValue FromNumber(double value, ValueType?typeHint)
 {
     switch (typeHint)
     {
     case ValueType.String:
         return(AnyValue.String(value.ToString()));
     }
     return(AnyValue.Number(value));
 }
Exemple #3
0
        /// <summary>
        /// Reads a single value of any type, if it is a scalar value or a null, or prepares to read
        /// the value if it is an array or object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The returned <see cref="AnyValue"/>'s <see cref="AnyValue.Type"/> property indicates the
        /// value type. If it is <see cref="ValueType.Bool"/>, <see cref="ValueType.Number"/>, or
        /// <see cref="ValueType.String"/>, check the corresponding <see cref="AnyValue.BoolValue"/>,
        /// <see cref="AnyValue.NumberValue"/>, or <see cref="AnyValue.StringValue"/> property. If it is
        /// <see cref="ValueType.Array"/> or <see cref="ValueType.Object"/>, the <c>AnyValue</c>'s
        /// <see cref="AnyValue.ArrayValue"/> or <see cref="AnyValue.ObjectValue"/> property has been
        /// initialized with an <see cref="ArrayReader"/> or <see cref="ObjectReader"/> just as if you
        /// had called the <c>JReader</c>'s <see cref="Array"/> or <see cref="Object"/> method.
        /// </para>
        /// </remarks>
        /// <returns>a scalar value or a marker for the beginning of a complex value</returns>
        /// <exception cref="SyntaxException">if there is a JSON parsing error</exception>
        public AnyValue Any()
        {
            _awaitingReadValue = false;
            if (_delegate != null)
            {
                var value = _delegate.NextValue(null, true);
                switch (value.Type)
                {
                // For arrays and objects, we need to set IsDefined to true here; the adapter won't
                // be able to do that if it can't see our internal APIs.
                case ValueType.Array:
                    return(AnyValue.Array(new ArrayReader(true)));

                case ValueType.Object:
                    return(AnyValue.Object(new ObjectReader(true, null)));
                }
                return(value);
            }
            var token = _tr.Any();

            switch (token.Type)
            {
            case ValueType.Bool:
                return(AnyValue.Bool(token.BoolValue));

            case ValueType.Number:
                return(AnyValue.Number(token.NumberValue));

            case ValueType.String:
                return(AnyValue.String(token.StringValue));

            case ValueType.Array:
                return(AnyValue.Array(new ArrayReader(true)));

            case ValueType.Object:
                return(AnyValue.Object(new ObjectReader(true, null)));

            default:
                return(AnyValue.Null());
            }
        }
            private AnyValue FromString(string value, ValueType?typeHint)
            {
                switch (typeHint)
                {
                case ValueType.Bool:
                    if (value == "true" || value == "on")
                    {
                        return(AnyValue.Bool(true));
                    }
                    if (value == "false" || value == "off")
                    {
                        return(AnyValue.Bool(false));
                    }
                    break;

                case ValueType.Number:
                    if (double.TryParse(value, out var n))
                    {
                        return(AnyValue.Number(n));
                    }
                    break;
                }
                return(AnyValue.String(value));
            }