Anonymous embedded struct produces invalid JSON: "go build -tags=go_json " #4461
zhuzaiye
started this conversation in
Show and tell
Replies: 1 comment
-
|
This is a known issue with The problem:
Solutions: 1. Remove the anonymous embedding (recommended): type skillUpResLabel struct {
ClassId int `json:"class_id"`
ClassName string `json:"class_name"`
// ... other fields
// Instead of anonymous embed, use named field:
StudyLabel skillUpStudyLabel `json:"study_label,omitempty"`
}Or flatten manually: type skillUpResLabel struct {
ClassId int `json:"class_id"`
HasPartList bool `json:"has_part_list"`
PartItems []*skillUpResLabel `json:"part_items"`
ResourceItems []*skillUpResource `json:"resource_items"`
}2. Use standard library JSON: Build without the go build ./... # no tags3. Use sonic instead: go build -tags=sonic ./...This is a goccy/go-json bug. You might want to report it to their repo if not already filed. Anonymous struct embedding with circular references is tricky for all JSON libs. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Warning: go build -tags=go_json
Code example (minimal reproducible)
Expected JSON output
{ "class_id": 150301, "class_name": "PreA1", "percent_rate": 0, "is_completed": false, "has_part_list": false, "part_items": [], "resource_items": [...] }Actual JSON output
macOS build produces invalid JSON, where the anonymous embedded struct is emitted as an object without a key:
{ "class_id": 150301, "class_name": "PreA1", "percent_rate": 0, "is_completed": false, { "has_part_list": false, "part_items": [], "resource_items": [ ... ] } }Environment
Build script:
Beta Was this translation helpful? Give feedback.
All reactions