Inheritance: Newtonsoft.Json.JsonException
Exemple #1
0
        private JsonContainerType GetTypeForCloseToken(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
                return(JsonContainerType.Object);

            case JsonToken.EndArray:
                return(JsonContainerType.Array);

            case JsonToken.EndConstructor:
                return(JsonContainerType.Constructor);

            default:
                throw JsonWriterException.Create(this, "No type for token: " + (object)token, (Exception)null);
            }
        }
Exemple #2
0
        private JsonToken GetCloseTokenForType(JsonContainerType type)
        {
            switch (type)
            {
            case JsonContainerType.Object:
                return(JsonToken.EndObject);

            case JsonContainerType.Array:
                return(JsonToken.EndArray);

            case JsonContainerType.Constructor:
                return(JsonToken.EndConstructor);

            default:
                throw JsonWriterException.Create(this, "No close token for type: " + (object)type, (Exception)null);
            }
        }
Exemple #3
0
        private void WriteEnd(JsonContainerType type)
        {
            switch (type)
            {
            case JsonContainerType.Object:
                this.WriteEndObject();
                return;

            case JsonContainerType.Array:
                this.WriteEndArray();
                return;

            case JsonContainerType.Constructor:
                this.WriteEndConstructor();
                return;
            }
            throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null);
        }
        protected override void WriteEnd(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
                this.textWriter_0.Write("}");
                return;

            case JsonToken.EndArray:
                this.textWriter_0.Write("]");
                return;

            case JsonToken.EndConstructor:
                this.textWriter_0.Write(")");
                return;
            }
            throw JsonWriterException.smethod_1(this, "Invalid JsonToken: " + token, null);
        }
        private void method_7(Enum12 enum12_0)
        {
            switch (enum12_0)
            {
            case Enum12.Object:
                this.WriteEndObject();
                return;

            case Enum12.Array:
                this.WriteEndArray();
                return;

            case Enum12.Constructor:
                this.WriteEndConstructor();
                return;
            }
            throw JsonWriterException.smethod_1(this, "Unexpected type when writing end: " + enum12_0, null);
        }
        protected override void WriteEnd(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
                this._writer.Write('}');
                return;

            case JsonToken.EndArray:
                this._writer.Write(']');
                return;

            case JsonToken.EndConstructor:
                this._writer.Write(')');
                return;
            }
            throw JsonWriterException.Create(this, "Invalid JsonToken: " + token, null);
        }
Exemple #7
0
        internal void AutoComplete(JsonToken tokenBeingWritten)
        {
            if (tokenBeingWritten != JsonToken.StartObject &&
                tokenBeingWritten != JsonToken.StartArray &&
                tokenBeingWritten != JsonToken.StartConstructor)
            {
                UpdateScopeWithFinishedValue();
            }

            // gets new state based on the current state and what is being written
            State newState = StateArray[(int)tokenBeingWritten][(int)_currentState];

            if (newState == State.Error)
            {
                throw JsonWriterException.Create(this, "Token {0} in state {1} would result in an invalid JSON object.".FormatWith(CultureInfo.InvariantCulture, tokenBeingWritten.ToString(), _currentState.ToString()), null);
            }

            if ((_currentState == State.Object || _currentState == State.Array || _currentState == State.Constructor) && tokenBeingWritten != JsonToken.Comment)
            {
                WriteValueDelimiter();
            }
            else if (_currentState == State.Property)
            {
                if (_formatting == Formatting.Indented)
                {
                    WriteIndentSpace();
                }
            }

            if (_formatting == Formatting.Indented)
            {
                WriteState writeState = WriteState;

                // don't indent a property when it is the first token to be written (i.e. at the start)
                if ((tokenBeingWritten == JsonToken.PropertyName && writeState != WriteState.Start) ||
                    writeState == WriteState.Array || writeState == WriteState.Constructor)
                {
                    WriteIndent();
                }
            }

            _currentState = newState;
        }
        /// <summary>Writes the specified end token.</summary>
        /// <param name="token">The end token to write.</param>
        protected override void WriteEnd(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
                this._writer.Write('}');
                break;

            case JsonToken.EndArray:
                this._writer.Write(']');
                break;

            case JsonToken.EndConstructor:
                this._writer.Write(')');
                break;

            default:
                throw JsonWriterException.Create((JsonWriter)this, "Invalid JsonToken: " + (object)token, (Exception)null);
            }
        }
        /// <summary>
        /// Writes the specified end token.
        /// </summary>
        /// <param name="token">The end token to write.</param>
        protected override void WriteEnd(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
                _writer.Write("}");
                break;

            case JsonToken.EndArray:
                _writer.Write("]");
                break;

            case JsonToken.EndConstructor:
                _writer.Write(")");
                break;

            default:
                throw JsonWriterException.Create(this, "Invalid JsonToken: " + token, null);
            }
        }
