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

encoding/json: marshall dont work on any lowercased field in struct #58956

Closed
ErwanMAS opened this issue Mar 10, 2023 · 2 comments
Closed

encoding/json: marshall dont work on any lowercased field in struct #58956

ErwanMAS opened this issue Mar 10, 2023 · 2 comments

Comments

@ErwanMAS
Copy link

the field name is excluded from the result because start with a lower case .

type ColorGroup struct {
		ID     int
		name   string
		Colors []string
	}

Does this issue reproduce with the latest release?

yes

What did you do?

a example , 99% inspired by the example from your documentation

https://go.dev/play/p/ZaxfyRsRg_g

What did you expect to see?

{"ID":1,"name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}

What did you see instead?

json result has only 2 fields

{"ID":1,"Colors":["Crimson","Red","Ruby","Maroon"]}

this issue is so obvious , that i think i miss a word in this documentation https://pkg.go.dev/encoding/json#Marshal
but i read twice , and i did not anything

@ErwanMAS
Copy link
Author

I just found this rule https://go.dev/ref/spec#Exported_identifiers , so it seem to apply to json marshalling .

Can you modify the documentation of marshall to add small few words about this ?

@rittneje
Copy link

It already mentions this.

Struct values encode as JSON objects. Each exported struct field becomes a member of the object, using the field name as the object key, unless the field is omitted for one of the reasons given below.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2023
@golang golang locked and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants