json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.42k stars 1.03k forks source link

private field doesn't work if tagged #605

Open quintans opened 2 years ago

quintans commented 2 years ago

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