/// <summary> /// Constructs a new <see cref="Utf8JsonWriter"/> instance with a specified <paramref name="utf8Json"/>. /// </summary> /// <param name="utf8Json">An instance of <see cref="Stream" /> used as a destination for writing JSON text into.</param> /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonWriter"/> /// By default, the <see cref="Utf8JsonWriter"/> writes JSON minimized (i.e. with no extra whitespace) /// and validates that the JSON being written is structurally valid according to JSON RFC.</param> /// <exception cref="ArgumentNullException"> /// Thrown when the instance of <see cref="Stream" /> that is passed in is null. /// </exception> public Utf8JsonWriter(Stream utf8Json, JsonWriterOptions options = default) { if (utf8Json == null) { throw new ArgumentNullException(nameof(utf8Json)); } if (!utf8Json.CanWrite) { throw new ArgumentException(SR.StreamNotWritable); } _stream = utf8Json; _arrayBufferWriter = new ArrayBufferWriter <byte>(); _output = _arrayBufferWriter; BytesPending = default; BytesCommitted = default; _memory = default; _inObject = default; _isNotPrimitive = default; _tokenType = default; _currentDepth = default; Options = options; // Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle. // This way we avoid allocations in the common, default cases, and allocate lazily. _bitStack = default; }
private void ResetHelper() { BytesPending = default; BytesCommitted = default; _memory = default; _inObject = default; _tokenType = default; _currentDepth = default; _bitStack = default; }
private void ResetHelper() { BytesPending = default; BytesCommitted = default; _memory = default; _inObject = default; _tokenType = default; _currentDepth = default; // Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle. // This way we avoid allocations in the common, default cases, and allocate lazily. _bitStack = default; }
/// <summary> /// Constructs a new <see cref="Utf8JsonWriter"/> instance with a specified <paramref name="bufferWriter"/>. /// </summary> /// <param name="bufferWriter">An instance of <see cref="IBufferWriter{Byte}" /> used as a destination for writing JSON text into.</param> /// <param name="state">If this is the first call to the ctor, pass in a default state. Otherwise, /// capture the state from the previous instance of the <see cref="Utf8JsonWriter"/> and pass that back.</param> /// <exception cref="ArgumentNullException"> /// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null. /// </exception> /// <remarks> /// Since this type is a ref struct, it is a stack-only type and all the limitations of ref structs apply to it. /// This is the reason why the ctor accepts a <see cref="JsonWriterState"/>. /// </remarks> public Utf8JsonWriter(IBufferWriter <byte> bufferWriter, JsonWriterState state = default) { _output = bufferWriter ?? throw new ArgumentNullException(nameof(bufferWriter)); _buffered = 0; BytesCommitted = 0; _buffer = _output.GetSpan(); _inObject = state._inObject; _isNotPrimitive = state._isNotPrimitive; _tokenType = state._tokenType; _writerOptions = state._writerOptions; _bitStack = state._bitStack; _currentDepth = state._currentDepth; }
/// <summary> /// Constructs a new <see cref="Utf8JsonWriter"/> instance with a specified <paramref name="bufferWriter"/>. /// </summary> /// <param name="bufferWriter">An instance of <see cref="IBufferWriter{Byte}" /> used as a destination for writing JSON text into.</param> /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonWriter"/> /// By default, the <see cref="Utf8JsonWriter"/> writes JSON minimized (that is, with no extra whitespace) /// and validates that the JSON being written is structurally valid according to JSON RFC.</param> /// <exception cref="ArgumentNullException"> /// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null. /// </exception> public Utf8JsonWriter(IBufferWriter <byte> bufferWriter, JsonWriterOptions options = default) { _output = bufferWriter ?? throw new ArgumentNullException(nameof(bufferWriter)); _stream = default; _arrayBufferWriter = default; BytesPending = default; BytesCommitted = default; _memory = default; _inObject = default; _tokenType = default; _currentDepth = default; _options = options; // Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle. // This way we avoid allocations in the common, default cases, and allocate lazily. _bitStack = default; }