diff --git a/encode_compile.go b/encode_compile.go index 7fc9915..d39f363 100644 --- a/encode_compile.go +++ b/encode_compile.go @@ -20,6 +20,7 @@ func (e *Encoder) compileHead(ctx *encodeCompileContext) (*opcode, error) { return e.compileMarshalTextPtr(ctx) } isPtr := false + orgType := typ if typ.Kind() == reflect.Ptr { typ = typ.Elem() isPtr = true @@ -28,6 +29,10 @@ func (e *Encoder) compileHead(ctx *encodeCompileContext) (*opcode, error) { return e.compileMap(ctx.withType(typ), isPtr) } else if typ.Kind() == reflect.Struct { return e.compileStruct(ctx.withType(typ), isPtr) + } else if isPtr && typ.Implements(marshalTextType) { + typ = orgType + } else if isPtr && typ.Implements(marshalJSONType) { + typ = orgType } return e.compile(ctx.withType(typ)) } @@ -112,12 +117,8 @@ func (e *Encoder) compile(ctx *encodeCompileContext) (*opcode, error) { func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) { typ := ctx.typ switch { - case typ.Implements(marshalJSONType): - return e.compileMarshalJSON(ctx) case rtype_ptrTo(typ).Implements(marshalJSONType): return e.compileMarshalJSONPtr(ctx) - case typ.Implements(marshalTextType): - return e.compileMarshalText(ctx) case rtype_ptrTo(typ).Implements(marshalTextType): return e.compileMarshalTextPtr(ctx) } diff --git a/encode_vm.go b/encode_vm.go index a3b5768..c41c306 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -402,14 +402,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { isPtr := code.typ.Kind() == reflect.Ptr p := e.ptrToUnsafePtr(ptr) if p == nil { - e.encodeNull() - e.encodeByte(',') + e.encodeBytes([]byte{'"', '"', ','}) } else if isPtr && *(*unsafe.Pointer)(p) == nil { e.encodeBytes([]byte{'"', '"', ','}) } else { - if isPtr && code.typ.Elem().Implements(marshalTextType) { - p = *(*unsafe.Pointer)(p) - } v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ typ: code.typ, ptr: p,