2021年3月28日星期日

Parsing YAML - instantiating elements in a list using the correct subtypes, without using YAML tags

When parsing a list with items of different subtypes using snakeYAML, how do you instantiate objects of the correct subtypes without using YAML tags?

I'd like to avoid using YAML tags if possible to remain compatible with JSON parsers, but I can't figure out how to parse something like the following, and instantiate the correct subtypes of Control:

controls:  - type: button    width: 10    height: 12    hasLED: Yes    - type: knob    resistence: 0.6    rotates360Degrees: Yes    - type: slider    width: 1    height: 300    motorized: Yes  

Can that done using snakeYAML?


Or with a YAML file in this format?:

controls:  - button:    width: 10    height: 12    hasLED: Yes    - knob:    resistence: 0.6    rotates360Degrees: Yes    - slider:    width: 3    height: 50    motorized: Yes  

Finally, a related question: which of the two previous chunks of YAML code is the more elegant or "preferred" way of representing that information (without using tags)? Or is there a better way?

https://stackoverflow.com/questions/66847687/parsing-yaml-instantiating-elements-in-a-list-using-the-correct-subtypes-with March 29, 2021 at 09:19AM

没有评论:

发表评论