Skip to content

Commit e5568a8

Browse files
committed
Publish post "(Ruby) YAML is weird"
1 parent 4b33777 commit e5568a8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: (Ruby) YAML is weird
3+
date: 2022-10-02 20:00:00 +0200
4+
categories: ruby yaml
5+
---
6+
7+
To my big suprise, turns out that `on` and `off` are supported boolean values in YAML:
8+
9+
```
10+
$ ruby -ryaml -e "puts YAML.load('on: hello')"
11+
{true=>"hello"}
12+
```
13+
14+
So `YAML.load('on: hello')` is the same as `YAML.load('true: hello')` 🤔
15+
YAML is usually used in configuration files because it is easier to read and navigate than JSON but I also feel like some of the parsing behavior is unexpected as this is the second time I'm taking time to write down some thoughts about how YAML parsing in Ruby surprised me (see [other post]({% post_url 2020-10-13-ruby-symbols-in-yaml %})).
16+
17+
So does the YAML parser in the Ruby stdlib, Psych, have the ability to parse YAML in a more limited JSON-esque manner?
18+
19+
The YAML spec mentions [JSON Schema](https://yaml.org/spec/1.2.2/#102-json-schema) which is different than JSON Schema used for validating JSON documents.
20+
Schemas in the context of YAML parsing is described as
21+
22+
> A YAML schema is a combination of a set of tags and a mechanism for resolving non-specific tags.
23+
24+
As far as I can tell Psych does not support JSON Schema and I'm also not sure if that would even solve the challenges I have experienced with it. Ultimately I would like to have a simplified YAML parser which just
25+
26+
- understands JSON scalar values: string, number, boolean and null
27+
- supports objects
28+
- treats all object keys as strings
29+
- supports arrays
30+
31+
Support for YAML aliases and anchors might also be useful but that's basically all I would expect from a YAML parser for most use cases, especially in the context of configuration files.
32+
33+
When time permits I will try to dedicate some time in the near future to look into what it would take to build this simplified YAML parser. Building any kind of parser always feels overwhelming for me but hopefully it will be possible to leverage Psych to achieve this. Wish me luck!

0 commit comments

Comments
 (0)