Skip to content

Json numeric type design#120

Closed
simbo1905 wants to merge 7 commits intomainfrom
cursor/json-numeric-type-design-bc5e
Closed

Json numeric type design#120
simbo1905 wants to merge 7 commits intomainfrom
cursor/json-numeric-type-design-bc5e

Conversation

@simbo1905
Copy link
Owner

What changed

  • Added DESIGN_CHOICES.md to explain the rationale behind JsonNumber's design, particularly regarding BigDecimal/BigInteger constructors.
  • Updated README.md to link to the new design document.
  • Added runnable Java examples (DesignChoicesExamples.java) and corresponding JUnit tests (DesignChoicesNumberExamplesTest.java) demonstrating the recommended approach for numeric handling.

Why this change is needed

This change clarifies why BigDecimal/BigInteger constructors were removed from JsonNumber during an upstream sync. The upstream design prioritizes:

  1. Lexical preservation: JSON numbers are treated as arbitrary precision text.
  2. Avoiding policy decisions: Adding BigDecimal/BigInteger factories would force formatting policies (e.g., toString() vs toPlainString()) into the core API.
  3. Minimal factories: Aligning with the API's preference for a few sharp factory methods over an explosion of constructor overloads.

The new documentation and examples provide clear guidance on how to perform lossless numeric interoperability using JsonNumber.toString() and BigDecimal/BigInteger explicitly.

How were these changes tested

  • New JUnit tests (DesignChoicesNumberExamplesTest.java) were added to verify the examples and design principles.
  • The module was built and tested using $(command -v mvnd || command -v mvn || command -v ./mvnw) -pl json-java21 test -Djava.util.logging.ConsoleHandler.level=INFO, which reported BUILD SUCCESS.

Checklist

  • Code builds / passes tests
  • New tests added if needed
  • Update to use CODING_STYLE_LLM.md convensions
  • Documentation updated if needed
  • AGENTS.md updated if appropriate

Open in Cursor Open in Web

cursoragent and others added 2 commits January 31, 2026 15:30
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
@cursor
Copy link

cursor bot commented Jan 31, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@gemini-code-assist
Copy link

Summary of Changes

Hello @simbo1905, 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 clarifies the design philosophy for handling numeric types within the JsonNumber API. It provides comprehensive documentation and practical examples to explain why direct BigDecimal/BigInteger constructors were removed, advocating for a 'text-first' approach to preserve lexical representation and avoid embedding formatting policies into the core API. The changes aim to guide developers in achieving lossless numeric interoperability using explicit conversions, thereby enhancing the API's consistency and maintainability.

Highlights

  • New Design Documentation: A new file, DESIGN_CHOICES.md, has been added to explain the rationale behind JsonNumber's design, specifically addressing the removal of BigDecimal/BigInteger constructors and emphasizing the 'text-first' approach for JSON numbers.
  • Updated README: The README.md file has been updated to include a link to the newly created DESIGN_CHOICES.md under a new 'Design notes' section, making the design considerations easily discoverable.
  • Illustrative Examples and Tests: New runnable Java examples (DesignChoicesExamples.java) and corresponding JUnit tests (DesignChoicesNumberExamplesTest.java) have been added to demonstrate the recommended approach for lossless numeric handling and to verify the design principles outlined in the documentation.
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 is a valuable addition, providing clear documentation and examples for the numeric handling design choices in JsonNumber. The new DESIGN_CHOICES.md file effectively explains the rationale for prioritizing lexical representation and avoiding BigDecimal/BigInteger constructors in the core API. The accompanying examples and tests are well-written and successfully demonstrate the recommended practices. My feedback includes a couple of suggestions to improve the clarity and consistency of the example code, making it even easier for developers to understand.

Comment on lines 50 to 58
LOGGER.info("Executing bigDecimalToJsonNumberRequiresChoosingATextPolicy");

var thousand = new BigDecimal("1000");

var plain = JsonNumber.of(thousand.toPlainString());
assertThat(plain.toString()).isEqualTo("1000");

var scientific = JsonNumber.of(new BigDecimal("1E+3").toString());
assertThat(scientific.toString()).isEqualTo("1E+3");

Choose a reason for hiding this comment

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

medium

The logic in this test can be made clearer and more consistent. The first part of the test defines a BigDecimal variable thousand, but the second part creates a BigDecimal inline. For better readability and to more clearly demonstrate the policies, consider defining separate, descriptively named variables for both cases. This makes the intent of each part of the test more explicit.

        LOGGER.info("Executing bigDecimalToJsonNumberRequiresChoosingATextPolicy");

        // Using toPlainString() for a plain number representation
        var bdPlain = new BigDecimal("1000");
        var plain = JsonNumber.of(bdPlain.toPlainString());
        assertThat(plain.toString()).isEqualTo("1000");

        // Using toString() which may produce scientific notation
        var bdScientific = new BigDecimal("1E+3");
        var scientific = JsonNumber.of(bdScientific.toString());
        assertThat(scientific.toString()).isEqualTo("1E+3");

Comment on lines 42 to 52
public static JsonNumber bigDecimalToJsonNumberChooseTextPolicy() {
var bd = new BigDecimal("1000");

var plain = JsonNumber.of(bd.toPlainString());
System.out.println("BigDecimal.toPlainString() -> JsonNumber: " + plain);

var scientific = JsonNumber.of(new BigDecimal("1E+3").toString());
System.out.println("BigDecimal.toString() -> JsonNumber: " + scientific);

return plain;
}

Choose a reason for hiding this comment

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

medium

This example method can be made clearer and more consistent. The first part defines a BigDecimal variable bd, but the second part creates a BigDecimal inline. To improve readability and better illustrate the different text policies, it would be beneficial to use separate, descriptively named variables for both the "plain" and "scientific" cases.

    public static JsonNumber bigDecimalToJsonNumberChooseTextPolicy() {
        // Example with toPlainString() to avoid scientific notation.
        var bdPlain = new BigDecimal("1000");
        var plain = JsonNumber.of(bdPlain.toPlainString());
        System.out.println("BigDecimal.toPlainString() -> JsonNumber: " + plain);

        // Example with toString(), which may use scientific notation.
        var bdScientific = new BigDecimal("1E+3");
        var scientific = JsonNumber.of(bdScientific.toString());
        System.out.println("BigDecimal.toString()      -> JsonNumber: " + scientific);

        return plain;
    }

cursoragent and others added 3 commits January 31, 2026 16:01
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
@simbo1905 simbo1905 marked this pull request as ready for review January 31, 2026 17:05
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0903da3ef8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +123 to +125
case JsonArray a -> a.elements().stream().map(DesignChoicesExamples::toNative).toList();
case JsonObject o -> o.members().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> toNative(e.getValue())));

Choose a reason for hiding this comment

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

P2 Badge Handle JsonNull values when collecting object members

toNative returns null for JsonNull, but the Collectors.toMap(...) call rejects null values and will throw a NullPointerException when a JSON object contains a null member value. This makes the documented “Map/List/primitives” recipe fail on valid JSON like {"a": null}. Consider collecting into a map that tolerates null values (e.g., manual HashMap population) or mapping JsonNull to a sentinel instead of null.

Useful? React with 👍 / 👎.

cursoragent and others added 2 commits January 31, 2026 17:09
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
@simbo1905 simbo1905 closed this Jan 31, 2026
@simbo1905 simbo1905 deleted the cursor/json-numeric-type-design-bc5e branch January 31, 2026 23:52
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.

2 participants