Skip to content

Conversation

quinnj
Copy link
Owner

@quinnj quinnj commented Oct 8, 2022

This is the best idea I can come up with to address #232. First a summary of the problem,

  • We allow custom number types to overload StructTypes.numbertype that we convert to before writing JSON
  • However, we only provide JSON writing routines for Integer and AbstractFloat
  • So a number like 3//2 (Rational) or π (Irrational) which are both <: Real, but not Integer or AbstractFloat we get the stack overlfow behavior reported above

The proposal here then is we first check if a Real has overloaded StructTypes.numbertype and if not, we'll use a new JSON3.tostring method, which by default converts the custom number to Float64 and then calls Base.string(x). This method allows custom number types, like FixedPointDecimal, to overload JSON3.tostring to provide an appropriate string representation for their type.

This is the best idea I can come up with to address
#232. First a summary of the problem,
  * We allow custom number types to overload `StructTypes.numbertype` that we
    convert to before writing JSON
  * However, we only provide JSON writing routines for `Integer` and `AbstractFloat`
  * So a number like `3//2` (`Rational`) or `π` (`Irrational`) which are both `<: Real`,
    but not `Integer` or `AbstractFloat` we get the stack overlfow behavior reported above

The proposal here then is we first check if a `Real` has overloaded `StructTypes.numbertype`
and _if not_, we'll use a new `JSON3.tostring` method, which by default converts the custom
number to `Float64` and then calls `Base.string(x)`. This method allows custom number types,
like FixedPointDecimal, to overload `JSON3.tostring` to provide an appropriate string representation
for their type.
@codecov
Copy link

codecov bot commented Oct 8, 2022

Codecov Report

Base: 88.36% // Head: 88.37% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (5646aa8) compared to base (3d542d3).
Patch coverage: 91.66% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #236      +/-   ##
==========================================
+ Coverage   88.36%   88.37%   +0.01%     
==========================================
  Files           9        9              
  Lines        1710     1721      +11     
==========================================
+ Hits         1511     1521      +10     
- Misses        199      200       +1     
Impacted Files Coverage Δ
src/write.jl 74.64% <91.66%> (+0.90%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@iamed2
Copy link

iamed2 commented Oct 11, 2022

I think this is a good solution! It's a bit unfortunate that a numerical package will need to depend on JSON3 but I think that's just something to think about in future designs/refactors and I'd be happy to add the dependency in for FixedPointDecimals for now.

@quinnj quinnj merged commit 3bac427 into main Oct 20, 2022
@quinnj quinnj deleted the jq/reals branch October 20, 2022 19:50
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