gvvaughan / lyaml

LibYAML binding for Lua.
gvvaughan.github.io/lyaml
Other
208 stars 34 forks source link

How to dump empty object and empty array? #47

Closed ichaozai closed 3 years ago

ichaozai commented 3 years ago
local lyaml = require "lyaml"

print(lyaml.dump({{array = {}, object = {}}}))

output:

---
array: []
object: []
...

Lua uses table as array. How to distinguish an empty table and an empty array?

gvvaughan commented 3 years ago

If there are no contents inside, does it matter? Perhaps you can already tell by the name of the key or some other clue from the document structure if you have specific documents.

Otherwise, If you really need to do this for generality or roundtripping or similar, then you’ll need to tweak the dumper code to write a sentinel value for one or the other or both - much the way that it already handles nil.

The best place to start is with a copy of dumper_mt in lib/lyaml/init.lua and then tweak accordingly.

ichaozai commented 3 years ago

It does matter if I process the YAML in static type languages, or convert YAML to JSON. I can make a PR to do this like lua-cjson.