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.Nodes.JsonNode
- System.Text.Json.Nodes.JsonArray
- System.Text.Json.Nodes.JsonObject
- System.Text.Json.Nodes.JsonValue
- System.Text.Json.JsonElement
- System.Text.Json.JsonDocument
- System.Text.Json.Serialization.JsonIgnoreAttribute
- Conditions - condition that must be met before a property will be ignored
- Always = Ignore (Default)
- Never = Serialize
- WhenWritingNull = Serialize
- WhenWritingDefault = Serialize
- Conditions - condition that must be met before a property will be ignored
- 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.
- System.Text.Json.Serialization.JsonExtensionDataAttribute
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)