/// <summary> /// Returns the current snapshot of the <see cref="Utf8JsonWriter"/> state which must /// be captured by the caller and passed back in to the <see cref="Utf8JsonWriter"/> ctor with more data. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown when there is JSON data that has been written and buffered but not yet flushed to the <see cref="IBufferWriter{Byte}" />. /// Getting the state for creating a new <see cref="Utf8JsonWriter"/> without first committing the data that has been written /// would result in an inconsistent state. Call Flush before getting the current state. /// </exception> /// <remarks> /// Unlike the <see cref="Utf8JsonWriter"/>, which is a ref struct, the state can survive /// across async/await boundaries and hence this type is required to provide support for reading /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonWriter"/>. /// </remarks> public JsonWriterState GetCurrentState() { if (_buffered != 0) { throw ThrowHelper.GetInvalidOperationException_CallFlushFirst(_buffered); } return(new JsonWriterState { _bytesWritten = BytesWritten, _bytesCommitted = BytesCommitted, _inObject = _inObject, _isNotPrimitive = _isNotPrimitive, _tokenType = _tokenType, _currentDepth = _currentDepth, _writerOptions = _writerOptions, _bitStack = _bitStack, }); }