コード例 #1
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: " + type, null);
            }
        }
コード例 #2
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: " + token, null);
            }
        }
コード例 #3
0
        /// <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);
            }
        }
コード例 #4
0
        private void WriteEnd(JsonContainerType type)
        {
            switch (type)
            {
            case JsonContainerType.Object:
                WriteEndObject();
                break;

            case JsonContainerType.Array:
                WriteEndArray();
                break;

            case JsonContainerType.Constructor:
                WriteEndConstructor();
                break;

            default:
                throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null);
            }
        }
コード例 #5
0
        internal void AutoComplete(JsonToken tokenBeingWritten)
        {
            // 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;
        }
コード例 #6
0
        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);
                }
            }
        }
コード例 #7
0
        /// <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);
        }