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

Invalid JSON produced when embedding structs (inline) #486

Open
antonsacred opened this issue Nov 22, 2023 · 0 comments
Open

Invalid JSON produced when embedding structs (inline) #486

antonsacred opened this issue Nov 22, 2023 · 0 comments

Comments

@antonsacred
Copy link

Case 1

type Structure struct {
	Id int `json:"id"`
	*Structure
}

func TestIssueWithInlineStruct(t *testing.T) {
	obj := Structure{
		Id: 99,
	}

	b, err := json.Marshal(obj)
	assertErr(t, err)
	assertEq(t, "key a", `{"id":99,null}`, string(b))

	// compare with standard library
	b, err = stdjson.Marshal(obj)
	assertErr(t, err)
	assertEq(t, "key b", `{"id":99}`, string(b))
}

Case 2

type AlternativeStructure struct {
	Id int `json:"id"`
	*Structure
}

type Structure struct {
	Id          int                  `json:"id"`
	Alternative AlternativeStructure `json:"alternative"`
}

func TestIssueWithInlineStruct(t *testing.T) {
	obj := Structure{
		Id:          99,
		Alternative: AlternativeStructure{Id: 123},
	}

	b, err := json.Marshal(obj)
	assertErr(t, err)
	assertEq(t, "key a", `{"id":99,"alternative":{"id":123,null}}`, string(b))

	// compare with standard library
	b, err = stdjson.Marshal(obj)
	assertErr(t, err)
	assertEq(t, "key b", `{"id":99,"alternative":{"id":123}}`, string(b))
}

When structure in embedded to itself go-json produce invalid json {"id":123,null}

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

No branches or pull requests

1 participant