Skip to content

IvanJosipovic/YamlDotNet.System.Text.Json

Repository files navigation

YamlDotNet.System.Text.Json

Nuget Nuget) codecov

What is this?

This project contains components which allow YamlDotNet to handle System.Text.Json objects and serialize them to YAML and back.

Supported Objects:

Installation

dotnet add package YamlDotNet.System.Text.Json

YamlConverter

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);

How to use manually

SystemTextJsonYamlTypeConverter

This is a type converter for reading and writing System.Text.Json objects.

.WithTypeConverter(new SystemTextJsonYamlTypeConverter())

SystemTextJsonTypeInspector

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();

Inspired By

https://github.com/tomlm/YamlConvert

About

Allows YamlDotNet to de/serialize System.Text.Json objects

Resources

License

Stars

Watchers

Forks

Languages