mirror of https://github.com/goccy/go-json.git
Merge pull request #50 from goccy/feature/fix-pointer-fields
Fix encoding of pointer fields ( primitive type )
This commit is contained in:
commit
d373a4b7d7
|
@ -277,6 +277,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
"StructFieldPtrAnonymousHeadOmitEmpty",
|
||||
"StructFieldPtrAnonymousHeadStringTag",
|
||||
"StructField",
|
||||
"StructFieldPtr",
|
||||
"StructFieldOmitEmpty",
|
||||
"StructFieldStringTag",
|
||||
} {
|
||||
|
|
|
@ -28,11 +28,13 @@ func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
|
|||
|
||||
func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) {
|
||||
header := (*interfaceHeader)(unsafe.Pointer(&key))
|
||||
d.dummy = header
|
||||
return d.keyDecoder.decode(buf, cursor, uintptr(header.ptr))
|
||||
}
|
||||
|
||||
func (d *mapDecoder) setValue(buf []byte, cursor int64, key interface{}) (int64, error) {
|
||||
header := (*interfaceHeader)(unsafe.Pointer(&key))
|
||||
d.dummy = header
|
||||
return d.valueDecoder.decode(buf, cursor, uintptr(header.ptr))
|
||||
}
|
||||
|
||||
|
|
2542
decode_test.go
2542
decode_test.go
File diff suppressed because it is too large
Load Diff
|
@ -481,8 +481,39 @@ func (e *Encoder) typeToHeaderType(op opType) opType {
|
|||
return opStructFieldHead
|
||||
}
|
||||
|
||||
func (e *Encoder) typeToFieldType(op opType) opType {
|
||||
switch op {
|
||||
func (e *Encoder) typeToFieldType(code *opcode) opType {
|
||||
switch code.op {
|
||||
case opPtr:
|
||||
switch code.next.op {
|
||||
case opInt:
|
||||
return opStructFieldPtrInt
|
||||
case opInt8:
|
||||
return opStructFieldPtrInt8
|
||||
case opInt16:
|
||||
return opStructFieldPtrInt16
|
||||
case opInt32:
|
||||
return opStructFieldPtrInt32
|
||||
case opInt64:
|
||||
return opStructFieldPtrInt64
|
||||
case opUint:
|
||||
return opStructFieldPtrUint
|
||||
case opUint8:
|
||||
return opStructFieldPtrUint8
|
||||
case opUint16:
|
||||
return opStructFieldPtrUint16
|
||||
case opUint32:
|
||||
return opStructFieldPtrUint32
|
||||
case opUint64:
|
||||
return opStructFieldPtrUint64
|
||||
case opFloat32:
|
||||
return opStructFieldPtrFloat32
|
||||
case opFloat64:
|
||||
return opStructFieldPtrFloat64
|
||||
case opString:
|
||||
return opStructFieldPtrString
|
||||
case opBool:
|
||||
return opStructFieldPtrBool
|
||||
}
|
||||
case opInt:
|
||||
return opStructFieldInt
|
||||
case opInt8:
|
||||
|
@ -553,8 +584,8 @@ func (e *Encoder) optimizeStructHeader(op opType, tag *structTag, withIndent boo
|
|||
return headType
|
||||
}
|
||||
|
||||
func (e *Encoder) optimizeStructField(op opType, tag *structTag, withIndent bool) opType {
|
||||
fieldType := e.typeToFieldType(op)
|
||||
func (e *Encoder) optimizeStructField(code *opcode, tag *structTag, withIndent bool) opType {
|
||||
fieldType := e.typeToFieldType(code)
|
||||
switch {
|
||||
case tag.isOmitEmpty:
|
||||
fieldType = fieldType.fieldToOmitEmptyField()
|
||||
|
@ -627,7 +658,7 @@ func (e *Encoder) structHeader(ctx *encodeCompileContext, fieldCode *opcode, val
|
|||
|
||||
func (e *Encoder) structField(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
|
||||
code := (*opcode)(unsafe.Pointer(fieldCode))
|
||||
op := e.optimizeStructField(valueCode.op, tag, ctx.withIndent)
|
||||
op := e.optimizeStructField(valueCode, tag, ctx.withIndent)
|
||||
fieldCode.op = op
|
||||
switch op {
|
||||
case opStructField,
|
||||
|
|
1172
encode_optype.go
1172
encode_optype.go
File diff suppressed because it is too large
Load Diff
479
encode_vm.go
479
encode_vm.go
File diff suppressed because it is too large
Load Diff
|
@ -15,19 +15,19 @@ import (
|
|||
type Size int
|
||||
|
||||
const (
|
||||
Unrecognized Size = iota
|
||||
Small
|
||||
Large
|
||||
unrecognized Size = iota
|
||||
small
|
||||
large
|
||||
)
|
||||
|
||||
func (s *Size) UnmarshalText(text []byte) error {
|
||||
switch strings.ToLower(string(text)) {
|
||||
default:
|
||||
*s = Unrecognized
|
||||
*s = unrecognized
|
||||
case "small":
|
||||
*s = Small
|
||||
*s = small
|
||||
case "large":
|
||||
*s = Large
|
||||
*s = large
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -37,9 +37,9 @@ func (s Size) MarshalText() ([]byte, error) {
|
|||
switch s {
|
||||
default:
|
||||
name = "unrecognized"
|
||||
case Small:
|
||||
case small:
|
||||
name = "small"
|
||||
case Large:
|
||||
case large:
|
||||
name = "large"
|
||||
}
|
||||
return []byte(name), nil
|
||||
|
@ -58,7 +58,7 @@ func Example_textMarshalJSON() {
|
|||
}
|
||||
|
||||
fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n",
|
||||
counts[Small], counts[Large], counts[Unrecognized])
|
||||
counts[small], counts[large], counts[unrecognized])
|
||||
|
||||
// Output:
|
||||
// Inventory Counts:
|
||||
|
|
Loading…
Reference in New Issue