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());
                }
            }
            protected override AnyValue ConsumeNext(ValueType?typeHint, ref object current)
            {
                var element = current;

                if (!_allowTypeCoercion)
                {
                    typeHint = null;
                }
                switch (element)
                {
                case null:
                    return(AnyValue.Null());

                case bool value:
                    return(FromBool(value, typeHint));

                case int value:
                    return(FromNumber(value, typeHint));

                case long value:
                    return(FromNumber(value, typeHint));

                case float value:
                    return(FromNumber(value, typeHint));

                case double value:
                    return(FromNumber(value, typeHint));

                case string value:
                    return(FromString(value, typeHint));

                case IEnumerable <KeyValuePair <string, object> > dict:
                    PushContext(new ParentContext {
                        ObjectEnumerator = dict.GetEnumerator()
                    });
                    return(AnyValue.Object(new ObjectReader()));

                case IEnumerable <KeyValuePair <object, object> > dict:
                    var transformedDict = dict.Select(kv => new KeyValuePair <string, object>(kv.Key.ToString(), kv.Value));
                    PushContext(new ParentContext {
                        ObjectEnumerator = transformedDict.GetEnumerator()
                    });
                    return(AnyValue.Object(new ObjectReader()));

                case IEnumerable <object> enumerable:
                    PushContext(new ParentContext {
                        ArrayEnumerator = enumerable.GetEnumerator()
                    });
                    return(AnyValue.Array(new ArrayReader()));

                default:
                    throw new SyntaxException(string.Format("ReaderAdapters.FromSimpleTypes does not support type {0}",
                                                            element.GetType()), null);
                }
            }
Exemplo n.º 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());
            }
        }