Newtonsoft.Json.JsonReader.ReadAsBoolean C# (CSharp) Method

ReadAsBoolean() public method

Reads the next JSON token from the stream as a Nullable{T} of Boolean.
public ReadAsBoolean ( ) : bool?
return bool?
        public virtual bool? ReadAsBoolean()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
                case JsonToken.None:
                case JsonToken.Null:
                case JsonToken.EndArray:
                    return null;
                case JsonToken.Integer:
                case JsonToken.Float:
                    bool b;
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_1
                    if (Value is BigInteger)
                    {
                        b = (BigInteger)Value != 0;
                    }
                    else
#endif
                    {
                        b = Convert.ToBoolean(Value, CultureInfo.InvariantCulture);
                    }

                    SetToken(JsonToken.Boolean, b, false);

                    return b;
                case JsonToken.String:
                    return ReadBooleanString((string)Value);
                case JsonToken.Boolean:
                    return (bool)Value;
            }

            throw JsonReaderException.Create(this, "Error reading boolean. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }

Usage Example

        /// <summary>
        /// Reads the next JSON token from the underlying <see cref="JsonReader"/> as a <see cref="Nullable{T}"/> of <see cref="Boolean"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{T}"/> of <see cref="Boolean"/>.</returns>
        public override bool?ReadAsBoolean()
        {
            bool?b = _reader.ReadAsBoolean();

            ValidateCurrentToken();
            return(b);
        }
All Usage Examples Of Newtonsoft.Json.JsonReader::ReadAsBoolean