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

Fix: std.json: Writing inf values returns InvalidJSON error #23258

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

bradcypert
Copy link

I believe this should fix #23253. First time contributor here, so please feel free to be as thorough as you'd like with feedback.

Abstract: Writing an infinite float in JSON produces the value inf which is not valid JSON. Since we can not convert an infinite value to JSON, introduce a new error type named InvalidJson and return that when attempting to write a value that can not be converted to valid JSON.

@bradcypert
Copy link
Author

bradcypert commented Mar 16, 2025

It looks like one of the errors in CI is related to this: #22107 . Anyone with more experience working on the std lib have any thoughts? I can cast the comptime floats to regular floats before the checks, but perhaps isInf is intended to also support comptime floats? I suppose I could also split the float and comptime float checks into two separate blocks in the switch, too.

@richard-powers
Copy link

#22107 (comment)
Seems it intentionally doesn't support comptime floats

Copy link
Collaborator

@linusg linusg left a comment

Choose a reason for hiding this comment

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

  • You'll have to update the error sets of all stringify functions, not just stringifyAlloc
  • The doc comment bulletpoint Zig floats -> JSON number or string. should be updated to mention this behavior
  • error.InvalidJson does not accurately describe the failure - this is serialization to JSON and the input value is invalid, not the other way around

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.

JSON stringify returning invalid token inf
5 participants