Skip to content

feat: implement block scalar chomping handling for serialization and deserialization#12

Merged
glucaci merged 1 commit intomainfrom
fix-string-serialization
Jan 21, 2026
Merged

feat: implement block scalar chomping handling for serialization and deserialization#12
glucaci merged 1 commit intomainfrom
fix-string-serialization

Conversation

@glucaci
Copy link
Copy Markdown
Member

@glucaci glucaci commented Jan 21, 2026

This pull request adds robust support for YAML block scalar "chomping" indicators (|, |-, |+) to both the reader and writer, ensuring correct handling of trailing newlines in multi-line strings. It also introduces logic to automatically use the literal block style when serializing multi-line strings and includes comprehensive tests to verify roundtripping and edge cases.

Block scalar chomping support:

  • Added a private field _blockScalarChomping and logic to track and store the chomping indicator in both ParseLiteralBlockScalar and ParseFoldedBlockScalar methods in Utf8YamlReader.Parsing.cs and Utf8YamlReader.cs. [1] [2] [3]
  • Updated ProcessBlockScalarContent to apply chomping rules: strip (-), keep (+), or clip (default), ensuring correct trimming or preservation of trailing newlines when reading block scalars. [1] [2] [3]
  • Modified the Read() method to pass the chomping indicator to ProcessBlockScalarContent.

Writer improvements for multi-line strings:

  • Updated WriteScalarValue in Utf8YamlWriter.cs to auto-detect multi-line strings and serialize them using literal block style (|, |-, or |+) based on the string's trailing newlines, with a dedicated WriteLiteralScalarValue helper. [1] [2]

Testing and validation:

  • Added extensive tests in PrimitiveSerializationTests.cs to verify:
    • Serialization of multi-line strings uses literal block style with correct chomping indicators.
    • Correct round-tripping of multi-line strings with and without trailing newlines.
    • Accurate parsing of block scalars with strip chomping, even when followed by other YAML content.

@glucaci glucaci changed the title fix: implement block scalar chomping handling for serialization and deserialization feat: implement block scalar chomping handling for serialization and deserialization Jan 21, 2026
@glucaci glucaci merged commit b54dc4f into main Jan 21, 2026
1 check passed
@glucaci glucaci deleted the fix-string-serialization branch January 21, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant