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

feat: add support for JSON null value #14

Merged
merged 1 commit into from
Oct 19, 2024
Merged

feat: add support for JSON null value #14

merged 1 commit into from
Oct 19, 2024

Conversation

mdawar
Copy link
Contributor

@mdawar mdawar commented Oct 19, 2024

Hey,

This pull request is a suggestion to add support for the JSON null value.
This is needed to prevent parse errors when an API returns a null value for a udecimal.Decimal type.

Other libraries like https://github.com/shopspring/decimal already support this.

Copy link

codecov bot commented Oct 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.82%. Comparing base (5d3d0ff) to head (08d645b).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #14      +/-   ##
==========================================
- Coverage   94.55%   93.82%   -0.73%     
==========================================
  Files           5        5              
  Lines        1267     1653     +386     
==========================================
+ Hits         1198     1551     +353     
- Misses         37       70      +33     
  Partials       32       32              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

// UnmarshalJSON implements the [json.Unmarshaler] interface.
func (d *Decimal) UnmarshalJSON(data []byte) error {
// Remove quotes if they exist.
if len(data) >= 2 && data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}

// null value.
if bytes.Equal(data, nullValue) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The bytes.Equal function is optimized for performance and will perform much better than bytes.Compare.

Copy link
Owner

Choose a reason for hiding this comment

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

I'm good with this. Just add benchmark here for reference

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i9-14900HX
BenchmarkByteEqual-32    	1000000000	         0.1213 ns/op	       0 B/op	       0 allocs/op
BenchmarkByteCmp-32    	247671904	         4.825 ns/op	       0 B/op	       0 allocs/op

@quagmt quagmt merged commit cf139d3 into quagmt:master Oct 19, 2024
9 of 10 checks passed
@mdawar mdawar deleted the null branch October 19, 2024 09:40
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