mirror of https://github.com/goccy/go-json.git
Fix pointer fields
This commit is contained in:
parent
66b8cb792a
commit
9351afdb88
|
@ -277,6 +277,7 @@ func (t opType) fieldToStringTagField() opType {
|
||||||
"StructFieldPtrAnonymousHeadOmitEmpty",
|
"StructFieldPtrAnonymousHeadOmitEmpty",
|
||||||
"StructFieldPtrAnonymousHeadStringTag",
|
"StructFieldPtrAnonymousHeadStringTag",
|
||||||
"StructField",
|
"StructField",
|
||||||
|
"StructFieldPtr",
|
||||||
"StructFieldOmitEmpty",
|
"StructFieldOmitEmpty",
|
||||||
"StructFieldStringTag",
|
"StructFieldStringTag",
|
||||||
} {
|
} {
|
||||||
|
|
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
|
return opStructFieldHead
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) typeToFieldType(op opType) opType {
|
func (e *Encoder) typeToFieldType(code *opcode) opType {
|
||||||
switch op {
|
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:
|
case opInt:
|
||||||
return opStructFieldInt
|
return opStructFieldInt
|
||||||
case opInt8:
|
case opInt8:
|
||||||
|
@ -553,8 +584,8 @@ func (e *Encoder) optimizeStructHeader(op opType, tag *structTag, withIndent boo
|
||||||
return headType
|
return headType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) optimizeStructField(op opType, tag *structTag, withIndent bool) opType {
|
func (e *Encoder) optimizeStructField(code *opcode, tag *structTag, withIndent bool) opType {
|
||||||
fieldType := e.typeToFieldType(op)
|
fieldType := e.typeToFieldType(code)
|
||||||
switch {
|
switch {
|
||||||
case tag.isOmitEmpty:
|
case tag.isOmitEmpty:
|
||||||
fieldType = fieldType.fieldToOmitEmptyField()
|
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 {
|
func (e *Encoder) structField(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
|
||||||
code := (*opcode)(unsafe.Pointer(fieldCode))
|
code := (*opcode)(unsafe.Pointer(fieldCode))
|
||||||
op := e.optimizeStructField(valueCode.op, tag, ctx.withIndent)
|
op := e.optimizeStructField(valueCode, tag, ctx.withIndent)
|
||||||
fieldCode.op = op
|
fieldCode.op = op
|
||||||
switch op {
|
switch op {
|
||||||
case opStructField,
|
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
|
type Size int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Unrecognized Size = iota
|
unrecognized Size = iota
|
||||||
Small
|
small
|
||||||
Large
|
large
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Size) UnmarshalText(text []byte) error {
|
func (s *Size) UnmarshalText(text []byte) error {
|
||||||
switch strings.ToLower(string(text)) {
|
switch strings.ToLower(string(text)) {
|
||||||
default:
|
default:
|
||||||
*s = Unrecognized
|
*s = unrecognized
|
||||||
case "small":
|
case "small":
|
||||||
*s = Small
|
*s = small
|
||||||
case "large":
|
case "large":
|
||||||
*s = Large
|
*s = large
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,9 @@ func (s Size) MarshalText() ([]byte, error) {
|
||||||
switch s {
|
switch s {
|
||||||
default:
|
default:
|
||||||
name = "unrecognized"
|
name = "unrecognized"
|
||||||
case Small:
|
case small:
|
||||||
name = "small"
|
name = "small"
|
||||||
case Large:
|
case large:
|
||||||
name = "large"
|
name = "large"
|
||||||
}
|
}
|
||||||
return []byte(name), nil
|
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",
|
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:
|
// Output:
|
||||||
// Inventory Counts:
|
// Inventory Counts:
|
||||||
|
|
Loading…
Reference in New Issue