go icon indicating copy to clipboard operation
go copied to clipboard

private field doesn't work if tagged

Open quintans opened this issue 3 years ago • 0 comments

I noticed that when we enable support for private fields and if we add a json tag, the field is no longer marshalled/unmarshalled.

bellow is an example

	type Foo struct {
		name string `json:"name"`
	}

	extra.SupportPrivateFields()
	wantedOutput := `{"name":"Paulo"}`
	foo := Foo{name: "Paulo"}
	b, err := jsoniter.Marshal(foo)
	output := string(b)
	if err != nil {
		fmt.Println("failed to marshal:", err)
	} else if output != wantedOutput {
		fmt.Printf("wanted '%s', got '%s'\n", wantedOutput, output)
	} else {
		fmt.Println("wanted output matches")
	}

this is easily fixed by deleting the following lines https://github.com/json-iterator/go/blob/master/extra/privat_fields.go#L48-L52

quintans avatar Feb 28 '22 10:02 quintans