Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
myocytebd opened this issue Oct 31, 2022 · 0 comments
Open

Comments

@myocytebd
Copy link

myocytebd commented Oct 31, 2022

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants