Create() static private method

static private Create ( IJsonLineInfo lineInfo, string path, string message, Exception ex ) : JsonReaderException
lineInfo IJsonLineInfo
path string
message string
ex System.Exception
return JsonReaderException
示例#1
0
        // Token: 0x06000C0A RID: 3082 RVA: 0x0004F940 File Offset: 0x0004DB40
        internal decimal?ReadDecimalString(string s)
        {
            if (StringUtils.IsNullOrEmpty(s))
            {
                this.SetToken(JsonToken.Null, null, false);
                return(null);
            }
            decimal num;

            if (decimal.TryParse(s, NumberStyles.Number, this.Culture, out num))
            {
                this.SetToken(JsonToken.Float, num, false);
                return(new decimal?(num));
            }
            if (ConvertUtils.DecimalTryParse(s.ToCharArray(), 0, s.Length, out num) == ParseResult.Success)
            {
                this.SetToken(JsonToken.Float, num, false);
                return(new decimal?(num));
            }
            this.SetToken(JsonToken.String, s, false);
            throw JsonReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, s));
        }
示例#2
0
        // Token: 0x06000C0E RID: 3086 RVA: 0x0004FBF4 File Offset: 0x0004DDF4
        internal DateTimeOffset?ReadDateTimeOffsetString(string s)
        {
            if (StringUtils.IsNullOrEmpty(s))
            {
                this.SetToken(JsonToken.Null, null, false);
                return(null);
            }
            DateTimeOffset dateTimeOffset;

            if (DateTimeUtils.TryParseDateTimeOffset(s, this._dateFormatString, this.Culture, out dateTimeOffset))
            {
                this.SetToken(JsonToken.Date, dateTimeOffset, false);
                return(new DateTimeOffset?(dateTimeOffset));
            }
            if (!DateTimeOffset.TryParse(s, this.Culture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
            {
                this.SetToken(JsonToken.String, s, false);
                throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, s));
            }
            this.SetToken(JsonToken.Date, dateTimeOffset, false);
            return(new DateTimeOffset?(dateTimeOffset));
        }
示例#3
0
        public virtual DateTime?ReadAsDateTime()
        {
            DateTime? nullable;
            JsonToken contentToken = this.GetContentToken();

            if (contentToken > JsonToken.String)
            {
                if (contentToken == JsonToken.Null || contentToken == JsonToken.EndArray)
                {
                    nullable = null;
                    return(nullable);
                }
                if (contentToken == JsonToken.Date)
                {
                    if (this.Value is DateTimeOffset)
                    {
                        DateTimeOffset value = (DateTimeOffset)this.Value;
                        this.SetToken(JsonToken.Date, value.DateTime, false);
                    }
                    return(new DateTime?((DateTime)this.Value));
                }
                throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, this.TokenType));
            }
            else
            {
                if (contentToken == JsonToken.None)
                {
                    nullable = null;
                    return(nullable);
                }
                if (contentToken == JsonToken.String)
                {
                    return(this.ReadDateTimeString((string)this.Value));
                }
                throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, this.TokenType));
            }
            nullable = null;
            return(nullable);
        }
示例#4
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));
        }
示例#5
0
        /// <summary>
        /// Reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="Decimal"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{T}"/> of <see cref="Decimal"/>. This method will return <c>null</c> at the end of an array.</returns>
        public virtual decimal?ReadAsDecimal()
        {
            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 decimal))
                {
                    decimal d;
#if HAVE_BIG_INTEGER
                    if (Value is BigInteger)
                    {
                        d = (decimal)(BigInteger)Value;
                    }
                    else
#endif
                    {
                        d = Convert.ToDecimal(Value, CultureInfo.InvariantCulture);
                    }

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

                return((decimal)Value);

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

            throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
示例#6
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{Decimal}"/>. 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 !(NET20 || NET35 || PORTABLE40 || PORTABLE)
                    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));
        }
示例#7
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="Nullable{Boolean}"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{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 !(NET20 || NET35 || PORTABLE40 || PORTABLE)
                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));
        }
示例#8
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="T:System.Nullable`1" />.
        /// </summary>
        /// <returns>A <see cref="T:System.Nullable`1" />. This method will return <c>null</c> at the end of an array.</returns>
        public virtual DateTime?ReadAsDateTime()
        {
            switch (this.GetContentToken())
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(new DateTime?());

            case JsonToken.String:
                return(this.ReadDateTimeString((string)this.Value));

            case JsonToken.Date:
                if (this.Value is DateTimeOffset)
                {
                    this.SetToken(JsonToken.Date, (object)((DateTimeOffset)this.Value).DateTime, false);
                }
                return(new DateTime?((DateTime)this.Value));

            default:
                throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)this.TokenType));
            }
        }
示例#9
0
        internal DateTime?ReadDateTimeString(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                this.SetToken(JsonToken.Null, (object)null, false);
                return(new DateTime?());
            }
            DateTime dateTime;

            if (DateTimeUtils.TryParseDateTime(s, this.DateTimeZoneHandling, this._dateFormatString, this.Culture, out dateTime))
            {
                dateTime = DateTimeUtils.EnsureDateTime(dateTime, this.DateTimeZoneHandling);
                this.SetToken(JsonToken.Date, (object)dateTime, false);
                return(new DateTime?(dateTime));
            }
            if (!DateTime.TryParse(s, (IFormatProvider)this.Culture, DateTimeStyles.RoundtripKind, out dateTime))
            {
                throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)s));
            }
            dateTime = DateTimeUtils.EnsureDateTime(dateTime, this.DateTimeZoneHandling);
            this.SetToken(JsonToken.Date, (object)dateTime, false);
            return(new DateTime?(dateTime));
        }
示例#10
0
        // Token: 0x06000C0D RID: 3085 RVA: 0x0004FB48 File Offset: 0x0004DD48
        public virtual DateTimeOffset?ReadAsDateTimeOffset()
        {
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                if (contentToken == JsonToken.None)
                {
                    goto IL_95;
                }
                if (contentToken == JsonToken.String)
                {
                    string s = (string)this.Value;
                    return(this.ReadDateTimeOffsetString(s));
                }
            }
            else
            {
                if (contentToken == JsonToken.Null || contentToken == JsonToken.EndArray)
                {
                    goto IL_95;
                }
                if (contentToken == JsonToken.Date)
                {
                    object value = this.Value;
                    if (value is DateTime)
                    {
                        DateTime dateTime = (DateTime)value;
                        this.SetToken(JsonToken.Date, new DateTimeOffset(dateTime), false);
                    }
                    return(new DateTimeOffset?((DateTimeOffset)this.Value));
                }
            }
            throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
IL_95:
            return(null);
        }
示例#11
0
        internal DateTime?ReadDateTimeString(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                SetToken(JsonToken.Null, null, false);
                return(null);
            }

            if (DateTimeUtils.TryParseDateTime(s, DateTimeZoneHandling, _dateFormatString, Culture, out DateTime dt))
            {
                dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
                SetToken(JsonToken.Date, dt, false);
                return(dt);
            }

            if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
            {
                dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
                SetToken(JsonToken.Date, dt, false);
                return(dt);
            }

            throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, s));
        }
示例#12
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="Nullable{DateTime}"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{DateTime}"/>. This method will return <c>null</c> at the end of an array.</returns>
        public virtual DateTime?ReadAsDateTime()
        {
            switch (GetContentToken())
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.Date:
                if (Value is DateTimeOffset)
                {
                    SetToken(JsonToken.Date, ((DateTimeOffset)Value).DateTime, false);
                }

                return((DateTime)Value);

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

            throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }
示例#13
0
 private void Push(JsonContainerType value)
 {
     this.UpdateScopeWithFinishedValue();
     if (this._currentPosition.Type == JsonContainerType.None)
     {
         this._currentPosition = new JsonPosition(value);
         return;
     }
     if (this._stack == null)
     {
         this._stack = new List <JsonPosition>();
     }
     this._stack.Add(this._currentPosition);
     this._currentPosition = new JsonPosition(value);
     if (this._maxDepth.HasValue)
     {
         int?nullable = this._maxDepth;
         if (this.Depth + 1 > nullable.GetValueOrDefault() & nullable.HasValue && !this._hasExceededMaxDepth)
         {
             this._hasExceededMaxDepth = true;
             throw JsonReaderException.Create(this, "The reader's MaxDepth of {0} has been exceeded.".FormatWith(CultureInfo.InvariantCulture, this._maxDepth));
         }
     }
 }
示例#14
0
        protected void SetStateBasedOnCurrent()
        {
            JsonContainerType jsonContainerType = this.Peek();

            switch (jsonContainerType)
            {
            case JsonContainerType.None:
                {
                    this.SetFinished();
                    return;
                }

            case JsonContainerType.Object:
            {
                this._currentState = JsonReader.State.Object;
                return;
            }

            case JsonContainerType.Array:
            {
                this._currentState = JsonReader.State.Array;
                return;
            }

            case JsonContainerType.Constructor:
            {
                this._currentState = JsonReader.State.Constructor;
                return;
            }

            default:
            {
                throw JsonReaderException.Create(this, "While setting the reader state back to current object an unexpected JsonType was encountered: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContainerType));
            }
            }
        }
示例#15
0
        public virtual decimal?ReadAsDecimal()
        {
            JsonToken contentToken = this.GetContentToken();

            switch (contentToken)
            {
            case JsonToken.Integer:
            case JsonToken.Float:
                if (!(this.Value is decimal))
                {
                    this.SetToken(JsonToken.Float, Convert.ToDecimal(this.Value, CultureInfo.InvariantCulture), false);
                }
                return(new decimal?((decimal)this.Value));

            case JsonToken.String:
                return(this.ReadDecimalString((string)this.Value));

            case JsonToken.Null:
            case JsonToken.EndArray:
            case JsonToken.None:
                return(null);
            }
            throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
        }
示例#16
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="T:System.String" />.
        /// </summary>
        /// <returns>A <see cref="T:System.String" />. This method will return <c>null</c> at the end of an array.</returns>
        public virtual string ReadAsString()
        {
            JsonToken contentToken = this.GetContentToken();

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

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

            default:
                if (!JsonTokenUtils.IsPrimitiveToken(contentToken) || this.Value == null)
                {
                    throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)contentToken));
                }
                string str = !(this.Value is IFormattable) ? ((object)(this.Value as Uri) == null ? this.Value.ToString() : ((Uri)this.Value).OriginalString) : ((IFormattable)this.Value).ToString((string)null, (IFormatProvider)this.Culture);
                this.SetToken(JsonToken.String, (object)str, false);
                return(str);
            }
        }
