YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Pretty serialization: dynamically switch flow/block style and line width

Open myocytebd opened this issue 3 years ago • 0 comments
trafficstars

My goal:

  • Switch flow and block style dynamically depending on context (e.g. object path).
  • Try to keep lines within a target width.

I don't know how to get this done:
A simple way would be:

  • Register a callback on every value. Check or infer path to decide the style. How to register such kind of callback? TypeConverter does not provide contextual info.
  • Try to serialize value with desired style. How to try serialize value with specified style? Emitter.Emit have style argument, but emit seems to be final. Create another serializer? But serialization might be different (if not plain data), and quite verbose (Because serializer does not have a style argument, it would need another couple of TypeConverter or TypeResolver to enforce styles).
  • Check or estimate line width from serialized value, and maybe fallback to block style, or request wrap-around.
    • Is line width or line-wrap supported?

In js-yaml, it is simple to get a limited workaround. replacer(key, value) callback is invoked on every value, and thus object path can be kept track of. serialize API has a flowLevel option, so try serialize is another serialize call. Downsides:

  • Another serialize -> different serialization.
  • Line-wrap cannot be done.

myocytebd avatar Oct 31 '22 17:10 myocytebd