Skip to content
This repository was archived by the owner on Nov 30, 2025. It is now read-only.

feat: Preserve scalar styles and YAML tags when serializing YamlNode#777

Open
Tomas-Jakubec wants to merge 2 commits intocharleskorn:mainfrom
Tomas-Jakubec:yaml-node-serialize-preserve-style-and-tags
Open

feat: Preserve scalar styles and YAML tags when serializing YamlNode#777
Tomas-Jakubec wants to merge 2 commits intocharleskorn:mainfrom
Tomas-Jakubec:yaml-node-serialize-preserve-style-and-tags

Conversation

@Tomas-Jakubec
Copy link
Copy Markdown

Summary

Fixes loss of scalar style and YAML tag information during round-trip serialization of YamlNodes. Resolves issue #749.

Problem

Currently YamlScalar does not retain the original scalar style, making it impossible to distinguish between:

  • A plain numeric scalar (e.g. 123)
  • A quoted string that looks like a number (e.g. "123")
    Additionally, YAML tags present in the parsed input were not emitted during serialization.

Root cause

  • Scalar style (plain, single quoted, double quoted, literal, folded, JSON style) was not stored alongside the scalar value.
  • Serialization of YamlNode relied on generic Kotlinx Serialization paths that discarded style and tag metadata.

Solution

  • Introduced preservation of scalar style via an explicit enum (YamlNodeScalarStyle) mapped to SnakeYAML engine styles.
  • Ensured YAML tags on nodes are carried through and emitted.
  • Delegated encoding of YamlNode subclasses to YamlOutput, separating it from standard Kotlinx Serialization logic to avoid interference and to centralize style/tag handling.

Key changes

  • Updated serialization pipeline to include style and tag emission.
  • Adjusted YamlOutput to take responsibility for YamlNode encoding.
  • Added/updated tests to assert round-trip fidelity for:
    • Quoted vs plain numeric-looking scalars
    • Block scalar styles (literal, folded)
    • Explicit tags

Backward compatibility

  • Public API surface minimally affected (new enum exposed).
  • Existing consumers not relying on style/tag round-tripping remain unaffected.
  • No breaking rename/removal of existing types.

Performance

  • Negligible impact; added metadata handling only during node serialization.

Risks / considerations

  • Consumers constructing YamlScalar programmatically may now wish to specify a style explicitly. However, not specifying it results in original behavior.
  • Ensure all new styles are covered in multiplatform targets. I tested all platforms except for tvosSimulatorArm64Test, please check.

Testing

Closes: #749

Let me know if a changelog entry format needs adjustment.

@Tomas-Jakubec Tomas-Jakubec changed the title Preserve scalar styles and YAML tags when serializing YamlNode feat: Preserve scalar styles and YAML tags when serializing YamlNode Oct 21, 2025
@Tomas-Jakubec
Copy link
Copy Markdown
Author

Corrected the style using spotlessKotlinApply.

@Tomas-Jakubec
Copy link
Copy Markdown
Author

@charleskorn

@Tomas-Jakubec
Copy link
Copy Markdown
Author

@charleskorn can you review the PR please?

@Tomas-Jakubec
Copy link
Copy Markdown
Author

@charleskorn please review the PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YamlNodeSerializer corrupts data due to implicit types conversion

1 participant