Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for decimal_class: BigDecimal #526

Closed
wants to merge 5 commits into from

Conversation

notEthan
Copy link
Contributor

I think it is excellent that it is so easy to preserve arbitrary numeric precision parsing JSON with decimal_class: BigDecimal. This currently has a couple of deficiencies, though.

  • Not round-trippable: currently JSON.generate will represent BigDecimal as a string. I'm not sure why this is, when it can be encoded perfectly as a number.
  • Obscure documentation - it's not mentioned on the ::JSON module or on the default (ext) parser. It is mentioned on the pure and java parsers.

This PR adds documentation where missing, and changes the generation to be a number, as seems much more natural.

I also split json_addition_test.rb to its own run because require 'json/add/bigdecimal' would override BigDecimal#to_json.

@notEthan notEthan marked this pull request as draft May 26, 2023 21:30
@notEthan
Copy link
Contributor Author

The implementation for c ext is in progress, and I'm not sure about implementing a java version. I wanted to put this up as a draft to solicit feedback while I address those things.

@notEthan
Copy link
Contributor Author

notEthan commented May 26, 2023

I added a c ext implementation but I am very inexperienced with c ruby extensions and it could probably be better. e.g. if it were to #include "bigdecimal.h" then generate_json could use klass == rb_cBigDecimal and the generate_json_bigdecimal nan/infinity checks could use VpIsNaN / VpIsInf, but I'm not sure if I can include bigdecimal.h. I will wait and see about feedback before I try to improve it further or even think about java.

@byroot
Copy link
Member

byroot commented Nov 5, 2024

I think this had too many incompatibility concerns. I'd recommend just defining a custom decimal_class that behave as you desire.

@byroot byroot closed this Nov 5, 2024
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