Skip to content

Threshold: Fix JSON marshal/unmarshal thresholds with NaN and zero values#1497

Merged
ryantxu merged 4 commits intomainfrom
threshold-marshal-unmarshal
Mar 9, 2026
Merged

Threshold: Fix JSON marshal/unmarshal thresholds with NaN and zero values#1497
ryantxu merged 4 commits intomainfrom
threshold-marshal-unmarshal

Conversation

@ryantxu
Copy link
Contributor

@ryantxu ryantxu commented Feb 28, 2026

See #1447, #1496 and #1466


// Threshold a single step on the threshold list
type Threshold struct {
Value ConfFloat64 `json:"value,omitempty"` // First value is always -Infinity serialize to null
Copy link
Contributor Author

Choose a reason for hiding this comment

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

omit empty was dropping zero -- and then it woudl come back as -inf

math.IsNaN(float64(*sf)) ||
math.IsInf(float64(*sf), -1) ||
math.IsInf(float64(*sf), +1) {
func (sf ConfFloat64) MarshalJSON() ([]byte, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

switched from pointer receiver to value reciever -- the logic was skipped for Thresholds because they are not pointers 🤦🏻

out, err := json.Marshal([]data.Threshold{tc.before})
require.NoError(t, err)

arr := []data.Threshold{}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@roosenj -- this test replicates your issue (i think!)

Copy link

Choose a reason for hiding this comment

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

Great, thanks for looking into this again! I notice you're testing with the builtin encoding/json, from what I saw the SDK uses json-iterator/go instead as its JSON parser, is the marshaling logic for these two libraries the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i updated the test for both json.Marshal and jsoniter.Marshal

@grafana-plugins-platform-bot grafana-plugins-platform-bot bot moved this from 📬 Triage to 🔬 In review in Grafana Catalog Team Mar 2, 2026
Copy link
Contributor

@gabor gabor left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@ryantxu ryantxu merged commit f4bfcf0 into main Mar 9, 2026
9 checks passed
@ryantxu ryantxu deleted the threshold-marshal-unmarshal branch March 9, 2026 12:16
@github-project-automation github-project-automation bot moved this from 🔬 In review to 🚀 Shipped in Grafana Catalog Team Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🚀 Shipped

Development

Successfully merging this pull request may close these issues.

3 participants