Skip to content

Adding support for config omitEmpty which can be used instead of setting omitEmpty on individual fields #445

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
ValidateJsonRawMessage bool
ObjectFieldMustBeSimpleString bool
CaseSensitive bool
OmitEmpty bool
}

// API the public interface of this package.
Expand Down Expand Up @@ -80,6 +81,7 @@ type frozenConfig struct {
streamPool *sync.Pool
iteratorPool *sync.Pool
caseSensitive bool
omitEmpty bool
}

func (cfg *frozenConfig) initCache() {
Expand Down Expand Up @@ -134,6 +136,7 @@ func (cfg Config) Froze() API {
onlyTaggedField: cfg.OnlyTaggedField,
disallowUnknownFields: cfg.DisallowUnknownFields,
caseSensitive: cfg.CaseSensitive,
omitEmpty: cfg.OmitEmpty,
}
api.streamPool = &sync.Pool{
New: func() interface{} {
Expand Down
2 changes: 1 addition & 1 deletion reflect_struct_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (encoder *structEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteObjectStart()
isNotFirst := false
for _, field := range encoder.fields {
if field.encoder.omitempty && field.encoder.IsEmpty(ptr) {
if (stream.cfg.omitEmpty || field.encoder.omitempty) && field.encoder.IsEmpty(ptr) {
continue
}
if field.encoder.IsEmbeddedPtrNil(ptr) {
Expand Down