How to use Yaml references with Rust's serde_yaml?
Asked Answered
H

1

6

How can I use yaml tags with serde YAML, does it supports it?

like:

field1: &tag
  - item

field2: *tag

I've tried using references, but couldn't figure out the lifetimes and std::rc:Rc<> violates the ownership requirements Deserialize has

Hatpin answered 10/8, 2022 at 19:13 Comment(4)
This deserializes just fine, it just copies field1's contents into field2, correct?Okie
@Okie So I guess the answer is, it works but it doesn't preserve the reference? I mean, it replaces the reference with a data duplication? Which is a little sad, because when serialized again, the tag is probably gone ... But I guess serde really isn't made for references sadlyAlkahest
Yeah, I don't think there's a way to serialize it back like that by default.Okie
Too bad. Copying the fields wouldn't be a problem for me, but I would definitely need to serialize this struct back into yaml at some point.Hatpin
E
5

Support for YAML tags and aliases in serde-yaml is serviceable but not fully featured. It will deserialize using the tags by simply duplicating the data, but it will not serialize with tags. The crate's primary job is to serve as an adapter between YAML and serde's data model which does not support aliasing.

In Issue #177: enable support for tags when serializing, dtolnay responds:

I think I would prefer not to build this into this crate. There is room for someone to build a more featureful YAML emitter which would expose this sort of thing.

There are certainly other yaml-related crates, though I do not know which ones can preserve or allow you to encode aliases.

Epistemic answered 21/9, 2022 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.