However as you can see, the top-level keys are dynamic and the examples in the docs all assume static keys. Any pointers on how to handle this use case would be much appreciated.
Actually if you try it with the original YAML file it doesn’t work, it chokes on those times. I think there might be something wrong with our Time::Format::YAML_DATE…
Yeah, I’ve been trying to use #from_yaml but to no avail. I can understand why its failing, but not sure how else to approach it.
Here’s my attempt so far:
record Item, foo : Int32, barred_at : Time, baz : Bool do
include YAML::Serializable
end
yaml = <<-YAML
---
item1:
foo: 1
barred_at: '2022-01-27T17:00:00Z'
baz: true
item2:
foo: 5
barred_at: '2022-01-27T17:30:00Z'
baz: false
item3:
foo: 10
barred_at: '2022-01-27T17:45:00Z'
baz: true
YAML
items = Array(Item).from_yaml(yaml)
which throws an Unhandled exception: Expected sequence, not mapping at line 2, column 1 (YAML::ParseException). I realise that the YAML represents a hash not an array, so this was never going to work, but hopefully it demonstrates what I’m trying to achieve.
Ultimately I’m hoping to be able to deserialize this YAML into an array of Item structs, which each also include their name (item1, item2, etc.) as a property.