Skip to content

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

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

Closed
gfremex opened this issue Jan 6, 2018 · 0 comments

Comments

@gfremex
Copy link

gfremex commented Jan 6, 2018

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"}

@taowen taowen closed this as completed in 0ab8806 Jan 7, 2018
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
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