3

Using JS-YAML but this probably applies to other YAML i/o libraries.

I'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 guess what I want is to serialize something that would parse in again later, so I'm not sure what value. undefined seems to work for me with this typescript definition

TSdef

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

dcsan
  • 11,333
  • 15
  • 77
  • 118
  • I use yaml-js in Node and I can successfully clear out the undefined fields with `content = JSON.parse(JSON.stringify(content, null, 3));` – mvermand Nov 27 '20 at 12:38

0 Answers0