Exemplo n.º 1
0
        /// <summary>
        /// Reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="Boolean"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{T}"/> of <see cref="Boolean"/>. This method will return <c>null</c> at the end of an array.</returns>
        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 HAVE_BIG_INTEGER
                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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="Double"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{T}"/> of <see cref="Double"/>. This method will return <c>null</c> at the end of an array.</returns>
        public virtual double?ReadAsDouble()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.Integer:
            case JsonToken.Float:
                if (!(Value is double))
                {
                    double d;
#if HAVE_BIG_INTEGER
                    if (Value is BigInteger)
                    {
                        d = (double)(BigInteger)Value;
                    }
                    else
#endif
                    {
                        d = Convert.ToDouble(Value, CultureInfo.InvariantCulture);
                    }

                    SetToken(JsonToken.Float, d, false);
                }

                return((double)Value);

            case JsonToken.String:
                return(ReadDoubleString((string)Value));
            }

            throw JsonReaderException.Create(this, "Error reading double. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads the next JSON token from the source as a <see cref="String"/>.
        /// </summary>
        /// <returns>A <see cref="String"/>. This method will return <c>null</c> at the end of an array.</returns>
        public virtual string ReadAsString()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.String:
                return((string)Value);
            }

            if (JsonTokenUtils.IsPrimitiveToken(t))
            {
                if (Value != null)
                {
                    string       s;
                    IFormattable formattable = Value as IFormattable;
                    if (formattable != null)
                    {
                        s = formattable.ToString(null, Culture);
                    }
                    else
                    {
                        Uri uri = Value as Uri;
                        s = uri != null ? uri.OriginalString : Value.ToString();
                    }

                    SetToken(JsonToken.String, s, false);
                    return(s);
                }
            }

            throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Exemplo n.º 4
0
 internal JsonReaderException CreateUnexpectedEndException()
 {
     return(JsonReaderException.Create(this, "Unexpected end when reading JSON."));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Reads the next JSON token from the source as a <see cref="Byte"/>[].
        /// </summary>
        /// <returns>A <see cref="Byte"/>[] or <c>null</c> if the next JSON token is null. This method will return <c>null</c> at the end of an array.</returns>
        public virtual byte[] ReadAsBytes()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
            case JsonToken.StartObject:
            {
                ReadIntoWrappedTypeObject();

                byte[] data = ReadAsBytes();
                ReaderReadAndAssert();

                if (TokenType != JsonToken.EndObject)
                {
                    throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
                }

                SetToken(JsonToken.Bytes, data, false);
                return(data);
            }

            case JsonToken.String:
            {
                // attempt to convert possible base 64 or GUID string to bytes
                // GUID has to have format 00000000-0000-0000-0000-000000000000
                string s = (string)Value;

                byte[] data;

                Guid g;
                if (s.Length == 0)
                {
                    data = CollectionUtils.ArrayEmpty <byte>();
                }
                else if (ConvertUtils.TryConvertGuid(s, out g))
                {
                    data = g.ToByteArray();
                }
                else
                {
                    data = Convert.FromBase64String(s);
                }

                SetToken(JsonToken.Bytes, data, false);
                return(data);
            }

            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.Bytes:
                if (ValueType == typeof(Guid))
                {
                    byte[] data = ((Guid)Value).ToByteArray();
                    SetToken(JsonToken.Bytes, data, false);
                    return(data);
                }

                return((byte[])Value);

            case JsonToken.StartArray:
                return(ReadArrayIntoByteArray());
            }

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