Beispiel #1
0
        internal int?ReadAsInt32Internal()
        {
            _readType = ReadType.ReadAsInt32;

            JsonToken t;

            do
            {
                if (!ReadInternal())
                {
                    SetToken(JsonToken.None);
                    return(null);
                }
                else
                {
                    t = TokenType;
                }
            } while (t == JsonToken.Comment);

            if (t == JsonToken.Integer || t == JsonToken.Float)
            {
                if (!(Value is int))
                {
                    SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture));
                }

                return((int)Value);
            }

            if (t == JsonToken.Null)
            {
                return(null);
            }

            int i;

            if (t == JsonToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(JsonToken.Null);
                    return(null);
                }

                if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
                {
                    SetToken(JsonToken.Integer, i);
                    return(i);
                }
                else
                {
                    throw JsonReaderException.Create(this, "Could not convert string to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
                }
            }

            if (t == JsonToken.EndArray)
            {
                return(null);
            }

            throw JsonReaderException.Create(this, "Error reading integer. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }
Beispiel #2
0
        internal byte[] ReadAsBytesInternal()
        {
            _readType = ReadType.ReadAsBytes;

            JsonToken t;

            do
            {
                if (!ReadInternal())
                {
                    SetToken(JsonToken.None);
                    return(null);
                }
                else
                {
                    t = TokenType;
                }
            } while (t == JsonToken.Comment);

            if (IsWrappedInTypeObject())
            {
                byte[] data = ReadAsBytes();
                ReadInternal();
                SetToken(JsonToken.Bytes, data);
                return(data);
            }

            // attempt to convert possible base 64 string to bytes
            if (t == JsonToken.String)
            {
                string s    = (string)Value;
                byte[] data = (s.Length == 0) ? new byte[0] : Convert.FromBase64String(s);
                SetToken(JsonToken.Bytes, data);
                return(data);
            }

            if (t == JsonToken.Null)
            {
                return(null);
            }

            if (t == JsonToken.Bytes)
            {
                return((byte[])Value);
            }

            if (t == JsonToken.StartArray)
            {
                List <byte> data = new List <byte>();

                while (ReadInternal())
                {
                    t = TokenType;
                    switch (t)
                    {
                    case JsonToken.Integer:
                        data.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture));
                        break;

                    case JsonToken.EndArray:
                        byte[] d = data.ToArray();
                        SetToken(JsonToken.Bytes, d);
                        return(d);

                    case JsonToken.Comment:
                        // skip
                        break;

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

                throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
            }

            if (t == JsonToken.EndArray)
            {
                return(null);
            }

            throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Beispiel #3
0
        internal decimal?ReadAsDecimalInternal()
        {
            _readType = ReadType.ReadAsDecimal;

            JsonToken t;

            do
            {
                if (!ReadInternal())
                {
                    SetToken(JsonToken.None);
                    return(null);
                }
                else
                {
                    t = TokenType;
                }
            } while (t == JsonToken.Comment);

            if (t == JsonToken.Integer || t == JsonToken.Float)
            {
                if (!(Value is decimal))
                {
                    SetToken(JsonToken.Float, Convert.ToDecimal(Value, CultureInfo.InvariantCulture));
                }

                return((decimal)Value);
            }

            if (t == JsonToken.Null)
            {
                return(null);
            }

            decimal d;

            if (t == JsonToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(JsonToken.Null);
                    return(null);
                }

                if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
                {
                    SetToken(JsonToken.Float, d);
                    return(d);
                }
                else
                {
                    throw JsonReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
                }
            }

            if (t == JsonToken.EndArray)
            {
                return(null);
            }

            throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Beispiel #4
0
        internal DateTimeOffset?ReadAsDateTimeOffsetInternal()
        {
            _readType = ReadType.ReadAsDateTimeOffset;

            JsonToken t;

            do
            {
                if (!ReadInternal())
                {
                    SetToken(JsonToken.None);
                    return(null);
                }
                else
                {
                    t = TokenType;
                }
            } while (t == JsonToken.Comment);

            if (t == JsonToken.Date)
            {
                if (Value is DateTime)
                {
                    SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value));
                }

                return((DateTimeOffset)Value);
            }

            if (t == JsonToken.Null)
            {
                return(null);
            }

            DateTimeOffset dt;

            if (t == JsonToken.String)
            {
                string s = (string)Value;
                if (string.IsNullOrEmpty(s))
                {
                    SetToken(JsonToken.Null);
                    return(null);
                }

                if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
                {
                    SetToken(JsonToken.Date, dt);
                    return(dt);
                }
                else
                {
                    throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
                }
            }

            if (t == JsonToken.EndArray)
            {
                return(null);
            }

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