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.33k stars 1.02k forks source link

RegisterTypeEncoderFunc isEmptyFunc never called #661

Open kimmykong opened 1 year ago

kimmykong commented 1 year ago

When I pass an isEmptyFuncfunction to RegisterTypeEncoderFunc, my isEmptyFuncis never called. My EncoderFuncfor the type does get called. Request is for the isEmptyFuncto be called.

My data looks like this, where TypeCis an array that can have empty bytes saved. When I use jsoniter.RegisterFieldEncoderFunc(reflect2.TypeOf(TypeB{}).String(), "TypeC", encodeB, isBEmpty), both encodeBand isBEmpty functions get called, but I lose the support for array type and can't parse out the empty []byte

type TypeA struct {
    TypeB TypeB
}

type TypeB struct {
    TypeC []TypeC `msgpack:"as_array" json:"omitempty"`
}

type TypeC []byte

func fun() {
    jsoniter.RegisterTypeEncoderFunc(reflect2.TypeOf(TypeC{}).String(), encodeFunc, isEmptyFunc)
}