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

[Bug] inline field Marshal error with "omitempty" tag #460

Open
zeruse opened this issue Aug 7, 2023 · 3 comments
Open

[Bug] inline field Marshal error with "omitempty" tag #460

zeruse opened this issue Aug 7, 2023 · 3 comments

Comments

@zeruse
Copy link

zeruse commented Aug 7, 2023

When an inline field has both "inline" and "omitempty" tag, using Marshal will get an illegal json. The result is correct if the field only has a "inline" tag.

code example:

package main

import (
	"encoding/json"
	"fmt"

	gojson "github.com/goccy/go-json"
)

type A struct {
	Foo string `json:"foo"`
	*B  `json:",inline,omitempty"`
}

type B struct {
	Bar string `json:"bar"`
}

func main() {
	a := A{
		Foo: "fff",
		B: &B{
			Bar: "bbb",
		},
	}
	jsonBytes, _ := json.Marshal(a)
	fmt.Println(string(jsonBytes)) // {"foo":"fff","bar":"bbb"}

	gojsonBytes, _ := gojson.Marshal(a)
	fmt.Println(string(gojsonBytes)) // {"foo":"fff","B":"bar":"bbb"}

}
@zeruse
Copy link
Author

zeruse commented Aug 7, 2023

Using v0.10.2. Maybe related to #116

@dtracers
Copy link

I ran into this bug just now. It happens even if the "inline" part does not exist.

Is there a version of this library that does not have this issue?

I have lots of embedded structs and use omitempty to save on storage space

@zeruse
Copy link
Author

zeruse commented Aug 30, 2023

I ran into this bug just now. It happens even if the "inline" part does not exist.

Is there a version of this library that does not have this issue?

I have lots of embedded structs and use omitempty to save on storage space

I'm using v0.10.2. I just tried an embedded field that only have "omitempty" tag, and the issue existed too.

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

2 participants