示例#17
0
        public virtual double?ReadAsDouble()
        {
            double    num;
            JsonToken contentToken = this.GetContentToken();

            switch (contentToken)
            {
            case JsonToken.Integer:
            case JsonToken.Float:
                if (this.Value is double)
                {
                    goto Label_009A;
                }
                if (!(this.Value is BigInteger))
                {
                    num = Convert.ToDouble(this.Value, CultureInfo.InvariantCulture);
                    break;
                }
                num = (double)((BigInteger)this.Value);
                break;

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

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

            default:
                throw JsonReaderException.Create(this, "Error reading double. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
            }
            this.SetToken(JsonToken.Float, num, false);
Label_009A:
            return(new double?((double)this.Value));
        }
示例#18
0
 internal static JsonReaderException Create(JsonReader reader, string message, Exception ex)
 {
     return(JsonReaderException.Create(reader as IJsonLineInfo, reader.Path, message, ex));
 }
示例#19
0
 internal static JsonReaderException Create(JsonReader reader, string message)
 {
     return(JsonReaderException.Create(reader, message, (Exception)null));
 }
示例#20
0
 internal JsonReaderException CreateUnexpectedEndException()
 {
     return(JsonReaderException.Create(this, "Unexpected end when reading JSON."));
 }
示例#21
0
        public virtual decimal?ReadAsDecimal()
        {
            decimal   num;
            JsonToken contentToken = this.GetContentToken();

            if (contentToken != JsonToken.None)
            {
                switch (contentToken)
                {
                case JsonToken.Integer:
                case JsonToken.Float:
                {
                    object value = this.Value;
                    object obj   = value;
                    object obj1  = obj;
                    if (obj is decimal)
                    {
                        num = (decimal)obj1;
                        return(new decimal?(num));
                    }
                    object obj2 = value;
                    obj1 = obj2;
                    if (!(obj2 is BigInteger))
                    {
                        try
                        {
                            num = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            throw JsonReaderException.Create(this, "Could not convert to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, value), exception);
                        }
                    }
                    else
                    {
                        num = (decimal)((BigInteger)obj1);
                    }
                    this.SetToken(JsonToken.Float, num, false);
                    return(new decimal?(num));
                }

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

                case JsonToken.Boolean:
                case JsonToken.Undefined:
                case JsonToken.EndObject:
                {
                    throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
                }

                case JsonToken.Null:
                case JsonToken.EndArray:
                {
                    break;
                }

                default:
                {
                    throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
                }
                }
            }
            return(null);
        }
示例#22
0
        // Token: 0x06000A9C RID: 2716 RVA: 0x0003F848 File Offset: 0x0003DA48
        public virtual byte[] ReadAsBytes()
        {
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                switch (contentToken)
                {
                case JsonToken.None:
                    break;

                case JsonToken.StartObject:
                {
                    this.ReadIntoWrappedTypeObject();
                    byte[] array = this.ReadAsBytes();
                    this.ReaderReadAndAssert();
                    if (this.TokenType != JsonToken.EndObject)
                    {
                        throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, this.TokenType));
                    }
                    this.SetToken(JsonToken.Bytes, array, false);
                    return(array);
                }

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

                default:
                {
                    if (contentToken != JsonToken.String)
                    {
                        goto IL_130;
                    }
                    string text = (string)this.Value;
                    byte[] array2;
                    Guid   guid;
                    if (text.Length == 0)
                    {
                        array2 = CollectionUtils.ArrayEmpty <byte>();
                    }
                    else if (ConvertUtils.TryConvertGuid(text, out guid))
                    {
                        array2 = guid.ToByteArray();
                    }
                    else
                    {
                        array2 = Convert.FromBase64String(text);
                    }
                    this.SetToken(JsonToken.Bytes, array2, false);
                    return(array2);
                }
                }
            }
            else if (contentToken != JsonToken.Null && contentToken != JsonToken.EndArray)
            {
                if (contentToken != JsonToken.Bytes)
                {
                    goto IL_130;
                }
                object value = this.Value;
                if (value is Guid)
                {
                    byte[] array3 = ((Guid)value).ToByteArray();
                    this.SetToken(JsonToken.Bytes, array3, false);
                    return(array3);
                }
                return((byte[])this.Value);
            }
            return(null);

IL_130:
            throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
        }
示例#23
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="Byte"/>[].
        /// </summary>
        /// <returns>A <see cref="Byte"/>[] or a null reference 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();

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

            if (TokenType == 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);
            }

            switch (t)
            {
            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 = new byte[0];
                }
                else if (ConvertUtils.TryConvertGuid(s, out g))
                {
                    data = g.ToByteArray();
                }
                else
                {
                    data = Convert.FromBase64String(s);
                }

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

            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));
        }
示例#24
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), false);
                }

                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, false);
                    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));
        }
示例#25
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), false);
                }

                return((decimal)Value);
            }

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

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

                decimal d;
                if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
                {
                    SetToken(JsonToken.Float, d, false);
                    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));
        }
示例#26
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, false);
                return(data);
            }

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

                byte[] data;

                Guid g;
                if (s.Length == 0)
                {
                    data = new byte[0];
                }
                else if (ConvertUtils.TryConvertGuid(s, out g))
                {
                    data = g.ToByteArray();
                }
                else
                {
                    data = Convert.FromBase64String(s);
                }

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

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

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

                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, false);
                        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));
        }
