how to handle undefined values cleanly?
See original GitHub issueI’m dump
ing an object, and fields without values come out like this:
fieldName: !<tag:yaml.org,2002:js/undefined> ''
how can I suppress that to either undefined
or just to skip it like JSON.stringify
?
I tried some options from the safeDump
const blob = yaml.dump(obj, { skipInvalid: true, lineWidth: 200 })
https://github.com/nodeca/js-yaml#safedump-object---options-
but without success. I guess I can strip out the undefined fields, but I’d have to do a deep
clean of the whole object… also check for false
vs undefined
etc. etc.
I tried this but it didn’t have any effect:
const clean = { ...obj } // remove nulls?
Sure there must be a nicer way to handle this with js-yaml
?
Do I have to define my own schema?
related: https://github.com/nodeca/js-yaml/issues/456 https://stackoverflow.com/a/38340374/1146785
async debugMessage(obj) {
console.log('json', JSON.stringify(obj, null, 2))
const blob = yaml.dump(obj)
console.log('yaml', blob)
}
// json
{
"msg": "router",
"input": "sleep",
"eventType": "action",
"handled": {
"handled": true,
"doc": {
"match": "^cont|.*",
"goto": "prologue"
},
"klass": "room",
"history": [
"goto"
]
}
}
// yaml
msg: router
input: sleep
eventType: action
parsed: !<tag:yaml.org,2002:js/undefined> ''
handled:
handled: true
doc:
match: ^cont|.*
goto: prologue
klass: room
history:
- goto
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Fixed in https://github.com/nodeca/js-yaml/commit/56d5616938af5be943074222b9b10e0519b170b7 (currently in
dev
branch).Now
undefined
follows roughly the same black magic as in JSON:null
in collections (errors out if null isn’t in the schema)If user defines custom schema for
undefined
, it serializes as before.An option would be a nice addition 👍