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.
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);
This is a type converter for reading and writing System.Text.Json objects.
.WithTypeConverter(new SystemTextJsonYamlTypeConverter())
This is a type inspector for reading System.Text.Json Attributes
.WithTypeInspector(x => new SystemTextJsonTypeInspector(x))
Example:
var serializer = new SerializerBuilder()
.WithTypeConverter(new SystemTextJsonYamlTypeConverter())
.WithTypeInspector(x => new SystemTextJsonTypeInspector(x))
.Build();
var deserializer = new DeserializerBuilder()
.WithTypeConverter(new SystemTextJsonYamlTypeConverter())
.WithTypeInspector(x => new SystemTextJsonTypeInspector(x))
.Build();