Skip to content

nested field with json tag 'omitempty' should not be marshalled if it has zero value #219

Closed
@gfremex

Description

@gfremex

Example code like this:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/json-iterator/go"
)

func main() {
	type S1 struct {
		F1 string `json:",omitempty"`
	}

	type S2 struct {
		*S1
		F2 string `json:",omitempty"`
	}

	s1 := &S1{
		//F1: "abc",
	}

	s2 := &S2{
		S1: s1,
		F2: "123",
	}

	b, err := json.Marshal(s2)

	if err != nil {
		panic(err)
	}

	fmt.Println("Marshalled by json:", string(b))

	b, err = jsoniter.Marshal(s2)

	if err != nil {
		panic(err)
	}

	fmt.Println("Marshalled by jsoniter:", string(b))
}

And the output is:

Marshalled by json: {"F2":"123"}
Marshalled by jsoniter: {"F1":"","F2":"123"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions