Skip to content

Commit

Permalink
Merge pull request aaubry#522 from cduplantis/patch-1
Browse files Browse the repository at this point in the history
docs: Correction to Quick Starts in README.md
  • Loading branch information
aaubry authored Nov 19, 2020
2 parents 108c192 + 728de14 commit 4453541
Showing 1 changed file with 65 additions and 13 deletions.
78 changes: 65 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,87 @@ The library is compatible with mono's [Ahead-of-Time compilation](https://www.mo

## Quick start

Here are some quick samples to get you started.
Here are some quick samples to get you started which can be viewed in [this fiddle](https://dotnetfiddle.net/CQ7ZKi).

### Deserialization from a string
### Serialization to a string

```c#
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
...

string yaml;
//
var deserializer = new DeserializerBuilder()
var person = new Person
{
Name = "Abe Lincoln",
Age = 25,
HeightInInches = 6f + 4f / 12f,
Addresses = new Dictionary<string, Address>{
{ "home", new Address() {
Street = "2720 Sundown Lane",
City = "Kentucketsville",
State = "Calousiyorkida",
Zip = "99978",
}},
{ "work", new Address() {
Street = "1600 Pennsylvania Avenue NW",
City = "Washington",
State = "District of Columbia",
Zip = "20500",
}},
}
};

var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

var order = deserializer.Deserialize<Order>(yaml);
var yaml = serializer.Serialize(person);
System.Console.WriteLine(yaml);
// Output:
// name: Abe Lincoln
// age: 25
// heightInInches: 6.3333334922790527
// addresses:
// home:
// street: 2720 Sundown Lane
// city: Kentucketsville
// state: Calousiyorkida
// zip: 99978
// work:
// street: 1600 Pennsylvania Avenue NW
// city: Washington
// state: District of Columbia
// zip: 20500
```

### Serialisation to a string

```c#
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
...

var objectToSerialize;
...
var serializer = new SerializerBuilder().Build();
//yaml contains a string containing your YAML
var yaml = serializer.Serialize(objectToSerialize);
var yml = @"
name: George Washington
age: 89
height_in_inches: 5.75
addresses:
home:
street: 400 Mockingbird Lane
city: Louaryland
state: Hawidaho
zip: 99970
";

var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml
.Build();

//yml contains a string containing your YAML
var p = deserializer.Deserialize<Person>(yml);
var h = p.Addresses["home"];
System.Console.WriteLine($"{p.Name} is {p.Age} years old and lives at {h.Street} in {h.City}, {h.State}.");
// Output:
// George Washington is 89 years old and lives at 400 Mockingbird Lane in Louaryland, Hawidaho.
```

## More information
Expand Down

0 comments on commit 4453541

Please sign in to comment.