Exemple #10
0
        private void WriteEnd(JsonContainerType type)
        {
            switch (type)
            {
            case JsonContainerType.Object:
                this.WriteEndObject();
                break;

            case JsonContainerType.Array:
                this.WriteEndArray();
                break;

            case JsonContainerType.Constructor:
                this.WriteEndConstructor();
                break;

            default:
                throw JsonWriterException.Create(this, "Unexpected type when writing end: " + (object)type, (Exception)null);
            }
        }
Exemple #11
0
        private void WriteConstructorDate(JsonReader reader)
        {
            if (!reader.Read())
            {
                throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", (Exception)null);
            }
            if (reader.TokenType != JsonToken.Integer)
            {
                throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + (object)reader.TokenType, (Exception)null);
            }
            DateTime dateTime = JsonConvert.ConvertJavaScriptTicksToDateTime((long)reader.Value);

            if (!reader.Read())
            {
                throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", (Exception)null);
            }
            if (reader.TokenType != JsonToken.EndConstructor)
            {
                throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + (object)reader.TokenType, (Exception)null);
            }
            this.WriteValue(dateTime);
        }
        private void method_6(JsonReader jsonReader_0)
        {
            if (!jsonReader_0.Read())
            {
                throw JsonWriterException.smethod_1(this, "Unexpected end when reading date constructor.", null);
            }
            if (jsonReader_0.JsonToken_0 != JsonToken.Integer)
            {
                throw JsonWriterException.smethod_1(this, "Unexpected token when reading date constructor. Expected Integer, got " + jsonReader_0.JsonToken_0, null);
            }
            long     num  = (long)jsonReader_0.Object_0;
            DateTime time = Class184.smethod_11(num);

            if (!jsonReader_0.Read())
            {
                throw JsonWriterException.smethod_1(this, "Unexpected end when reading date constructor.", null);
            }
            if (jsonReader_0.JsonToken_0 != JsonToken.EndConstructor)
            {
                throw JsonWriterException.smethod_1(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + jsonReader_0.JsonToken_0, null);
            }
            this.WriteValue(time);
        }
Exemple #13
0
        private void WriteConstructorDate(JsonReader reader)
        {
            if (!reader.Read())
            {
                throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null);
            }
            if (reader.TokenType != JsonToken.Integer)
            {
                throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null);
            }
            long     javaScriptTicks = (long)reader.Value;
            DateTime value           = DateTimeUtils.ConvertJavaScriptTicksToDateTime(javaScriptTicks);

            if (!reader.Read())
            {
                throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null);
            }
            if (reader.TokenType != JsonToken.EndConstructor)
            {
                throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null);
            }
            this.WriteValue(value);
        }
        protected override void WriteEnd(JsonToken token)
        {
            switch (token)
            {
            case JsonToken.EndObject:
            {
                this._writer.Write('}');
                return;
            }

            case JsonToken.EndArray:
            {
                this._writer.Write(']');
                return;
            }

            case JsonToken.EndConstructor:
            {
                this._writer.Write(')');
                return;
            }
            }
            throw JsonWriterException.Create(this, string.Concat("Invalid JsonToken: ", token), null);
        }
Exemple #15
0
 internal void AutoComplete(JsonToken tokenBeingWritten)
 {
     JsonWriter.State state = JsonWriter.StateArray[(int)tokenBeingWritten][(int)this._currentState];
     if (state == JsonWriter.State.Error)
     {
         throw JsonWriterException.Create(this, "Token {0} in state {1} would result in an invalid JSON object.".FormatWith(CultureInfo.InvariantCulture, tokenBeingWritten.ToString(), this._currentState.ToString()), null);
     }
     if ((this._currentState == JsonWriter.State.Object || this._currentState == JsonWriter.State.Array || this._currentState == JsonWriter.State.Constructor) && tokenBeingWritten != JsonToken.Comment)
     {
         this.WriteValueDelimiter();
     }
     if (this._formatting == Formatting.Indented)
     {
         if (this._currentState == JsonWriter.State.Property)
         {
             this.WriteIndentSpace();
         }
         if (this._currentState == JsonWriter.State.Array || this._currentState == JsonWriter.State.ArrayStart || this._currentState == JsonWriter.State.Constructor || this._currentState == JsonWriter.State.ConstructorStart || (tokenBeingWritten == JsonToken.PropertyName && this._currentState != JsonWriter.State.Start))
         {
             this.WriteIndent();
         }
     }
     this._currentState = state;
 }
 internal static JsonWriterException Create(JsonWriter writer, string message, Exception ex)
 {
     return(JsonWriterException.Create(writer.ContainerPath, message, ex));
 }
 private static JsonWriterException smethod_4(JsonWriter jsonWriter_0, object object_0)
 {
     return(JsonWriterException.smethod_1(jsonWriter_0, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".smethod_0(CultureInfo.InvariantCulture, object_0.GetType()), null));
 }
Exemple #18
0
        private void AutoCompleteClose(JsonContainerType type)
        {
            int num = 0;

            if (this._currentPosition.Type == type)
            {
                num = 1;
            }
            else
            {
                int num2 = this.Top - 2;
                for (int j = num2; j >= 0; j--)
                {
                    int num4 = num2 - j;
                    if (this._stack[num4].Type == type)
                    {
                        num = j + 2;
                        break;
                    }
                }
            }
            if (num == 0)
            {
                throw JsonWriterException.Create(this, "No token to close.", null);
            }
            for (int i = 0; i < num; i++)
            {
                JsonToken closeTokenForType = this.GetCloseTokenForType(this.Pop());
                if (this._currentState == State.Property)
                {
                    this.WriteNull();
                }
                if (((this._formatting == Newtonsoft.Json.Formatting.Indented) && (this._currentState != State.ObjectStart)) && (this._currentState != State.ArrayStart))
                {
                    this.WriteIndent();
                }
                this.WriteEnd(closeTokenForType);
                JsonContainerType type2 = this.Peek();
                switch (type2)
                {
                case JsonContainerType.None:
                    this._currentState = State.Start;
                    break;

                case JsonContainerType.Object:
                    this._currentState = State.Object;
                    break;

                case JsonContainerType.Array:
                    this._currentState = State.Array;
                    break;

                case JsonContainerType.Constructor:
                    this._currentState = State.Array;
                    break;

                default:
                    throw JsonWriterException.Create(this, "Unknown JsonType: " + type2, null);
                }
            }
        }
        private void method_10(Enum12 enum12_0)
        {
            int num = 0;

            if (this.struct25_0.enum12_0 == enum12_0)
            {
                num = 1;
            }
            else
            {
                int num2 = this.Top - 2;
                for (int j = num2; j >= 0; j--)
                {
                    int num4 = num2 - j;
                    if (this.list_0[num4].enum12_0 == enum12_0)
                    {
                        num = j + 2;
                        break;
                    }
                }
            }
            if (num == 0)
            {
                throw JsonWriterException.smethod_1(this, "No token to close.", null);
            }
            for (int i = 0; i < num; i++)
            {
                JsonToken token = this.method_9(this.method_2());
                if (this.state_2 == State.Property)
                {
                    this.WriteNull();
                }
                if (((this.formatting_0 == Newtonsoft.Json.Formatting.Indented) && (this.state_2 != State.ObjectStart)) && (this.state_2 != State.ArrayStart))
                {
                    this.WriteIndent();
                }
                this.WriteEnd(token);
                Enum12 enum2 = this.method_3();
                switch (enum2)
                {
                case Enum12.None:
                    this.state_2 = State.Start;
                    break;

                case Enum12.Object:
                    this.state_2 = State.Object;
                    break;

                case Enum12.Array:
                    this.state_2 = State.Array;
                    break;

                case Enum12.Constructor:
                    this.state_2 = State.Array;
                    break;

                default:
                    throw JsonWriterException.smethod_1(this, "Unknown JsonType: " + enum2, null);
                }
            }
        }
        /// <summary>
        /// Writes a <see cref="Object"/> value.
        /// An error will raised if the value cannot be written as a single JSON token.
        /// </summary>
        /// <param name="value">The <see cref="Object"/> value to write.</param>
        public virtual void WriteValue(object value)
        {
            if (value == null)
            {
                WriteNull();
                return;
            }
            else if (ConvertUtils.IsConvertible(value))
            {
                IConvertible convertible = ConvertUtils.ToConvertible(value);

                switch (convertible.GetTypeCode())
                {
                case TypeCode.String:
                    WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Char:
                    WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Boolean:
                    WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.SByte:
                    WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int16:
                    WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt16:
                    WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int32:
                    WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Byte:
                    WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt32:
                    WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int64:
                    WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt64:
                    WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Single:
                    WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Double:
                    WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.DateTime:
                    WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Decimal:
                    WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
                    return;

#if !(NETFX_CORE || PORTABLE)
                case TypeCode.DBNull:
                    WriteNull();
                    return;
#endif
                }
            }
#if !PocketPC && !NET20
            else if (value is DateTimeOffset)
            {
                WriteValue((DateTimeOffset)value);
                return;
            }
#endif
            else if (value is byte[])
            {
                WriteValue((byte[])value);
                return;
            }
            else if (value is Guid)
            {
                WriteValue((Guid)value);
                return;
            }
            else if (value is Uri)
            {
                WriteValue((Uri)value);
                return;
            }
            else if (value is TimeSpan)
            {
                WriteValue((TimeSpan)value);
                return;
            }

            throw JsonWriterException.Create(this, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
        }
        private void AutoCompleteClose(JsonContainerType type)
        {
            // write closing symbol and calculate new state
            int levelsToComplete = 0;

            if (_currentPosition.Type == type)
            {
                levelsToComplete = 1;
            }
            else
            {
                int top = Top - 2;
                for (int i = top; i >= 0; i--)
                {
                    int currentLevel = top - i;

                    if (_stack[currentLevel].Type == type)
                    {
                        levelsToComplete = i + 2;
                        break;
                    }
                }
            }

            if (levelsToComplete == 0)
            {
                throw JsonWriterException.Create(this, "No token to close.", null);
            }

            for (int i = 0; i < levelsToComplete; i++)
            {
                JsonToken token = GetCloseTokenForType(Pop());

                if (_currentState == State.Property)
                {
                    WriteNull();
                }

                if (_formatting == Formatting.Indented)
                {
                    if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
                    {
                        WriteIndent();
                    }
                }

                WriteEnd(token);

                JsonContainerType currentLevelType = Peek();

                switch (currentLevelType)
                {
                case JsonContainerType.Object:
                    _currentState = State.Object;
                    break;

                case JsonContainerType.Array:
                    _currentState = State.Array;
                    break;

                case JsonContainerType.Constructor:
                    _currentState = State.Array;
                    break;

                case JsonContainerType.None:
                    _currentState = State.Start;
                    break;

                default:
                    throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null);
                }
            }
        }
Exemple #22
0
 private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value)
 {
     return(JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null));
 }
