mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
701 stars 206 forks source link

Support for document streams? #49

Open ottawadeveloper opened 8 years ago

ottawadeveloper commented 8 years ago

Hi!

I love using Spyc for YAML parsing, but I have a question - was it intentional not to have multiple document support?

Background: As per http://yaml.org/spec/1.0/#id2489959 YAML streams can have multiple documents. For instance:


---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
  This is an error message
  for the log file

---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
  A slightly different error
  message.
...

should result in two different YAML entries. Instead, I end up with:

array(4) {
  ["Time"]=>
  string(22) "2001-11-23 15:02:31 -5"
  ["User"]=>
  string(2) "ed"
  ["Warning"]=>
  array(2) {
    [0]=>
    string(26) "A slightly different error"
    [1]=>
    string(8) "message."
  }
  [0]=>
  string(3) "..."
}

Which is fine if we're never expecting multiple documents in the same stream, but it can cause problems if we are trying to use it in that fashion. Would you be interested in a patch that adds a new method or a flag for the load methods that causes them to become aware of documents?

I'm not sure if syck handles them or not either, but if they do then this would cause two different behaviours depending on whether or not syck is available.

jackmcdade commented 7 years ago

I think it's more common for YAML to be used for Front Matter these days, in which everything after the 2nd set of --- hyphens becomes Markdown or another form of text/markup. In that scenario, --- becomes an <hr>. Any solution for streams would have to take that into account as a default behavior.

Perhaps Spyc::YAMLLoadStream()?