Is json.dumps a guarantee not to lose floating point precision?

I am talking about JSON conversion, for example:

>>> a = {'asas': 1/7.0} >>> b = json.dumps(a) >>> c = json.loads(b) >>> c {u'asas': 0.14285714285714285} >>> c['asas'] == 1.0/7 True 

Is JSON encoding a guarantee not to round a number?

In my How to save a floating point number as text without loss of precision? Mark Dickinson says that repr does not result in a loss of accuracy. Does json.dumps repr ?

+6
source share
1 answer

json doesn't mention repr , but this is the current implementation of coercion float-to-string:

 FLOAT_REPR = repr 

( Lib/json/encoder.py , line 31 )

You can create your own JSONEncoder if you need a strict guarantee.

+6
source

Source: https://habr.com/ru/post/918547/


All Articles