Reduce code duplication in converters and event emitters#1090
Merged
EdwardCooke merged 3 commits intoaaubry:masterfrom Apr 9, 2026
Merged
Reduce code duplication in converters and event emitters#1090EdwardCooke merged 3 commits intoaaubry:masterfrom
EdwardCooke merged 3 commits intoaaubry:masterfrom
Conversation
Create abstract ScalarConverterBase<T> providing shared functionality: - Generic Accepts(Type) method (eliminates boilerplate from 5 classes) - ConsumeScalarValue() helper for reading scalars - EmitScalar() helper for writing scalars with a given style Migrate DateTimeConverter, DateTimeOffsetConverter, DateTime8601Converter, DateOnlyConverter, and TimeOnlyConverter to inherit from the new base class.
- JsonEventEmitter: use formatter.FormatNumber() for float/double/decimal instead of direct CultureInfo.InvariantCulture formatting, matching the pattern used by TypeAssigningEventEmitter. Preserves NaN/Infinity quoting. - TypeAssigningEventEmitter: use .IsEnum() extension method instead of .IsEnum property for enum detection, matching JsonEventEmitter pattern. - Remove unused System.Globalization and System.Text.RegularExpressions imports from JsonEventEmitter.
The FormatNumber() method uses YAML-style symbols (.nan, .inf, -.inf) which are incorrect for JSON output. For NaN/Infinity values, use CultureInfo.InvariantCulture to produce standard representations (NaN, Infinity, -Infinity) while still using FormatNumber() for regular numeric values.
Contributor
Author
|
Fixed the CI failure in commit 07c17d4. Root cause: The refactoring of Fix: For float/double values that are The |
This was referenced Apr 9, 2026
This was referenced Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduce code duplication across DateTime/TimeOnly converters and event emitter scalar rendering.
Extract
ScalarConverterBase<T>for converter familyCreated new abstract base class
ScalarConverterBase<T>providing:Accepts(Type)method — eliminates identical boilerplate from 5 converter classesConsumeScalarValue()helper for reading scalar values from the parserEmitScalar()helper for writing scalars with a given styleMigrated 5 converters to inherit from the base:
DateTimeConverterDateTimeOffsetConverterDateTime8601ConverterDateOnlyConverterTimeOnlyConverterAlign event emitter scalar rendering
formatter.FormatNumber()forfloat,double, anddecimalinstead of directCultureInfo.InvariantCultureformatting. This aligns withTypeAssigningEventEmitterand ensures the configured formatter is respected. NaN/Infinity double-quoting is preserved..IsEnum()extension method instead of.IsEnumproperty for enum detection, matching theJsonEventEmitterpattern.System.GlobalizationandSystem.Text.RegularExpressionsimports fromJsonEventEmitter.Files Changed
YamlDotNet/Serialization/Converters/ScalarConverterBase.cs(new)YamlDotNet/Serialization/Converters/DateTimeConverter.csYamlDotNet/Serialization/Converters/DateTimeOffsetConverter.csYamlDotNet/Serialization/Converters/DateTime8601Converter.csYamlDotNet/Serialization/Converters/DateOnlyConverter.csYamlDotNet/Serialization/Converters/TimeOnlyConverter.csYamlDotNet/Serialization/EventEmitters/JsonEventEmitter.csYamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.csValidation
All 1845 tests pass (4 pre-existing locale-dependent float formatting failures unrelated to these changes).