コード例 #1
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);
                }
            }
        }
コード例 #2
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);
        }