4

So for my text parsing in C# question, I got directed at YAML. I'm hitting a wall with this library I was recommended, so this is a quickie.

heading:
 name: A name
 taco: Yes
 age: 32

heading:
 name: Another name
 taco: No
 age: 27

And so on. Is that valid?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Bernard
  • 45,296
  • 18
  • 54
  • 69

8 Answers8

12

Partially. YAML supports the notion of multiple consecutive "documents". If this is what you are trying to do here, then yes, it is correct - you have two documents (or document fragments). To make it more explicit, you should separate them with three dashes, like this:

---
heading:
 name: A name
 taco: Yes
 age: 32
---
heading:
 name: Another name
 taco: No
 age: 27

On the other hand if you wish to make them part of the same document (so that deserializing them would result in a list with two elements), you should write it like the following. Take extra care with the indentation level:

- heading:
  name: A name
  taco: Yes
  age: 32
- heading:
  name: Another name
  taco: No
  age: 27

In general YAML is concise and human readable / editable, but not really human writable, so you should always use libraries to generate it. Also, take care that there exists some breaking changes between different versions of YAML, which can bite you if you are using libraries in different languages which conform to different versions of the standard.

Grey Panther
  • 12,870
  • 6
  • 46
  • 64
  • 1
    In your second example, you need to indent the "name", "taco", and "age" lines a bit more. Right now, "heading" is lined up with them, and PyYAML puts "heading" into the same mapping with the other three. – eksortso Apr 29 '09 at 03:38
  • 3
    AFAIK, after looking at the standard briefly, it seems that it should work as it is currently (I might be wrong however). Again, my point is that there are many implementations of YAML and it is not always clear the level of compatibility between them :-( – Grey Panther Apr 29 '09 at 08:54
  • **//YAML is concise and human readable / editable, but not really human writable, so you should always use libraries to generate it//** Everything else about this answer was spot on ... but what's this about? *raises eyebrows* – dreftymac Oct 07 '17 at 16:16
  • @dreftymac - for one YAML is white-space sensitive which brings all the baggage of tabs-vs-spaces and I always need to turn on "show whitespaces" in my editor when working with YAML. Also, besides lists and dictionaries, I find it quite hard to remember the syntax of the more exotic elements. *gasp* I even find XML easier to write than YAML :-) – Grey Panther Oct 08 '17 at 17:16
  • @GreyPanther: Hmmm ... interesting ... sounds like a strong personal preference against indentation-based syntax. Fair enough, I wasn't a fan originally either. Python and YAML both have architectural reasons for that, which some do not prefer. However, with a good text editor and sufficient use, the expressive power of both can really shine for those who are not inclined against them. YAML can be written extensively by hand with the support of templates, snippets and pre-fabricated boilerplate. – dreftymac Oct 08 '17 at 18:36
4

Well, it appears YAML is gone out the window then. I want something both human writable and readable. Plus, this C# implementation...I have no idea if it's working or not, the documentation consists of a few one line code examples. It barfs on their own YAML files, and is an old student project. The only other C# YAML parser I've found uses the MS-PL which I'm not really comfortable using.

I might just end up rolling my own format. Best practices be damned, all I want to do is associate a key with a value.

Bernard
  • 45,296
  • 18
  • 54
  • 69
  • 3
    I normally try to avoid necromancy, but basically CDMan was talking total BS when he said it's not human writable. It's *extremely easy to write*, just as easy as editing it. If you can understand the way whitespace is significant in Python, then you can write YAML no problem. ... It would be pretty silly otherwise, since being genuinely human-writable was one of YAML's primary aims. – kampu May 26 '13 at 07:37
  • Have to agree with @kampu here. YAML is no less human-friendly than any other ordinary text file where spaces is used to indicate structure and nesting. – dreftymac Oct 08 '17 at 18:39
4

Try this(Online YAML parser).

You don't have to download anything or do something. Just go there, and copy & paste. That's it.

SeniorLee
  • 805
  • 1
  • 12
  • 25
3

There appears to be a YAML validator called Kwalify which should give you the answer. You shoulda just gone with the String tokenizing, man. Writing parsers is fun :)

eplawless
  • 4,225
  • 7
  • 34
  • 35
2

There is another YAML library for .NET which is under development. Right now it supports reading YAML streams. It has been tested on Windows and Mono. Write support is currently being implemented.

Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
2

CodeProject has one at:

http://www.codeproject.com/KB/recipes/yamlparser.aspx

I haven't tried it too much, but it's worth a look.

1

You can see the output in the online yaml parser :

http://yaml-online-parser.appspot.com/?yaml=heading%3A%0D%0A+name%3A+A+name%0D%0A+taco%3A+Yes%0D%0A+age%3A+32%0D%0A%0D%0Aheading%3A%0D%0A+name%3A+Another+name%0D%0A+taco%3A+No%0D%0A+age%3A+27%0D%0A&type=json

As you can see, there is only one heading node created.

Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
0

Just to make an explicit comment about it: You have a duplicate mapping key issue. A YAML processor will resolve this as a !!map, which prohibits duplicate keys. Not all processors enforce this constraint, though, so you might get an incorrect result if you pass an incorrect YAML stream to a processor.

MrBackend
  • 597
  • 3
  • 15