Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomlm/yamlconvert
YamlDotNet library to serialize objects to/from YAML honoring JSON.NET attributes
https://github.com/tomlm/yamlconvert
Last synced: 5 days ago
JSON representation
YamlDotNet library to serialize objects to/from YAML honoring JSON.NET attributes
- Host: GitHub
- URL: https://github.com/tomlm/yamlconvert
- Owner: tomlm
- License: mit
- Created: 2021-12-21T00:07:01.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-21T14:38:53.000Z (2 months ago)
- Last Synced: 2024-12-21T20:37:08.184Z (about 1 month ago)
- Language: C#
- Size: 43 KB
- Stars: 9
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# YamlConvert
**YamlConvert** is a YamlDotNet extension library which makes it trivially easy to serialize objects to/from YAML honoring JSON.NET attributes## Json.Net attributes
If you have been using JSON.NET you probably already use JsonProperty/JsonIgnore annotations to make your serialization just right. Then you think "why
not try this YAML thing...and discover that your JObjects blow up and your annotations are ignored.This library fixes this by creating a object converter for yamldotnet which understands how to read and write JObject/JArray/JValue objects. Not only that, but it
honors the JSON.NET attributes you have painstakingly created.## Installation
```nuget install YamlConvert```## YamlConvert
**YamlConvert** - exposes SerializeObject() and DeserializeObject() methods which work just like JsonConvert, but with YAML```
// to serialize to yaml
var yaml = YamlConvert.SerializeObject(someObject);// to load your object as a JToken
dynamic obj1 = YamlConvert.DeserializeObject(yaml);// to load your object as a typed object
var obj2 = YamlConvert.DeserializeObject(yaml);
```## JTokenYamlConverter
This is a type converter for reading and writing JToken objects. It's automatically used by YamlConvert, but you can add it to your own serializer definition by using
``` .WithTypeConverter(new JTokenYamlConverter())```Example:
```
var serializer = new SerializerBuilder()
.WithTypeConverter(new JTokenYamlConverter())
.Build();
var deserializer = new DeserializerBuilder()
.WithTypeConverter(new JTokenYamlConverter())
.Build();
```