Skip to content
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

Coerce All Numbers to Floats to Maintain Consistency Between JS, JSON Schema, and Python #405

Merged
merged 6 commits into from
Sep 29, 2023

Conversation

garrettmflynn
Copy link
Member

fix #399 and #400

obj[key] = math.nan
obj[key] = math.nan # Turn None into NaN if a number is expected (JavaScript JSON.stringify turns NaN into None)
elif prop_schema.get("type") == "number" and isinstance(value, int):
obj[key] = float(value) # Turn integer into float if an integer is expected (JavaScript / JSON Schema coerces floats with trailing zeros to integers)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the comment be 'if a float is expected'?

Also is it possible to auto-detect from the presence of a . in the stringified number whether it ought to be float or int?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes my bad. I'd written this before I looked up that number is equivalent to float in JSON Schema.

Theoretically yes—but that'd require us to roll our own JSON parser in addition to storing this information together with the parsed value (as JS has no way to differentiate 20 and 20.0) until it's re-stringified for storage / use by Python.

@CodyCBakerPhD CodyCBakerPhD merged commit 2ddd10f into main Sep 29, 2023
6 checks passed
@CodyCBakerPhD CodyCBakerPhD deleted the coerce-numbers-to-floats branch September 29, 2023 15:31
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