Skip to content

Clean nested string-keyed maps before JSON encoding - #190

Merged
hanzei merged 2 commits into
stretchr:masterfrom
yuefdev:fix/json-nested-string-map-cleanup-sol
Oct 20, 2026
Merged

hanzei merged 2 commits into
stretchr:masterfrom
yuefdev:fix/json-nested-string-map-cleanup-sol

Conversation

@yuefdev

@yuefdev yuefdev commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Map.JSON() cleans nested interface-keyed maps, but skips them when they sit inside an ordinary map[string]any. A YAML-style boolean key then causes JSON encoding to fail instead of being converted to a string.

Recurse through string-keyed maps too. The regression covers a direct map and one inside a mixed array, and failed before the fix.

Full tests and go vet ./... pass on Windows/Go 1.27. Changed-code golangci-lint passes; the full lint run reports existing gofmt differences in accessors.go, doc.go and map.go. Race testing is blocked locally because cgo is disabled.

@hanzei hanzei left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

Suggestion: have JSON() clean a copy instead of the receiver

JSON() writes the cleaned values back into the map it was called on (m[k] = cleanUp(v)). This PR makes that more visible: a top-level map[string]any value becomes an objx.Map once JSON(), MustJSON() or Base64() runs.

m := objx.Map{"config": map[string]any{"a": 1}}
_, _ = m.JSON()
_, ok := m["config"].(map[string]any)
// before this PR: ok == true
// after this PR:  ok == false (m["config"] is objx.Map)

@yuefdev

yuefdev commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@hanzei I've changed JSON() to marshal a cleaned copy rather than write back into the receiver (89e0a9e). The new regression checks that JSON(), MustJSON() and Base64() preserve the original nested map types and contents. All three cases fail before this change and pass afterwards.

go test ./... and go vet ./... pass locally. Race testing is blocked here because cgo is disabled, and golangci-lint isn't installed.

@hanzei hanzei left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@hanzei
hanzei merged commit 2947651 into stretchr:master Oct 20, 2026
4 checks passed
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