Skip to content

Double roundtripping#98

Merged
tayloraswift merged 5 commits intomasterfrom
double-roundtripping
Jan 19, 2026
Merged

Double roundtripping#98
tayloraswift merged 5 commits intomasterfrom
double-roundtripping

Conversation

@tayloraswift
Copy link
Member

fix #83
fix #91

@gemini-code-assist
Copy link

Summary of Changes

Hello @tayloraswift, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a substantial overhaul of how JSON numbers are represented, parsed, and encoded within the library. The primary goal is to improve the accuracy and robustness of numeric handling, particularly for floating-point values and non-finite numbers, by adopting a more flexible and efficient internal representation. This refactoring also aligns the project with the latest Swift 6.2 toolchain and enhances the overall testing coverage for numeric types.

Highlights

  • Swift Version Upgrade: The project has been updated to require Swift 6.2, with corresponding changes in Package.swift and README.md. New Swift settings for upcoming features and warnings have also been enabled.
  • Refactored JSON Number Representation: The core JSON.Number type has been refactored from a struct to an enum, now supporting inline (for efficient fixed-point numbers), fallback (for complex numbers represented as strings), infinity, nan, and snan. A new JSON.Number.Inline struct was introduced for memory-efficient fixed-point number storage.
  • Improved Floating-Point Handling and Roundtripping: The parsing and encoding logic for numbers has been significantly enhanced to correctly handle and roundtrip floating-point values, including Float16, Float, Double, and Float80, as well as non-finite numbers like NaN, sNaN, and Infinity. This ensures better precision and interoperability.
  • Simplified JSON Node and Object Structures: The JSON.Node enum no longer has a separate numberExtension_ case, consolidating all number types under the number case. Additionally, the JSON.Object's special init(encoding:) for numbers has been removed, streamlining the API.
  • Enhanced Numeric Testing: A new dedicated test suite, ParsingNumerics.swift, has been added to provide comprehensive testing for number parsing and roundtripping, covering various integer and floating-point edge cases.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant refactoring of how numeric values are handled to ensure lossless round-tripping of floating-point numbers, which resolves issues #83 and #91. The JSON.Number type is changed from a struct to an enum to support non-finite numbers and values that don't fit into a 64-bit representation, using a string fallback. The parsing and encoding logic is updated accordingly, with new tests to verify the round-tripping behavior.

My review has identified a critical issue in the number parsing logic where an integer overflow can occur, potentially crashing the application. I've also suggested a simplification for the Swift settings configuration in Package.swift to improve readability and maintainability.

Comment on lines +104 to +106
case .minus:
// note: potential crash if `exponent.magnitude` is absurdly large
places += exponent.magnitude

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

As noted in the comment, places += exponent.magnitude can cause an integer overflow, which would lead to a crash in debug builds or incorrect parsing in release builds. You should use addingReportingOverflow to safely perform the addition and fall back to the string representation if an overflow occurs.

Suggested change
case .minus:
// note: potential crash if `exponent.magnitude` is absurdly large
places += exponent.magnitude
case .minus:
let (sum, overflow) = places.addingReportingOverflow(exponent.magnitude)
if overflow { break representable }
places = sum

Comment on lines +66 to +82
package.targets = package.targets.map {
switch $0.type {
case .plugin: return $0
case .binary: return $0
default: break
}
{
var settings: [SwiftSetting] = $0 ?? []

settings.append(.enableUpcomingFeature("ExistentialAny"))
settings.append(.treatWarning("ExistentialAny", as: .error))
settings.append(.treatWarning("MutableGlobalVariable", as: .error))

$0 = settings
} (&$0.swiftSettings)
return $0
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for adding Swift settings is overly complex and uses an obscure pattern with an immediately-invoked closure and an inout parameter. This makes the code hard to read and maintain. A more straightforward approach using a guard statement and direct property assignment would be much clearer.

package.targets = package.targets.map {
    guard $0.type != .plugin, $0.type != .binary else {
        return $0
    }

    $0.swiftSettings = ($0.swiftSettings ?? []) + [
        .enableUpcomingFeature("ExistentialAny"),
        .treatWarning("ExistentialAny", as: .error),
        .treatWarning("MutableGlobalVariable", as: .error),
    ]
    return $0
}

@tayloraswift tayloraswift merged commit e5a8ddc into master Jan 19, 2026
8 checks passed
@tayloraswift tayloraswift deleted the double-roundtripping branch January 19, 2026 23:14
tayloraswift added a commit that referenced this pull request Mar 15, 2026
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.

Conform floating points to JSONEncodable. JSON.Node(parsing:) throws an error when a fractional number is too long

1 participant