mirror of https://github.com/goccy/go-json.git
Fix double pointer
This commit is contained in:
parent
ee52d7f0ae
commit
59f5713178
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue