yaml / yaml-test-suite

Comprehensive, language independent Test Suite for YAML
MIT License
172 stars 58 forks source link

8G76: an empty file is not a valid JSON document #116

Closed Snaipe closed 2 years ago

Snaipe commented 2 years ago

Test case 8G76 contains an empty in.json file, but an empty file is not a valid JSON document.

I'd need confirmation, but I think in.yaml parses canonically as null (since it's the empty string, and it matches null in the 1.2 core schema). In this case, I'd expect in.json to just contain the string null.

Snaipe commented 2 years ago

HWV9 also seem to suffer the same issue.

perlpunk commented 2 years ago

We are actually using a special format in in.json, which is not strictly JSON. It can contain multiple structures, just like YAML can contain multiple documents. e.g. this YAML:

---
foo: bar
---
- baz

would have this in.json file:

{
  "foo": "bar"
}
[
  "baz"
]

An empty YAML file contains zero documents, so it will load as an empty array. The same is true for an empty in.json file.

Snaipe commented 2 years ago

I see. Do 8G76 and HWV9 not contain any YAML document? They're not zero-size files, the first contains one comment, and the second contains a document-end marker -- I'd have expected both to contain exactly one document with a null value, and it's been the assumption of the comment-preserving parser I'm currently writing.

perlpunk commented 2 years ago

right, they do not contain any document, which you should see when looking at the test.event file. only a document start --- or actual content will result in an actual document. comments or ... are simply ignored in that regard. related to that: theoretically a library providing a function for exactly one document should return an error for an empty stream, or at least return something that is different from null if the language provides something like that.

regarding the location of a comment in the parse tree: there should probably be a node kind for an empty document, if the stream is supposed to roundtrip. for example it's also possible to have this:

foo
...
# comment
...
# comment
...
perlpunk commented 2 years ago

is your parser already public somewhere? we're always interested in new libraries :)

Snaipe commented 2 years ago

Thanks for clarifying -- ignoring these empty documents might be somewhat problematic from the perspective of a parser trying to preserve presentation details like comments, but I guess I can figure out a way to save these as extra metadata somewhere and appropriately do not count them as documents.

is your parser already public somewhere? we're always interested in new libraries :)

The YAML parser itself is still being written, and will be part of a larger WIP at https://github.com/Snaipe/boa -- I'll make sure to link back here once I get all the tests passing and the parser gets merged!

Snaipe commented 2 years ago

Closing this as it was not, in fact, a bug.