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

Add native json.Number support #364

Merged
merged 4 commits into from
Oct 1, 2024
Merged

Conversation

klauspost
Copy link
Collaborator

@klauspost klauspost commented Sep 28, 2024

Allow encoding and decoding of json.Number values, either as struct members or as interface members.

Numbers will be encoded as integer, if possible, otherwise float64 is used. The zero value json.Number will be encoded as 0.

It is possible to encode as string with //msgp:replace json.Number with:string.

Fixes #292

Review notes

I think the only contentious part is that it refuses to encode invalid values, which seems reasonable to me, so the output will either be float or integer or an empty string.

Empty string or null, for a "zero value" (not 0)? A bit undecided on this. Open to changes.

Allow encoding and decoding of `json.Number` values, either as struct members or as interface members.

Numbers will be encoded as integer, if possible, otherwise float64 is used. To cover the zero values, write an empty string, but invalid values will return errors when encoded

It is possible to encode as string with `//msgp:replace json.Number with:string`.

Fixes tinylib#292
@klauspost klauspost requested a review from philhofer September 28, 2024 20:22
@philhofer
Copy link
Member

Empty string or null, for a "zero value" (not 0)? A bit undecided on this. Open to changes.

The behavior of encoding/json with json.Number is to use zero as the zero value, so that's probably the best option.

@klauspost
Copy link
Collaborator Author

Good point. Also makes the output more reliable and guaranteed to be a number type.

@klauspost
Copy link
Collaborator Author

Amended, and now will also optionally use float32 as per #365

@klauspost klauspost merged commit 128abdb into tinylib:master Oct 1, 2024
4 checks passed
@klauspost klauspost deleted the add-json-number branch October 1, 2024 06:59
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.

Support json.Number type
2 participants