This project contains components which allow YamlDotNet to handle System.Text.Json objects and serialize them to YAML and back.
Supported Objects:
-
System.Text.Json.Serialization.JsonIgnoreAttribute
- Conditions - controls whether a property is included while serializing and deserializing
- Always = Ignore during serialization and deserialization (default)
- Never = Include during serialization and deserialization
- WhenWritingNull = Ignore during serialization when the value is
null - WhenWritingDefault = Ignore during serialization when the value is the type's default value
- WhenWriting = Ignore during serialization
- WhenReading = Ignore during deserialization
Conditional
WhenWritingNullandWhenWritingDefaultproperties remain readable during deserialization. Properties ignored withAlwaysorWhenReadingare treated as known ignored properties and skipped when they appear in YAML input. - Conditions - controls whether a property is included while serializing and deserializing
-
System.Text.Json.Serialization.JsonPropertyNameAttribute
- Name - Specifies the property name that is present in the JSON/YAML when serializing and deserializing.
-
System.Text.Json.Serialization.JsonPropertyOrderAttribute
- Order - Sets the serialization order of the property.
-
System.Text.Json.Serialization.JsonStringEnumMemberNameAttribute
- Name - Sets the value for the Enum Member that is present in the JSON/YAML when serializing and deserializing.
dotnet add package YamlDotNet.System.Text.Json
YamlConverter - exposes Serialize() and Deserialize<T>() methods
// to serialize a object to yaml
var yaml = YamlConverter.Serialize(someObject);
// to serialize json to yaml
var yaml = YamlConverter.SerializeJson(someJson);
// to load your object as a typed object
var obj = YamlConverter.Deserialize<MyTypedObject>(yaml);Example:
using YamlDotNet.Serialization;
using YamlDotNet.System.Text.Json;
var serializer = new SerializerBuilder()
.AddSystemTextJson()
.Build();
var yaml = serializer.Serialize(obj);
var deserializer = new DeserializerBuilder()
.AddSystemTextJson()
.Build();
var myObject = deserializer.Deserialize<MyType>(yaml)