On Aug 9, 2019, at 17:53, Greg Ewing <greg.ew...@canterbury.ac.nz> wrote: > > If you really insist on being strict, it could require you to > return a special wrapper type that takes a string of digits and > checks that it conforms to the syntax of a JSON number. > > Come to think of it... you could use Decimal as that wrapper > type!
No you can’t. Decimal accepts strings that aren’t valid as JSON numbers, like `.3`, or `nan`, just as float does and C’s atoi, etc. (And it accepts strings that are valid as JSON numbers, but not recommended for interoperability, like `1.0E999`.) If you want to know whether something can be parsed as a JSON number, the obvious thing to do is call json.loads on it. (And if you want to know whether it’s something that’s “interoperable”, check whether loads returns a float—and maybe whether that float is equal to your input.) It’s not exactly the hardest thing to parse in the world, but why duplicate the parser (and potentially create new bugs) if you already have one? _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/7FEUBOEK5BFPLL7IJ5BG3XHOZU4R2FWW/ Code of Conduct: http://python.org/psf/codeofconduct/