YAML and JSON
Convert between YAML and JSON in either direction, with anything the reader does not fully implement refused by name rather than guessed at.
Result
Good to know
- Scalars follow the YAML 1.2 core schema, so no, yes, on and off are strings. YAML 1.1 read them as booleans — which is how a country code became false, and the reason that is remembered as the Norway problem.
- Anything this reader does not fully implement is refused by name and by line rather than guessed at: tags, anchors and aliases, merge keys, explicit keys, more than one document in a stream, tabs used for indentation, and a key that appears twice. A subset parser is not a smaller correct parser — it is one that is wrong on the inputs nobody thinks to test.
- Comments are not carried across. What comes out has the document's data, not its annotations — there is nowhere in JSON to put them.
- Going the other way, anything that would read back as a number, a boolean or null is quoted. So are yes, no, on and off: they are strings here, but a YAML 1.1 reader somewhere else would take them for booleans.
- An integer too large to survive as a double is kept as a string rather than silently rounded. A converter that returns a different number than it was given is worse than one that says it cannot.
17 views