tags:

views:

798

answers:

6

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?

+2  A: 

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  A: 

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.

Cd-MaN
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
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 :-(
Cd-MaN
+1  A: 

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
+1  A: 

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
A: 

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.

Jeff Lorenzini