YamlWriter Class
Writes YAML tokens for use by YamlConverter implementations.
public sealed class YamlWriter : YamlReaderWriterBase- Inheritance
YamlReaderWriterBaseYamlWriter
Nested types
| Name | Description |
|---|---|
| YamlWriter.BlockSequenceItemStyleScope | Restores the block sequence item styles that were active before a YamlWriter.PushBlockSequenceItemStyle call. |
| YamlWriter.StringStyleScope | Restores the string style that was active before a YamlWriter.PushStringStyle call. |
Constructors
| Name | Description |
|---|---|
| YamlWriter(TextWriter, YamlSerializerOptions?) | Initializes a new instance of the YamlWriter class. |
| YamlWriter(StringBuilder, YamlSerializerOptions?) | Initializes a new instance of the YamlWriter class that writes directly to a StringBuilder. |
YamlWriter overloads
YamlWriter(TextWriter, YamlSerializerOptions?)
YamlWriter.csInitializes a new instance of the YamlWriter class.
public YamlWriter(TextWriter writer, YamlSerializerOptions? options = null)Parameters
writerTextWriter- The destination writer.
optionsYamlSerializerOptions?- The serializer options used for formatting.
Exceptions
ArgumentNullExceptionwriterisnull.
YamlWriter(StringBuilder, YamlSerializerOptions?)
YamlWriter.csInitializes a new instance of the YamlWriter class that writes directly to a StringBuilder.
public YamlWriter(StringBuilder stringBuilder, YamlSerializerOptions? options = null)Parameters
stringBuilderStringBuilder- The destination string builder.
optionsYamlSerializerOptions?- The serializer options used for formatting.
Exceptions
ArgumentNullExceptionstringBuilderisnull.
Properties
| Name | Description |
|---|---|
| IsCollectingReferences | Gets a value indicating whether this writer only collects the object references of the value being serialized, and discards what it writes. |
IsCollectingReferences
YamlWriter.csGets a value indicating whether this writer only collects the object references of the value being serialized, and discards what it writes.
public bool IsCollectingReferences { get; }Property value
Remarks
With YamlReferenceHandling.PreserveMinimal, a value is written twice: a first pass finds the references that are shared or cyclic, and only those get an anchor when the value is actually written by the second pass. Converters are called in both passes, so a converter with side effects can check this property to run them once.
The IYamlOnSerialized.OnSerialized callback is not invoked by the first pass. See YamlWriter.ShouldInvokeOnSerializing for the IYamlOnSerializing.OnSerializing callback.
Methods
| Name | Description |
|---|---|
| PushBlockSequenceItemStyle(YamlSequenceItemStyle, YamlSequenceItemStyle) | Temporarily overrides how nested block collections are emitted when they appear as items in block sequences. |
| PushStringStyle(ScalarStyle) | Temporarily overrides the style used to write string values. |
| ShouldInvokeOnSerializing(object) | Determines whether the IYamlOnSerializing.OnSerializing callback of value is invoked by this writer, so it runs once when the value is written by the two passes of YamlReferenceHandling.PreserveMinimal. |
| TryWriteReference(object?) | Attempts to preserve object references by writing an alias when value was previously anchored, or by writing an anchor for the next value when it is seen for the first time. |
| WriteAlias(string) | Writes a YAML alias value (a reference to an anchor). |
| WriteAnchor(string) | Writes a YAML anchor for the next node, which is a mapping key when YamlWriter.WritePropertyName is called next. |
| WriteDictionaryKey(object) | Writes a dictionary key the way the built-in dictionary converters write it. |
| WriteEndMapping() | Writes the end of a mapping. |
| WriteEndSequence() | Writes the end of a sequence. |
| WriteNullValue() | Writes a null scalar. |
| WritePropertyName(string) | Writes a mapping key. |
| WriteScalar(bool) | Writes a boolean scalar value. |
| WriteScalar(byte) | Writes an 8-bit unsigned integer scalar value. |
| WriteScalar(char) | Writes a character scalar value. |
| WriteScalar(DateOnly) | Writes a date-only scalar value using the round-trip format. |
| WriteScalar(DateTime) | Writes a date and time scalar value using the round-trip format. |
| WriteScalar(DateTimeOffset) | Writes a date and time with offset scalar value using the round-trip format. |
| WriteScalar(decimal) | Writes a decimal scalar value. |
| WriteScalar(double) | Writes a double-precision floating-point scalar value. |
| WriteScalar(Guid) | Writes a GUID scalar value. |
| WriteScalar(Half) | Writes a half-precision floating-point scalar value. |
| WriteScalar(Int128) | Writes a 128-bit signed integer scalar value. |
| WriteScalar(short) | Writes a 16-bit signed integer scalar value. |
| WriteScalar(int) | Writes a 32-bit signed integer scalar value. |
| WriteScalar(long) | Writes a 64-bit signed integer scalar value. |
| WriteScalar(nint) | Writes a platform-sized signed integer scalar value. |
| WriteScalar(BFloat16) | Writes a bfloat16 floating-point scalar value. |
| WriteScalar(Decimal128) | Writes a 128-bit IEEE 754 decimal floating-point scalar value. |
| WriteScalar(Decimal32) | Writes a 32-bit IEEE 754 decimal floating-point scalar value. |
| WriteScalar(Decimal64) | Writes a 64-bit IEEE 754 decimal floating-point scalar value. |
| WriteScalar(ReadOnlySpan<char>) | Writes a scalar value from a character span. |
| WriteScalar(sbyte) | Writes an 8-bit signed integer scalar value. |
| WriteScalar(float) | Writes a single-precision floating-point scalar value. |
| WriteScalar(string?) | Writes a scalar value. |
| WriteScalar(TimeOnly) | Writes a time-only scalar value using the round-trip format. |
| WriteScalar(TimeSpan) | Writes a time interval scalar value using the constant format. |
| WriteScalar(UInt128) | Writes a 128-bit unsigned integer scalar value. |
| WriteScalar(ushort) | Writes a 16-bit unsigned integer scalar value. |
| WriteScalar(uint) | Writes a 32-bit unsigned integer scalar value. |
| WriteScalar(ulong) | Writes a 64-bit unsigned integer scalar value. |
| WriteScalar(nuint) | Writes a platform-sized unsigned integer scalar value. |
| WriteScalar<T>(T) | Writes a scalar value for a span-formattable type using invariant culture formatting. |
| WriteStartMapping() | Writes the start of a mapping. |
| WriteStartSequence() | Writes the start of a sequence. |
| WriteString(string?) | Writes a CLR string value, quoting ambiguous YAML scalars when configured. |
| WriteTag(string) | Writes a YAML tag for the next node, which is a mapping key when YamlWriter.WritePropertyName is called next. |
PushBlockSequenceItemStyle(YamlSequenceItemStyle, YamlSequenceItemStyle)
YamlWriter.csTemporarily overrides how nested block collections are emitted when they appear as items in block sequences.
public YamlWriter.BlockSequenceItemStyleScope PushBlockSequenceItemStyle(YamlSequenceItemStyle mappingStyle, YamlSequenceItemStyle sequenceStyle)Parameters
mappingStyleYamlSequenceItemStyle- The mapping style override, or
YamlSequenceItemStyle.Defaultto keep the current mapping style. sequenceStyleYamlSequenceItemStyle- The sequence style override, or
YamlSequenceItemStyle.Defaultto keep the current sequence style.
Returns
YamlWriter.BlockSequenceItemStyleScope A scope that restores the previous styles when disposed.
Exceptions
ArgumentOutOfRangeExceptionmappingStyleorsequenceStyleis not a definedYamlSequenceItemStyle.
PushStringStyle(ScalarStyle)
YamlWriter.csTemporarily overrides the style used to write string values.
public YamlWriter.StringStyleScope PushStringStyle(ScalarStyle style)Parameters
styleScalarStyle- The style override, or
ScalarStyle.Anyto keep the current style.
Returns
YamlWriter.StringStyleScope A scope that restores the previous style when disposed.
Exceptions
ArgumentOutOfRangeExceptionstyleis not a definedScalarStyle.
Remarks
The style applies to string values only. It is ignored for a value that cannot be represented in it, and for mapping keys.
ShouldInvokeOnSerializing(object)
YamlWriter.csDetermines whether the IYamlOnSerializing.OnSerializing callback of value is invoked by this writer, so it runs once when the value is written by the two passes of YamlReferenceHandling.PreserveMinimal.
Parameters
valueobject- The value about to be written.
Returns
bool true when the callback must be invoked before writing value; otherwise false.
Exceptions
ArgumentNullExceptionvalueisnull.
Remarks
The callback of an object runs in the first pass, before its references are collected, so the second pass writes the same references. The callback of a value type runs in the second pass: each pass writes its own copy of the value, so a change made to the copy of the first pass would be lost.
This method always returns true for other reference handling modes. It is intended for generated serializers and custom converters that invoke the serialization callbacks themselves. See YamlWriter.IsCollectingReferences.
TryWriteReference(object?)
YamlWriter.csAttempts to preserve object references by writing an alias when value was previously anchored, or by writing an anchor for the next value when it is seen for the first time.
Parameters
valueobject?- The value to track.
Returns
bool true when an alias was written and no further output for this value is required.
Remarks
This method is intended for use by generated serializers and custom converters. It is a no-op unless YamlSerializerOptions.ReferenceHandling is YamlReferenceHandling.Preserve or YamlReferenceHandling.PreserveMinimal.
WriteAlias(string)
YamlWriter.csWriteAnchor(string)
YamlWriter.csWrites a YAML anchor for the next node, which is a mapping key when YamlWriter.WritePropertyName is called next.
Parameters
anchorstring
WriteDictionaryKey(object)
YamlWriter.csWrites a dictionary key the way the built-in dictionary converters write it.
Parameters
keyobject- The key.
Exceptions
YamlExceptionkeyisnull.InvalidOperationException- The writer is not positioned within a mapping key.
Remarks
A string key is converted using YamlSerializerOptions.DictionaryKeyPolicy and is quoted when it would resolve to a null, a boolean, or a number, so it is read back as a string by an untyped reader. An enum key is written with the names of the enum converter, and the other keys use their invariant representation (true, 1.5, .inf, the round-trip format of dates, ...).
This is how the keys of a dictionary whose key type is Object are written.
WriteEndMapping()
YamlWriter.csWrites the end of a mapping.
public void WriteEndMapping()WriteEndSequence()
YamlWriter.csWrites the end of a sequence.
public void WriteEndSequence()WriteNullValue()
YamlWriter.csWrites a null scalar.
public void WriteNullValue()WritePropertyName(string)
YamlWriter.csWrites a mapping key.
Parameters
namestring- The key name.
Exceptions
InvalidOperationException- The writer is not positioned within a mapping key.
WriteScalar overloads
WriteScalar(bool)
YamlWriter.csWriteScalar(byte)
YamlWriter.csWriteScalar(char)
YamlWriter.csWriteScalar(DateOnly)
YamlWriter.csWriteScalar(DateTime)
YamlWriter.csWriteScalar(DateTimeOffset)
YamlWriter.csWrites a date and time with offset scalar value using the round-trip format.
public void WriteScalar(DateTimeOffset value)Parameters
valueDateTimeOffset- The value to write.
WriteScalar(decimal)
YamlWriter.csWriteScalar(double)
YamlWriter.csWriteScalar(Guid)
YamlWriter.csWriteScalar(Half)
YamlWriter.csWriteScalar(Int128)
YamlWriter.csWriteScalar(short)
YamlWriter.csWriteScalar(int)
YamlWriter.csWriteScalar(long)
YamlWriter.csWriteScalar(nint)
YamlWriter.csWriteScalar(BFloat16)
YamlWriter.csAvailable in: net11.0(not available in the other target frameworks of the package)
WriteScalar(Decimal128)
YamlWriter.csAvailable in: net11.0(not available in the other target frameworks of the package)
Writes a 128-bit IEEE 754 decimal floating-point scalar value.
public void WriteScalar(Decimal128 value)Parameters
valueDecimal128- The value to write.
WriteScalar(Decimal32)
YamlWriter.csAvailable in: net11.0(not available in the other target frameworks of the package)
WriteScalar(Decimal64)
YamlWriter.csAvailable in: net11.0(not available in the other target frameworks of the package)
WriteScalar(ReadOnlySpan<char>)
YamlWriter.csWrites a scalar value from a character span.
public void WriteScalar(ReadOnlySpan<char> value)Parameters
valueReadOnlySpan<char>- The scalar text.
Remarks
Text that a plain scalar would resolve to null, such as null or ~, is quoted.
WriteScalar(sbyte)
YamlWriter.csWriteScalar(float)
YamlWriter.csWriteScalar(string?)
YamlWriter.csWrites a scalar value.
Parameters
valuestring?
Remarks
A non-null value is never written as a null scalar: text such as null or ~, which a plain scalar would resolve to null, is quoted. Use YamlWriter.WriteNullValue to write a null.
WriteScalar(TimeOnly)
YamlWriter.csWriteScalar(TimeSpan)
YamlWriter.csWriteScalar(UInt128)
YamlWriter.csWriteScalar(ushort)
YamlWriter.csWriteScalar(uint)
YamlWriter.csWriteScalar(ulong)
YamlWriter.csWriteScalar(nuint)
YamlWriter.csWriteScalar<T>(T)
YamlWriter.csWrites a scalar value for a span-formattable type using invariant culture formatting.
public void WriteScalar<T>(T value) where T : IFormattableType parameters
T- The value type to write.
Parameters
valueT- The value to write.
WriteStartMapping()
YamlWriter.csWrites the start of a mapping.
public void WriteStartMapping()Exceptions
YamlException- The configured maximum nesting depth was exceeded.
WriteStartSequence()
YamlWriter.csWrites the start of a sequence.
public void WriteStartSequence()Exceptions
YamlException- The configured maximum nesting depth was exceeded.
WriteString(string?)
YamlWriter.csWriteTag(string)
YamlWriter.csWrites a YAML tag for the next node, which is a mapping key when YamlWriter.WritePropertyName is called next.
Parameters
tagstring- The YAML tag, such as
!dog.