Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Support serialization of strings to literal and folded scalars #174

Closed
spenserblack opened this issue Sep 30, 2020 · 7 comments
Closed

Support serialization of strings to literal and folded scalars #174

spenserblack opened this issue Sep 30, 2020 · 7 comments

Comments

@spenserblack
Copy link

spenserblack commented Sep 30, 2020

Apologies if this feature already exists, but I didn't see it in the documentation.

Info

the literal scalar | can be used in YAML to break up a string to multiple lines, treating the line breaks literally.

the folded scalar > also can be used to break up a string into multiple lines, but it treats line breaks as spaces.

Request

When serializing a value containing a multiline string, it seems to always serialize that string to a single line with line breaks represented as \n (reproduction)
I think that there should be some way to specify how you want your string to be serialized (single line, folded scalar, literal scalar).

Scalar Examples

Literal: |

Input

key: |
  line 1
  line 2

Output (JSON)

{ "key": "line 1\nline 2\n" }

Folded: >

Input

key: >
  word1
  word2

Output (JSON)

{ "key": "word1 word2" }
@emmanueltouzery
Copy link

I'm also interested in this feature, and several others, but this would have to be implemented by the underlying library used by serde-yaml.

Currently, that is yaml-rust, which is not really maintained. There are three unanswered pull requests for yaml-rust to add the feature:

There isn't much that can be done from serde-yaml I'm afraid.

@spenserblack
Copy link
Author

I see... I was worried that that was the case 😞

Hopefully when those PRs are addressed or a fork is created, this issue can be addressed.

Thanks for your answer!

@jhoobergs
Copy link

@dtolnay Is this something that you would want in this library? Or should I use my own fork(s) if I want this? The main problem does lay at https://github.com/chyh1990/yaml-rust I guess?

@dtolnay
Copy link
Owner

dtolnay commented Aug 10, 2021

To the extent that this issue is asking to expose choice over the string representation at the granularity of individual fields, that is not something I would accept in this library.

If it is asking just for prettier defaults, such as serializing to literal scalar if a string value contains newlines, I am open to that but this crate is not where that code lives.

@dtolnay dtolnay closed this as completed Aug 10, 2021
@jhoobergs
Copy link

@dtolnay yaml-rust doesn't seem so active. Where do you stand on using an on-crates-published-fork (like https://github.com/davvid/yaml-rust) which implements these prettier defaults? (it basically equals yaml-rust but with chyh1990/yaml-rust#137 merged and working CI)

@dtolnay
Copy link
Owner

dtolnay commented Aug 10, 2021

I wouldn't do that. It would be good to drop the dependency on yaml-rust entirely though, in favor of the backend style described in dtolnay/request-for-implementation#9.

@spenserblack
Copy link
Author

If it is asking just for prettier defaults, such as serializing to literal scalar if a string value contains newlines

That is actually what I initially wanted, but was worried that it was too opinionated 😆

this crate is not where that code lives.

If/when possible, could this issue be transferred to the relevant repo?

Repository owner locked and limited conversation to collaborators Jul 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants