10

I am trying to use YamlDotNet to help me parse a config file. I have studies its documentation and found two ways:

  1. Using YamlStream's Load() method, followed by examining the nodes of the YamlDocument it has created;
  2. Writing a family of related classes for result storage, and then using Deserializer's Deserialize() method to automatically instantiate and populate the objects with data.

The first approach is not particularly elegant (the code is messy). But it allows me to have extra "label: value" pairs in the input file. Anything extra is ignored. I can also use logic in my code to detect if any "label" is missing and skip trying to read its value.

The second approach is very elegant, and the code is very clean. However, it chokes on the extra "label: value" pairs. Also, if any expected "label: value" pair is missing in the input file, it also throws an exception.

I am looking for a way to use the second approach (calling Deserialize method) but allow it to work even if there is extra data in the input file, or something is missing.

I did not find an "Optional" attribute which I was hoping I could apply to the members of my object model.

Can someone educate me please if having optional nodes or extra unused nodes is possible when using Deserialize approach?

onTy
  • 193
  • 2
  • 12
  • Did you ever figure this out? I need an [optional] parameter too... – irhetoric Dec 12 '17 at 18:10
  • Hello, no I did not. I decided to switch to using JSON instead of YAML, and I've been very happy with Newtonsoft implementation. I highly recommend using JSON.NET as it does everything I needed, except with JSON. – onTy Dec 15 '17 at 16:16
  • 2
    In 2019, still waiting for a halfway decent YAML library for .NET.... I had high hopes for YamlDotNet, but was abandoned too soon... The SharpYaml fork made some good improvements like Enum support, but then went and removed object deserialization... Maybe in another decade I will check back, until then, looks like will have to revert back to JSON. – ForeverZer0 Apr 07 '19 at 07:39

1 Answers1

12

The second approach is actually possible. You need to do the following:

  1. Specify default values for all fields which may be missing in the YAML file, like here:
[DefaultValue(1)]
public double Priority { get; set; }
  1. Tell the deserializer to ignore unmatched properties, like in the following code snippet.
var deserializer = new DeserializerBuilder()
    .IgnoreUnmatchedProperties()
    .Build();
var deserialized = deserializer.Deserialize<T>(input);

I can't find any documentation on this configuration option but for me it works as intended. The only thing I could find is the PR where the feature was introduced.

scriptator
  • 314
  • 2
  • 7