Exemple #23
0
        public virtual void WriteValue(object value)
        {
            if (value == null)
            {
                this.WriteNull();
            }
            else
            {
                if (ConvertUtils.IsConvertible(value))
                {
                    IConvertible convertible = ConvertUtils.ToConvertible(value);
                    switch (convertible.GetTypeCode())
                    {
                    case TypeCode.DBNull:
                        this.WriteNull();
                        return;

                    case TypeCode.Boolean:
                        this.WriteValue(convertible.ToBoolean((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Char:
                        this.WriteValue(convertible.ToChar((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.SByte:
                        this.WriteValue(convertible.ToSByte((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Byte:
                        this.WriteValue(convertible.ToByte((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int16:
                        this.WriteValue(convertible.ToInt16((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt16:
                        this.WriteValue(convertible.ToUInt16((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int32:
                        this.WriteValue(convertible.ToInt32((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt32:
                        this.WriteValue(convertible.ToUInt32((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int64:
                        this.WriteValue(convertible.ToInt64((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt64:
                        this.WriteValue(convertible.ToUInt64((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Single:
                        this.WriteValue(convertible.ToSingle((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Double:
                        this.WriteValue(convertible.ToDouble((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Decimal:
                        this.WriteValue(convertible.ToDecimal((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.DateTime:
                        this.WriteValue(convertible.ToDateTime((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.String:
                        this.WriteValue(convertible.ToString((IFormatProvider)CultureInfo.InvariantCulture));
                        return;
                    }
                }
                else if (value is byte[])
                {
                    this.WriteValue((byte[])value);
                    return;
                }
                else if (value is Guid)
                {
                    this.WriteValue((Guid)value);
                    return;
                }
                else if (value is Uri)
                {
                    this.WriteValue((Uri)value);
                    return;
                }
                else if (value is TimeSpan)
                {
                    this.WriteValue((TimeSpan)value);
                    return;
                }
                throw JsonWriterException.Create(this, StringUtils.FormatWith("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.", (IFormatProvider)CultureInfo.InvariantCulture, (object)value.GetType()), (Exception)null);
            }
        }
Exemple #24
0
        private void AutoCompleteClose(JsonContainerType type)
        {
            int num = 0;

            if (this._currentPosition.Type == type)
            {
                num = 1;
            }
            else
            {
                int num2 = this.Top - 2;
                for (int i = num2; i >= 0; i--)
                {
                    int index = num2 - i;
                    if (this._stack[index].Type == type)
                    {
                        num = i + 2;
                        break;
                    }
                }
            }
            if (num == 0)
            {
                throw JsonWriterException.Create(this, "No token to close.", null);
            }
            for (int j = 0; j < num; j++)
            {
                JsonToken closeTokenForType = this.GetCloseTokenForType(this.Pop());
                if (this._currentState == JsonWriter.State.Property)
                {
                    this.WriteNull();
                }
                if (this._formatting == Formatting.Indented && this._currentState != JsonWriter.State.ObjectStart && this._currentState != JsonWriter.State.ArrayStart)
                {
                    this.WriteIndent();
                }
                this.WriteEnd(closeTokenForType);
                JsonContainerType jsonContainerType = this.Peek();
                switch (jsonContainerType)
                {
                case JsonContainerType.None:
                    this._currentState = JsonWriter.State.Start;
                    break;

                case JsonContainerType.Object:
                    this._currentState = JsonWriter.State.Object;
                    break;

                case JsonContainerType.Array:
                    this._currentState = JsonWriter.State.Array;
                    break;

                case JsonContainerType.Constructor:
                    this._currentState = JsonWriter.State.Array;
                    break;

                default:
                    throw JsonWriterException.Create(this, "Unknown JsonType: " + jsonContainerType, null);
                }
            }
        }