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); } } }
/// <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) { 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); } } }
// Token: 0x06000E35 RID: 3637 RVA: 0x00010C2B File Offset: 0x0000EE2B internal static JsonWriterException Create(JsonWriter writer, string message, [Nullable(2)] Exception ex) { return(JsonWriterException.Create(writer.ContainerPath, message, ex)); }
private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value) => JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);