示例#27
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), false);
                }

                return((DateTimeOffset)Value);
            }

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

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

                DateTimeOffset dt;
                if (DateTimeUtils.TryParseDateTimeOffset(s, _dateFormatString, Culture, out dt))
                {
                    SetToken(JsonToken.Date, dt, false);
                    return(dt);
                }

                if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
                {
                    SetToken(JsonToken.Date, dt, false);
                    return(dt);
                }

                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));
        }
示例#28
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));
    }
示例#29
0
        internal byte[] ReadAsBytesInternal()
        {
            this._readType = ReadType.ReadAsBytes;
            while (this.ReadInternal())
            {
                JsonToken tokenType = this.TokenType;
                if (tokenType != JsonToken.Comment)
                {
                    if (this.IsWrappedInTypeObject())
                    {
                        byte[] array = this.ReadAsBytes();
                        this.ReadInternal();
                        this.SetToken(JsonToken.Bytes, array);
                        return(array);
                    }
                    if (tokenType == JsonToken.String)
                    {
                        string text   = (string)this.Value;
                        byte[] array2 = (text.Length == 0) ? new byte[0] : Convert.FromBase64String(text);
                        this.SetToken(JsonToken.Bytes, array2);
                        return(array2);
                    }
                    if (tokenType == JsonToken.Null)
                    {
                        return(null);
                    }
                    if (tokenType == JsonToken.Bytes)
                    {
                        return((byte[])this.Value);
                    }
                    if (tokenType == JsonToken.StartArray)
                    {
                        List <byte> list = new List <byte>();
                        while (this.ReadInternal())
                        {
                            tokenType = this.TokenType;
                            JsonToken jsonToken = tokenType;
                            switch (jsonToken)
                            {
                            case JsonToken.Comment:
                                continue;

                            case JsonToken.Raw:
                                break;

                            case JsonToken.Integer:
                                list.Add(Convert.ToByte(this.Value, CultureInfo.InvariantCulture));
                                continue;

                            default:
                                if (jsonToken == JsonToken.EndArray)
                                {
                                    byte[] array3 = list.ToArray();
                                    this.SetToken(JsonToken.Bytes, array3);
                                    return(array3);
                                }
                                break;
                            }
                            throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, tokenType));
                        }
                        throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
                    }
                    if (tokenType == JsonToken.EndArray)
                    {
                        return(null);
                    }
                    throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, tokenType));
                }
            }
            this.SetToken(JsonToken.None);
            return(null);
        }
示例#30
0
        internal bool ReadForType(JsonContract contract, bool hasConverter)
        {
            // don't read properties with converters as a specific value
            // the value might be a string which will then get converted which will error if read as date for example
            if (hasConverter)
            {
                return(Read());
            }

            ReadType t = contract?.InternalReadType ?? ReadType.Read;

            switch (t)
            {
            case ReadType.Read:
                return(ReadAndMoveToContent());

            case ReadType.ReadAsInt32:
                ReadAsInt32();
                break;

            case ReadType.ReadAsInt64:
                bool result = ReadAndMoveToContent();
                if (TokenType == JsonToken.Undefined)
                {
                    throw JsonReaderException.Create(this, "An undefined token is not a valid {0}.".FormatWith(CultureInfo.InvariantCulture, contract?.UnderlyingType ?? typeof(long)));
                }
                return(result);

            case ReadType.ReadAsDecimal:
                ReadAsDecimal();
                break;

            case ReadType.ReadAsDouble:
                ReadAsDouble();
                break;

            case ReadType.ReadAsBytes:
                ReadAsBytes();
                break;

            case ReadType.ReadAsBoolean:
                ReadAsBoolean();
                break;

            case ReadType.ReadAsString:
                ReadAsString();
                break;

            case ReadType.ReadAsDateTime:
                ReadAsDateTime();
                break;

#if HAVE_DATE_TIME_OFFSET
            case ReadType.ReadAsDateTimeOffset:
                ReadAsDateTimeOffset();
                break;
#endif
            default:
                throw new ArgumentOutOfRangeException();
            }

            return(TokenType != JsonToken.None);
        }