go icon indicating copy to clipboard operation
go copied to clipboard

OmitDefault implement with extension

Open luweimy opened this issue 4 years ago • 2 comments

ignore json tag omitempty, can output empty field

// OmitDefaultExtension 忽略json tag的omitempty
type OmitDefaultExtension struct {
	jsoniter.DummyExtension
}

// DecorateEncoder 修饰编码器
func (extension *OmitDefaultExtension) DecorateEncoder(typ reflect2.Type, encoder jsoniter.ValEncoder) jsoniter.ValEncoder {
	return omitDefaultEncoder{encoder}
}

type omitDefaultEncoder struct {
	jsoniter.ValEncoder
}

func (o omitDefaultEncoder) IsEmpty(ptr unsafe.Pointer) bool {
	return false
}

My Question

请问这样实现有问题吗,我试了下是输出是符合要求的,但是不清楚IsEmpty全部返回false有没有副作用。 我搜了下代码IsEmpty,field.encoder.omitempty && field.encoder.IsEmpty(ptr)只有这样用的,看着应该没有问题,还是想确认下才安心。

Is there any problem with this implementation? I have tried and found that the output meets the requirements, but I am not sure whether IsEmpty returns false or not. I searched the code IsEmpty, 'field.encoder Field. Encoder.IsEmpty(PTR) 'only in this way, look should be no problem, or want to confirm the peace of mind

luweimy avatar Oct 03 '21 01:10 luweimy

想咨询一下老哥 后续使用有没有副作用呢?

wulorn avatar Sep 18 '22 06:09 wulorn

想咨询一下老哥 后续使用有没有副作用呢?

还没发现啥副作用

luweimy avatar Sep 19 '22 02:09 luweimy