#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections.Generic; using System.IO; #if HAVE_BIG_INTEGER using System.Numerics; #endif using LC.Newtonsoft.Json.Utilities; using System.Globalization; #if !HAVE_LINQ using LC.Newtonsoft.Json.Utilities.LinqBridge; #else using System.Linq; #endif namespace LC.Newtonsoft.Json { /// /// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data. /// public abstract partial class JsonWriter : IDisposable { internal enum State { Start = 0, Property = 1, ObjectStart = 2, Object = 3, ArrayStart = 4, Array = 5, ConstructorStart = 6, Constructor = 7, Closed = 8, Error = 9 } // array that gives a new state based on the current state an the token being written private static readonly State[][] StateArray; internal static readonly State[][] StateArrayTempate = new[] { // Start PropertyName ObjectStart Object ArrayStart Array ConstructorStart Constructor Closed Error // /* None */new[] { State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error }, /* StartObject */new[] { State.ObjectStart, State.ObjectStart, State.Error, State.Error, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.Error, State.Error }, /* StartArray */new[] { State.ArrayStart, State.ArrayStart, State.Error, State.Error, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.Error, State.Error }, /* StartConstructor */new[] { State.ConstructorStart, State.ConstructorStart, State.Error, State.Error, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.Error, State.Error }, /* Property */new[] { State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error }, /* Comment */new[] { State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Raw */new[] { State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Value (this will be copied) */new[] { State.Start, State.Object, State.Error, State.Error, State.Array, State.Array, State.Constructor, State.Constructor, State.Error, State.Error } }; internal static State[][] BuildStateArray() { List allStates = StateArrayTempate.ToList(); State[] errorStates = StateArrayTempate[0]; State[] valueStates = StateArrayTempate[7]; EnumInfo enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(typeof(JsonToken)); foreach (ulong valueToken in enumValuesAndNames.Values) { if (allStates.Count <= (int)valueToken) { JsonToken token = (JsonToken)valueToken; switch (token) { case JsonToken.Integer: case JsonToken.Float: case JsonToken.String: case JsonToken.Boolean: case JsonToken.Null: case JsonToken.Undefined: case JsonToken.Date: case JsonToken.Bytes: allStates.Add(valueStates); break; default: allStates.Add(errorStates); break; } } } return allStates.ToArray(); } static JsonWriter() { StateArray = BuildStateArray(); } private List? _stack; private JsonPosition _currentPosition; private State _currentState; private Formatting _formatting; /// /// Gets or sets a value indicating whether the destination should be closed when this writer is closed. /// /// /// true to close the destination when this writer is closed; otherwise false. The default is true. /// public bool CloseOutput { get; set; } /// /// Gets or sets a value indicating whether the JSON should be auto-completed when this writer is closed. /// /// /// true to auto-complete the JSON when this writer is closed; otherwise false. The default is true. /// public bool AutoCompleteOnClose { get; set; } /// /// Gets the top. /// /// The top. protected internal int Top { get { int depth = _stack?.Count ?? 0; if (Peek() != JsonContainerType.None) { depth++; } return depth; } } /// /// Gets the state of the writer. /// public WriteState WriteState { get { switch (_currentState) { case State.Error: return WriteState.Error; case State.Closed: return WriteState.Closed; case State.Object: case State.ObjectStart: return WriteState.Object; case State.Array: case State.ArrayStart: return WriteState.Array; case State.Constructor: case State.ConstructorStart: return WriteState.Constructor; case State.Property: return WriteState.Property; case State.Start: return WriteState.Start; default: throw JsonWriterException.Create(this, "Invalid state: " + _currentState, null); } } } internal string ContainerPath { get { if (_currentPosition.Type == JsonContainerType.None || _stack == null) { return string.Empty; } return JsonPosition.BuildPath(_stack, null); } } /// /// Gets the path of the writer. /// public string Path { get { if (_currentPosition.Type == JsonContainerType.None) { return string.Empty; } bool insideContainer = (_currentState != State.ArrayStart && _currentState != State.ConstructorStart && _currentState != State.ObjectStart); JsonPosition? current = insideContainer ? (JsonPosition?)_currentPosition : null; return JsonPosition.BuildPath(_stack!, current); } } private DateFormatHandling _dateFormatHandling; private DateTimeZoneHandling _dateTimeZoneHandling; private StringEscapeHandling _stringEscapeHandling; private FloatFormatHandling _floatFormatHandling; private string? _dateFormatString; private CultureInfo? _culture; /// /// Gets or sets a value indicating how JSON text output should be formatted. /// public Formatting Formatting { get => _formatting; set { if (value < Formatting.None || value > Formatting.Indented) { throw new ArgumentOutOfRangeException(nameof(value)); } _formatting = value; } } /// /// Gets or sets how dates are written to JSON text. /// public DateFormatHandling DateFormatHandling { get => _dateFormatHandling; set { if (value < DateFormatHandling.IsoDateFormat || value > DateFormatHandling.MicrosoftDateFormat) { throw new ArgumentOutOfRangeException(nameof(value)); } _dateFormatHandling = value; } } /// /// Gets or sets how time zones are handled when writing JSON text. /// public DateTimeZoneHandling DateTimeZoneHandling { get => _dateTimeZoneHandling; set { if (value < DateTimeZoneHandling.Local || value > DateTimeZoneHandling.RoundtripKind) { throw new ArgumentOutOfRangeException(nameof(value)); } _dateTimeZoneHandling = value; } } /// /// Gets or sets how strings are escaped when writing JSON text. /// public StringEscapeHandling StringEscapeHandling { get => _stringEscapeHandling; set { if (value < StringEscapeHandling.Default || value > StringEscapeHandling.EscapeHtml) { throw new ArgumentOutOfRangeException(nameof(value)); } _stringEscapeHandling = value; OnStringEscapeHandlingChanged(); } } internal virtual void OnStringEscapeHandlingChanged() { // hacky but there is a calculated value that relies on StringEscapeHandling } /// /// Gets or sets how special floating point numbers, e.g. , /// and , /// are written to JSON text. /// public FloatFormatHandling FloatFormatHandling { get => _floatFormatHandling; set { if (value < FloatFormatHandling.String || value > FloatFormatHandling.DefaultValue) { throw new ArgumentOutOfRangeException(nameof(value)); } _floatFormatHandling = value; } } /// /// Gets or sets how and values are formatted when writing JSON text. /// public string? DateFormatString { get => _dateFormatString; set => _dateFormatString = value; } /// /// Gets or sets the culture used when writing JSON. Defaults to . /// public CultureInfo Culture { get => _culture ?? CultureInfo.InvariantCulture; set => _culture = value; } /// /// Initializes a new instance of the class. /// protected JsonWriter() { _currentState = State.Start; _formatting = Formatting.None; _dateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind; CloseOutput = true; AutoCompleteOnClose = true; } internal void UpdateScopeWithFinishedValue() { if (_currentPosition.HasIndex) { _currentPosition.Position++; } } private void Push(JsonContainerType value) { if (_currentPosition.Type != JsonContainerType.None) { if (_stack == null) { _stack = new List(); } _stack.Add(_currentPosition); } _currentPosition = new JsonPosition(value); } private JsonContainerType Pop() { JsonPosition oldPosition = _currentPosition; if (_stack != null && _stack.Count > 0) { _currentPosition = _stack[_stack.Count - 1]; _stack.RemoveAt(_stack.Count - 1); } else { _currentPosition = new JsonPosition(); } return oldPosition.Type; } private JsonContainerType Peek() { return _currentPosition.Type; } /// /// Flushes whatever is in the buffer to the destination and also flushes the destination. /// public abstract void Flush(); /// /// Closes this writer. /// If is set to true, the destination is also closed. /// If is set to true, the JSON is auto-completed. /// public virtual void Close() { if (AutoCompleteOnClose) { AutoCompleteAll(); } } /// /// Writes the beginning of a JSON object. /// public virtual void WriteStartObject() { InternalWriteStart(JsonToken.StartObject, JsonContainerType.Object); } /// /// Writes the end of a JSON object. /// public virtual void WriteEndObject() { InternalWriteEnd(JsonContainerType.Object); } /// /// Writes the beginning of a JSON array. /// public virtual void WriteStartArray() { InternalWriteStart(JsonToken.StartArray, JsonContainerType.Array); } /// /// Writes the end of an array. /// public virtual void WriteEndArray() { InternalWriteEnd(JsonContainerType.Array); } /// /// Writes the start of a constructor with the given name. /// /// The name of the constructor. public virtual void WriteStartConstructor(string name) { InternalWriteStart(JsonToken.StartConstructor, JsonContainerType.Constructor); } /// /// Writes the end constructor. /// public virtual void WriteEndConstructor() { InternalWriteEnd(JsonContainerType.Constructor); } /// /// Writes the property name of a name/value pair of a JSON object. /// /// The name of the property. public virtual void WritePropertyName(string name) { InternalWritePropertyName(name); } /// /// Writes the property name of a name/value pair of a JSON object. /// /// The name of the property. /// A flag to indicate whether the text should be escaped when it is written as a JSON property name. public virtual void WritePropertyName(string name, bool escape) { WritePropertyName(name); } /// /// Writes the end of the current JSON object or array. /// public virtual void WriteEnd() { WriteEnd(Peek()); } /// /// Writes the current token and its children. /// /// The to read the token from. public void WriteToken(JsonReader reader) { WriteToken(reader, true); } /// /// Writes the current token. /// /// The to read the token from. /// A flag indicating whether the current token's children should be written. public void WriteToken(JsonReader reader, bool writeChildren) { ValidationUtils.ArgumentNotNull(reader, nameof(reader)); WriteToken(reader, writeChildren, true, true); } /// /// Writes the token and its value. /// /// The to write. /// /// The value to write. /// A value is only required for tokens that have an associated value, e.g. the property name for . /// null can be passed to the method for tokens that don't have a value, e.g. . /// public void WriteToken(JsonToken token, object? value) { switch (token) { case JsonToken.None: // read to next break; case JsonToken.StartObject: WriteStartObject(); break; case JsonToken.StartArray: WriteStartArray(); break; case JsonToken.StartConstructor: ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteStartConstructor(value.ToString()); break; case JsonToken.PropertyName: ValidationUtils.ArgumentNotNull(value, nameof(value)); WritePropertyName(value.ToString()); break; case JsonToken.Comment: WriteComment(value?.ToString()); break; case JsonToken.Integer: ValidationUtils.ArgumentNotNull(value, nameof(value)); #if HAVE_BIG_INTEGER if (value is BigInteger integer) { WriteValue(integer); } else #endif { WriteValue(Convert.ToInt64(value, CultureInfo.InvariantCulture)); } break; case JsonToken.Float: ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value is decimal decimalValue) { WriteValue(decimalValue); } else if (value is double doubleValue) { WriteValue(doubleValue); } else if (value is float floatValue) { WriteValue(floatValue); } else { WriteValue(Convert.ToDouble(value, CultureInfo.InvariantCulture)); } break; case JsonToken.String: ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteValue(value.ToString()); break; case JsonToken.Boolean: ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteValue(Convert.ToBoolean(value, CultureInfo.InvariantCulture)); break; case JsonToken.Null: WriteNull(); break; case JsonToken.Undefined: WriteUndefined(); break; case JsonToken.EndObject: WriteEndObject(); break; case JsonToken.EndArray: WriteEndArray(); break; case JsonToken.EndConstructor: WriteEndConstructor(); break; case JsonToken.Date: ValidationUtils.ArgumentNotNull(value, nameof(value)); #if HAVE_DATE_TIME_OFFSET if (value is DateTimeOffset dt) { WriteValue(dt); } else #endif { WriteValue(Convert.ToDateTime(value, CultureInfo.InvariantCulture)); } break; case JsonToken.Raw: WriteRawValue(value?.ToString()); break; case JsonToken.Bytes: ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value is Guid guid) { WriteValue(guid); } else { WriteValue((byte[])value!); } break; default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(token), token, "Unexpected token type."); } } /// /// Writes the token. /// /// The to write. public void WriteToken(JsonToken token) { WriteToken(token, null); } internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments) { int initialDepth = CalculateWriteTokenInitialDepth(reader); do { // write a JValue date when the constructor is for a date if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value?.ToString(), "Date", StringComparison.Ordinal)) { WriteConstructorDate(reader); } else { if (writeComments || reader.TokenType != JsonToken.Comment) { WriteToken(reader.TokenType, reader.Value); } } } while ( // stop if we have reached the end of the token being read initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0) && writeChildren && reader.Read()); if (IsWriteTokenIncomplete(reader, writeChildren, initialDepth)) { throw JsonWriterException.Create(this, "Unexpected end when reading token.", null); } } private bool IsWriteTokenIncomplete(JsonReader reader, bool writeChildren, int initialDepth) { int finalDepth = CalculateWriteTokenFinalDepth(reader); return initialDepth < finalDepth || (writeChildren && initialDepth == finalDepth && JsonTokenUtils.IsStartToken(reader.TokenType)); } private int CalculateWriteTokenInitialDepth(JsonReader reader) { JsonToken type = reader.TokenType; if (type == JsonToken.None) { return -1; } return JsonTokenUtils.IsStartToken(type) ? reader.Depth : reader.Depth + 1; } private int CalculateWriteTokenFinalDepth(JsonReader reader) { JsonToken type = reader.TokenType; if (type == JsonToken.None) { return -1; } return JsonTokenUtils.IsEndToken(type) ? reader.Depth - 1 : reader.Depth; } private void WriteConstructorDate(JsonReader reader) { if (!JavaScriptUtils.TryGetDateFromConstructorJson(reader, out DateTime dateTime, out string? errorMessage)) { throw JsonWriterException.Create(this, errorMessage, null); } WriteValue(dateTime); } 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); } } private void AutoCompleteAll() { while (Top > 0) { WriteEnd(); } } 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); } } private void AutoCompleteClose(JsonContainerType type) { int levelsToComplete = CalculateLevelsToComplete(type); 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); UpdateCurrentState(); } } private int CalculateLevelsToComplete(JsonContainerType type) { 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); } return levelsToComplete; } private void UpdateCurrentState() { 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); } } /// /// Writes the specified end token. /// /// The end token to write. protected virtual void WriteEnd(JsonToken token) { } /// /// Writes indent characters. /// protected virtual void WriteIndent() { } /// /// Writes the JSON value delimiter. /// protected virtual void WriteValueDelimiter() { } /// /// Writes an indent space. /// protected virtual void WriteIndentSpace() { } 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(); } if (_formatting == Formatting.Indented) { if (_currentState == State.Property) { WriteIndentSpace(); } // don't indent a property when it is the first token to be written (i.e. at the start) if ((_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.Constructor || _currentState == State.ConstructorStart) || (tokenBeingWritten == JsonToken.PropertyName && _currentState != State.Start)) { WriteIndent(); } } _currentState = newState; } #region WriteValue methods /// /// Writes a null value. /// public virtual void WriteNull() { InternalWriteValue(JsonToken.Null); } /// /// Writes an undefined value. /// public virtual void WriteUndefined() { InternalWriteValue(JsonToken.Undefined); } /// /// Writes raw JSON without changing the writer's state. /// /// The raw JSON to write. public virtual void WriteRaw(string? json) { InternalWriteRaw(); } /// /// Writes raw JSON where a value is expected and updates the writer's state. /// /// The raw JSON to write. public virtual void WriteRawValue(string? json) { // hack. want writer to change state as if a value had been written UpdateScopeWithFinishedValue(); AutoComplete(JsonToken.Undefined); WriteRaw(json); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(string? value) { InternalWriteValue(JsonToken.String); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(int value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. [CLSCompliant(false)] public virtual void WriteValue(uint value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(long value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. [CLSCompliant(false)] public virtual void WriteValue(ulong value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(float value) { InternalWriteValue(JsonToken.Float); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(double value) { InternalWriteValue(JsonToken.Float); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(bool value) { InternalWriteValue(JsonToken.Boolean); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(short value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. [CLSCompliant(false)] public virtual void WriteValue(ushort value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(char value) { InternalWriteValue(JsonToken.String); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(byte value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. [CLSCompliant(false)] public virtual void WriteValue(sbyte value) { InternalWriteValue(JsonToken.Integer); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(decimal value) { InternalWriteValue(JsonToken.Float); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(DateTime value) { InternalWriteValue(JsonToken.Date); } #if HAVE_DATE_TIME_OFFSET /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(DateTimeOffset value) { InternalWriteValue(JsonToken.Date); } #endif /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(Guid value) { InternalWriteValue(JsonToken.String); } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(TimeSpan value) { InternalWriteValue(JsonToken.String); } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(int? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. [CLSCompliant(false)] public virtual void WriteValue(uint? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(long? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. [CLSCompliant(false)] public virtual void WriteValue(ulong? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(float? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(double? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(bool? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(short? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. [CLSCompliant(false)] public virtual void WriteValue(ushort? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(char? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(byte? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. [CLSCompliant(false)] public virtual void WriteValue(sbyte? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(decimal? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(DateTime? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } #if HAVE_DATE_TIME_OFFSET /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(DateTimeOffset? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } #endif /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(Guid? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a of value. /// /// The of value to write. public virtual void WriteValue(TimeSpan? value) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault()); } } /// /// Writes a [] value. /// /// The [] value to write. public virtual void WriteValue(byte[]? value) { if (value == null) { WriteNull(); } else { InternalWriteValue(JsonToken.Bytes); } } /// /// Writes a value. /// /// The value to write. public virtual void WriteValue(Uri? value) { if (value == null) { WriteNull(); } else { InternalWriteValue(JsonToken.String); } } /// /// Writes a value. /// An error will raised if the value cannot be written as a single JSON token. /// /// The value to write. public virtual void WriteValue(object? value) { if (value == null) { WriteNull(); } else { #if HAVE_BIG_INTEGER // this is here because adding a WriteValue(BigInteger) to JsonWriter will // mean the user has to add a reference to System.Numerics.dll if (value is BigInteger) { throw CreateUnsupportedTypeException(this, value); } #endif WriteValue(this, ConvertUtils.GetTypeCode(value.GetType()), value); } } #endregion /// /// Writes a comment /*...*/ containing the specified text. /// /// Text to place inside the comment. public virtual void WriteComment(string? text) { InternalWriteComment(); } /// /// Writes the given white space. /// /// The string of white space characters. public virtual void WriteWhitespace(string ws) { InternalWriteWhitespace(ws); } void IDisposable.Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (_currentState != State.Closed && disposing) { Close(); } } internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value) { while (true) { switch (typeCode) { case PrimitiveTypeCode.Char: writer.WriteValue((char)value); return; case PrimitiveTypeCode.CharNullable: writer.WriteValue((value == null) ? (char?)null : (char)value); return; case PrimitiveTypeCode.Boolean: writer.WriteValue((bool)value); return; case PrimitiveTypeCode.BooleanNullable: writer.WriteValue((value == null) ? (bool?)null : (bool)value); return; case PrimitiveTypeCode.SByte: writer.WriteValue((sbyte)value); return; case PrimitiveTypeCode.SByteNullable: writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value); return; case PrimitiveTypeCode.Int16: writer.WriteValue((short)value); return; case PrimitiveTypeCode.Int16Nullable: writer.WriteValue((value == null) ? (short?)null : (short)value); return; case PrimitiveTypeCode.UInt16: writer.WriteValue((ushort)value); return; case PrimitiveTypeCode.UInt16Nullable: writer.WriteValue((value == null) ? (ushort?)null : (ushort)value); return; case PrimitiveTypeCode.Int32: writer.WriteValue((int)value); return; case PrimitiveTypeCode.Int32Nullable: writer.WriteValue((value == null) ? (int?)null : (int)value); return; case PrimitiveTypeCode.Byte: writer.WriteValue((byte)value); return; case PrimitiveTypeCode.ByteNullable: writer.WriteValue((value == null) ? (byte?)null : (byte)value); return; case PrimitiveTypeCode.UInt32: writer.WriteValue((uint)value); return; case PrimitiveTypeCode.UInt32Nullable: writer.WriteValue((value == null) ? (uint?)null : (uint)value); return; case PrimitiveTypeCode.Int64: writer.WriteValue((long)value); return; case PrimitiveTypeCode.Int64Nullable: writer.WriteValue((value == null) ? (long?)null : (long)value); return; case PrimitiveTypeCode.UInt64: writer.WriteValue((ulong)value); return; case PrimitiveTypeCode.UInt64Nullable: writer.WriteValue((value == null) ? (ulong?)null : (ulong)value); return; case PrimitiveTypeCode.Single: writer.WriteValue((float)value); return; case PrimitiveTypeCode.SingleNullable: writer.WriteValue((value == null) ? (float?)null : (float)value); return; case PrimitiveTypeCode.Double: writer.WriteValue((double)value); return; case PrimitiveTypeCode.DoubleNullable: writer.WriteValue((value == null) ? (double?)null : (double)value); return; case PrimitiveTypeCode.DateTime: writer.WriteValue((DateTime)value); return; case PrimitiveTypeCode.DateTimeNullable: writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value); return; #if HAVE_DATE_TIME_OFFSET case PrimitiveTypeCode.DateTimeOffset: writer.WriteValue((DateTimeOffset)value); return; case PrimitiveTypeCode.DateTimeOffsetNullable: writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value); return; #endif case PrimitiveTypeCode.Decimal: writer.WriteValue((decimal)value); return; case PrimitiveTypeCode.DecimalNullable: writer.WriteValue((value == null) ? (decimal?)null : (decimal)value); return; case PrimitiveTypeCode.Guid: writer.WriteValue((Guid)value); return; case PrimitiveTypeCode.GuidNullable: writer.WriteValue((value == null) ? (Guid?)null : (Guid)value); return; case PrimitiveTypeCode.TimeSpan: writer.WriteValue((TimeSpan)value); return; case PrimitiveTypeCode.TimeSpanNullable: writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value); return; #if HAVE_BIG_INTEGER case PrimitiveTypeCode.BigInteger: // this will call to WriteValue(object) writer.WriteValue((BigInteger)value); return; case PrimitiveTypeCode.BigIntegerNullable: // this will call to WriteValue(object) writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value); return; #endif case PrimitiveTypeCode.Uri: writer.WriteValue((Uri)value); return; case PrimitiveTypeCode.String: writer.WriteValue((string)value); return; case PrimitiveTypeCode.Bytes: writer.WriteValue((byte[])value); return; #if HAVE_DB_NULL_TYPE_CODE case PrimitiveTypeCode.DBNull: writer.WriteNull(); return; #endif default: #if HAVE_ICONVERTIBLE if (value is IConvertible convertible) { ResolveConvertibleValue(convertible, out typeCode, out value); continue; } #endif // write an unknown null value, fix https://github.com/JamesNK/Newtonsoft.Json/issues/1460 if (value == null) { writer.WriteNull(); return; } throw CreateUnsupportedTypeException(writer, value); } } } #if HAVE_ICONVERTIBLE private static void ResolveConvertibleValue(IConvertible convertible, out PrimitiveTypeCode typeCode, out object value) { // the value is a non-standard IConvertible // convert to the underlying value and retry TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible); // if convertible has an underlying typecode of Object then attempt to convert it to a string typeCode = typeInformation.TypeCode == PrimitiveTypeCode.Object ? PrimitiveTypeCode.String : typeInformation.TypeCode; Type resolvedType = typeInformation.TypeCode == PrimitiveTypeCode.Object ? typeof(string) : typeInformation.Type; value = convertible.ToType(resolvedType, CultureInfo.InvariantCulture); } #endif 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); } /// /// Sets the state of the . /// /// The being written. /// The value being written. protected void SetWriteState(JsonToken token, object value) { switch (token) { case JsonToken.StartObject: InternalWriteStart(token, JsonContainerType.Object); break; case JsonToken.StartArray: InternalWriteStart(token, JsonContainerType.Array); break; case JsonToken.StartConstructor: InternalWriteStart(token, JsonContainerType.Constructor); break; case JsonToken.PropertyName: if (!(value is string s)) { throw new ArgumentException("A name is required when setting property name state.", nameof(value)); } InternalWritePropertyName(s); break; case JsonToken.Comment: InternalWriteComment(); break; case JsonToken.Raw: InternalWriteRaw(); break; case JsonToken.Integer: case JsonToken.Float: case JsonToken.String: case JsonToken.Boolean: case JsonToken.Date: case JsonToken.Bytes: case JsonToken.Null: case JsonToken.Undefined: InternalWriteValue(token); break; case JsonToken.EndObject: InternalWriteEnd(JsonContainerType.Object); break; case JsonToken.EndArray: InternalWriteEnd(JsonContainerType.Array); break; case JsonToken.EndConstructor: InternalWriteEnd(JsonContainerType.Constructor); break; default: throw new ArgumentOutOfRangeException(nameof(token)); } } internal void InternalWriteEnd(JsonContainerType container) { AutoCompleteClose(container); } internal void InternalWritePropertyName(string name) { _currentPosition.PropertyName = name; AutoComplete(JsonToken.PropertyName); } internal void InternalWriteRaw() { } internal void InternalWriteStart(JsonToken token, JsonContainerType container) { UpdateScopeWithFinishedValue(); AutoComplete(token); Push(container); } internal void InternalWriteValue(JsonToken token) { UpdateScopeWithFinishedValue(); AutoComplete(token); } internal void InternalWriteWhitespace(string ws) { if (ws != null) { if (!StringUtils.IsWhiteSpace(ws)) { throw JsonWriterException.Create(this, "Only white space characters should be used.", null); } } } internal void InternalWriteComment() { AutoComplete(JsonToken.Comment); } } }