1

I am traversing through a Yaml file and want to deserialize the string saved in Value of YamlScalarNode to a dotnet type.

I could not find a Convert Method in YamlScalarNode.

There must be a converter in the project which regards the yaml conventions for parsing in both ways. I just can't find it.

Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
rudimenter
  • 3,242
  • 4
  • 33
  • 46

1 Answers1

0

YamlScalarNode is part of the DOM API, which provides an object model where you manipulate YAML constructs such as scalars and mappings. This is similar to using XmlDocument.

What you probably want is to use the Deserializer class. It allows you to read a YAML stream as a graph of objects.

Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
  • The problem is i don't have a class structure to deserialize the yaml into it. What about [TypeConverter](https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/Utilities/TypeConverter.cs). Is this meant for using in client code? Does TypeConverter respect special Yaml syntax like _!!float_ – rudimenter Feb 24 '14 at 10:52
  • TypeConverter is used to convert between types using the various mechanisms that exist in the .NET framework and the C# language: TypeConverters, IConvertible, Convert, casting operators, etc. It knows nothing about YAML. I am not sure I understand what you are trying to achieve. Can you provide more context? – Antoine Aubry Feb 24 '14 at 17:06
  • I made some tests and see that this feature is not implemented. Your yaml implementation seem not to care about explicit yaml syntax during deserialization. Basically if you have something like this: --- foo: !!bool true ... This still deserializes into this class: class xyz { public xyz() { } public string foo { get; set; } } But deserialization should fail here because foo is not boolean. But i require this so i will implement my own typeconverter for this. Anyway, nice work. Still save me a lot time :-) – rudimenter Feb 25 '14 at 10:15
  • That is not correct. The deserializer will correctly interpret the !!bool true as a boolean. Then it will attempt to assign it to the string property. Since there is a valid conversion of bool to string, that conversion will be used. – Antoine Aubry Feb 25 '14 at 15:22
  • Ok for _true_. But the following fragment fails for example: _--- foo: !!bool YES ... This one is not deserialized as boolean into: _class xyz { public xyz() { } public bool foo { get; set; } }_ According to [boolean Yaml](http://yaml.org/type/bool.html) this is a valid boolean type. – rudimenter Feb 25 '14 at 15:44
  • BTW: It would be nice if i could use the Deserializer also only on Fragments and not the whole Document. This would be exactly what i need. – rudimenter Feb 25 '14 at 15:47
  • Regarding 'YES', you are correct. We have started implementing schemas, which will also cover this case but it is not usable yet. The deserializer can be used on a fragment, there is no requirement that an entire document is supplied. – Antoine Aubry Feb 26 '14 at 10:01
  • Thanks for clarification. Would be handy to have an overload of the deserializer which accepts a yamlnode as starting point. I've only found a textreader and eventreader overload. – rudimenter Feb 26 '14 at 10:11