Skip to content

Write DateTimeOffset, DateOnly, TimeOnly and Version values as one scalar [patch] - #220

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/scalar-converters
Oct 30, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/scalar-converters

Conversation

@matt-edmondson

@matt-edmondson matt-edmondson commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #217

What changed

The shared Serializer in YamlSerializer.cs now has an InvariantScalarTypeConverter registered. It writes each of these types as a single scalar instead of a mapping of its CLR properties:

Type Written as
DateTimeOffset 2024-01-15T10:30:00.0000000+02:00, plain ISO 8601 round-trip, so Jekyll and Hugo read it as a timestamp
DateOnly (.NET 6+) 2024-01-15
TimeOnly (.NET 6+) "10:30:00" or "10:30:05.25", double-quoted because YAML 1.1 reads a plain 10:30:00 as the base-60 number 37800
Version its ToString(), written as a string, so 1.10 is quoted rather than read back as the number 1.1

All formats use CultureInfo.InvariantCulture. YamlDotNet's own DateOnlyConverter uses the current culture, which is why this PR doesn't use it.

This covers every path that writes through the serializer:

  • AddFrontmatter, both when it creates a header and when it inserts into an existing one
  • ReplaceFrontmatter
  • SerializeYamlObject
  • Nested values

Tests

Frontmatter.Test/InvariantScalarTypeTests.cs adds 8 cases. They run under a non-invariant culture that uses . for times and , for decimals. The culture is cloned from the invariant one, so the tests also run where globalization-invariant mode is on and named cultures like de-DE aren't available.

The cases check:

  • The exact header text for each type.
  • That ExtractFrontmatter reads the value back as one scalar.
  • Inserting into an existing header.
  • Values nested in a mapping.

With the converter reverted, all 8 fail. The full suite passes: 377/377 on net10.0. The library also builds for netstandard2.0/2.1 and net8/9/10, where DateOnly/TimeOnly are behind NET6_0_OR_GREATER.

Notes

🤖 Generated with Claude Code

https://claude.ai/code/session_017vfJW4yhNfJ8wLeTtjP3L1

…alar [patch]

The shared serializer had no converters for these types, so YamlDotNet wrote
each one as a mapping of its public properties. A DateTimeOffset date came out
as about 20 keys that site generators cannot read.

They now go through one invariant converter:
- DateTimeOffset is written plain as an ISO 8601 round-trip timestamp.
- DateOnly is written as yyyy-MM-dd.
- TimeOnly is written as a double-quoted HH:mm:ss[.fffffff], because YAML 1.1
  reads a plain 10:30:00 as a base-60 number.
- Version is written as a string, which quotes one such as 1.10 that would
  otherwise read back as a number.

None of them depend on the current culture.

Fixes #217

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4yhNfJ8wLeTtjP3L1
…overage gate

The converter is registered on the serializer only, so ReadYaml never ran; it now
throws NotSupportedException. Version takes the default branch, which no longer
needs a separate null case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4yhNfJ8wLeTtjP3L1
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 2825278 into main Oct 30, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/scalar-converters branch October 30, 2026 02:52
matt-edmondson pushed a commit that referenced this pull request Oct 31, 2026
CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
matt-edmondson pushed a commit that referenced this pull request Oct 31, 2026
CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)
matt-edmondson pushed a commit that referenced this pull request Oct 31, 2026
CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)
matt-edmondson pushed a commit that referenced this pull request Nov 2, 2026
CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)
matt-edmondson pushed a commit that referenced this pull request Nov 2, 2026
CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
…h] (#226)

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223


Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3

Co-authored-by: Claude <noreply@anthropic.com>
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
…227)

* Keep keys in input order when CombineFrontmatter merges them [patch]

MergeSimilarProperties wrote its output one canonical-name group at a
time, so a later key that shares a canonical name with an earlier one
moved up next to it, even when the two were kept apart (different value
types). FrontmatterOrder.AsIs then did not keep the document's order,
and under Sorted a second run reordered the keys again.

The merged dictionary is now put back in input order: a key that keeps
its own name keeps its own position, and a merged key takes the
position of the first member merged into it. Adds both reproductions
from the issue and seeded tests for AsIs order and idempotence across
every merge strategy.

Fixes #224

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3

* Give the caller-value serializer the invariant scalar converter [patch]

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3

---------

Co-authored-by: Claude <noreply@anthropic.com>
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
* Quote caller strings that read back as floats, = or << [patch]

AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter now double-quote
caller strings matching the YAML 1.2 float pattern (1e3, 2e10, 1.5e3), which
quoteYaml1_1Strings left plain since #216, and strings equal to = or <<,
which PyYAML's safe_load cannot construct.

Fixes #228
Fixes #230

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2dKE2suWaWTkfYf74

* Apply the lookalike quoting on both serializers so the builder setup stays as it was

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2dKE2suWaWTkfYf74

* Give the caller-value serializer the invariant scalar converter [patch]

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)

---------

Co-authored-by: Claude <noreply@anthropic.com>
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
* Resolve only a plain << as a YAML merge key [patch]

A single- or double-quoted "<<" key is an ordinary string key in YAML 1.1,
PyYAML and js-yaml. Deserialize through a parser that swaps a quoted <<
scalar for a stand-in, so ResolveMergeKeys no longer flattens its contents
into the parent, and turn the stand-in back into << on conversion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2dKE2suWaWTkfYf74

* Give the caller-value serializer the invariant scalar converter [patch]

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)

---------

Co-authored-by: Claude <noreply@anthropic.com>
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
…nts [patch] (#236)

* Insert a double or decimal into the header without dropping its comments [patch]

TryInsertIntoHeader compared the header it read back against the caller's
raw values, but this library's reader reads numbers lossily, so a double
that isn't exact in float32 (51.507351) or a decimal with a trailing zero
(9.90m) never compared equal. The insert was rejected and the header was
rebuilt, dropping its comments and requoting existing values.

The expected properties are now the existing ones plus the added keys as
the reader reads their written text back, so both sides of the check go
through the same reader.

Fixes #234

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW6SmLTCjq6sfKLU8dj

* Give the caller-value serializer the invariant scalar converter [patch]

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)

---------

Co-authored-by: Claude <noreply@anthropic.com>
matt-edmondson added a commit that referenced this pull request Nov 4, 2026
…itten [patch] (#237)

* Keep values a header already holds as written when the header is rewritten [patch]

ReplaceFrontmatter, and AddFrontmatter's rebuild fallback through it,
wrote every value with the caller-value serializer, which quotes YAML 1.1
lookalikes. A value read from the document, such as published: no, came
back as the truthy string "no", so a Jekyll draft got published.

Values that serialize the same as the existing header's value for the key
are now written with the document serializer; only changed or added
values get YAML 1.1 quoting.

Fixes #233

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW6SmLTCjq6sfKLU8dj

* Give the caller-value serializer the invariant scalar converter [patch]

CallerValueSerializer, added in #216, was built without the
InvariantScalarTypeConverter that #220 gave the shared Serializer, so
AddFrontmatter, ReplaceFrontmatter and SerializeFrontmatter wrote
DateTimeOffset, DateOnly, TimeOnly and Version values as mappings of
their properties again, and main failed 7 InvariantScalarTypeTests.

Both serializers now start from one CreateSerializerBuilder, so only
their string quoting differs and the next shared option cannot be left
off one of them. Adds tests for ReplaceFrontmatter, SerializeFrontmatter
and values inside a list.

Fixes #223

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5WhwpCJojPcgRgxL3
(cherry picked from commit 06f270b)

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

AddFrontmatter writes DateTimeOffset, DateOnly, TimeOnly and Version values as a mapping of their CLR properties instead of a scalar

2 participants