-
Notifications
You must be signed in to change notification settings - Fork 528
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
adds as_str
to Number
if arbitrary_precision
is enabled
#1067
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty different from the intended usage of
as_str()
, right? I don't see why one would use as_str in this manner. From #1066 I'd inferred it would be to inspect the original formatting of a number that was parsed from a JSON document using serde_json::from_str.I think showing that representative usage would make a more compelling example.
I've learned the value of method-level example code like this is chiefly to illustrate why one would want to call it, as opposed to how to call a method in Rust, which the reader can be assumed to know by the time they are reading serde_json docs. (Yes, most of the existing examples are bad and old.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes total sense sense. Thank you for the wisdom; I'm horrible at documentation and I'm definitely trying to get better at it. The rust ecosystem has certainly raised the bar on such.
I hope the example I just committed is more in line with what you are thinking. Please let me know if there's more I can do to improve it.
edit: removed comment about an example with a leading 0 returning an error. The JSON diagram on numbers confused me; it seemed like leading 0s were allowed. However, the spec explicitly states "A number is a sequence of decimal digits with no superfluous leading zero."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated example is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I personally need
as_str
is to avoid an allocation viato_string
.For a bit of context, I'm working on a JSON Schema crate which evaluates
serde_json::Value
s. In order to properly compare decimals and also to support big numeric types, I have written parsers to convert the string value ofNumber
tonum::BigRational
andnum::BigInt
.I hope the example illustrates the utility of having access to the underlying string. If others need to compare fractions or deal with big numbers,
"arbitrary_precision"
with a custom string parser is, so far as I can tell, the only viable path to achieving said goal.