Code Monkey home page Code Monkey logo

Comments (5)

boring-cyborg avatar boring-cyborg commented on July 18, 2024

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

from airflow.

potiuk avatar potiuk commented on July 18, 2024

Json does not make a difference between float and ints. https://www.json.org/json-en.html - this is just number. There is no way you can distinguish the two 18.0 when parsed is just 18 and that's how it is printed. Those two are equivalent and you can't do anything about it.

from airflow.

potiuk avatar potiuk commented on July 18, 2024

BTW. If you want to preserve ".0" and it makes a difference for you - you should store it as string and expect string in the value.

from airflow.

ThePsyjo avatar ThePsyjo commented on July 18, 2024
>>> import json
>>> json.dumps(18.0)
'18.0'
>>> json.loads(json.dumps(18.0))
18.0

The json module does preserve fractions though. As described earlier, the fraction is stored correctly. It is only converted to int when loaded by the web interface.

BTW. If you want to preserve ".0" and it makes a difference for you - you should store it as string and expect string in the value.

I implemented this already to get my stuff working reliably again :)

Obviously it is JS doing the ugly stuff

> JSON.stringify(18.0)
'18'

from airflow.

potiuk avatar potiuk commented on July 18, 2024

What you are referring to is an implementation detail of Python JSON parser - when you are using Python dictionaries / variables you have indeed different types of fields - float and int are different. And the dump method of the parser might choose to add '.0' in this case.

But JSON specification is very clear. Any number that you pass in json is a "number" and JSON specification does not mandate or even hint, whether floating points should be preserved if they are .0. Simply 18.0 and 18 are different representation of the same number and json parser and producer has absolutely no expectation and requirement the .0 will be preserved when serializing/deserializing JSON. So JS does not do ugly stuff. It acts according to the specification. And you should never rely on the fact that .0 is preserved when you go through serialize/deserialize because it's not guaranteed or not even hinted that it should be preserved.

BTW. I am not sure if you are aware but J in JSON stands for Javascript because that's where JSON was originally used, so I'd argue that Python implementation diverged from what original JSON implementation did.

from airflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.