chicken-yaml
chicken-yaml copied to clipboard
Dumping fails on nested mappings
Hello. Thank you for making this egg. I've been using it to parse YAML and like it a lot :)
I found an issue with dumping nested mappings, like the following:
foo:
bar: 123
As shown by the following snippet, which fails with an "unknown"
exn:
(yaml-dump (yaml-load "--- {foo: {bar: 123}}"))
Other cases of weird behaviour is when sequences are nested in maps, like the following:
(yaml-dump (yaml-load "--- {foo: [123, 456]}"))
which yields:
---
- - foo
- 123
- 456
I think the source of the problem is here, which I interpret as a predicate to check object
is an alist:
https://github.com/tenderlove/chicken-yaml/blob/5c9d4fbf9e62af0c67def86198d7ebf7d92d0074/yaml.scm#L67-L69
In it, the final (list? (car object))
is actually #t
because an association to a list is also a list :cry:
I couldn't think of a clean predicate for this, since plain lists are mapped to yaml sequences. I think that's probably the reason why medea uses vectors for JSON lists, and other JSON eggs use hash-tables for mappings. It can get quite ugly to implement a proper alist?
predicate. This is my attempt, which sadly forsakes correctness for nested lists, and is tied to the type mappings for strings.
(define (yaml-alist? object)
(and (list? object)
(not (null? object))
(pair? (car object))
(or (string? (caar object))
(symbol? (caar object)))))