Compare commits

...

37 Commits

Author SHA1 Message Date
Masaaki Goshima 7c661769df fix lint error 2021-03-11 19:19:30 +09:00
Masaaki Goshima 6c5b9420d7 Add test case 2021-03-11 19:07:52 +09:00
Masaaki Goshima be35ec63b8 Fix issue147 2021-03-11 18:33:31 +09:00
Masaaki Goshima d84b6bca18 Fix recursive call 2021-03-11 17:59:22 +09:00
Masaaki Goshima b78ca46c15 Fix test 2021-03-11 16:46:14 +09:00
Masaaki Goshima f57beb9841 Refactor json.Number 2021-03-11 15:29:32 +09:00
Masaaki Goshima 66d8dd9c61 Support json.Number 2021-03-11 14:58:33 +09:00
Masaaki Goshima 4f372bebd0 Fix Marshaler 2021-03-11 13:09:49 +09:00
Masaaki Goshima 55cfca3b0a Fix test 2021-03-10 19:27:37 +09:00
Masaaki Goshima d0fb2a9a4e Fix opcode 2021-03-10 02:12:53 +09:00
Masaaki Goshima 37fb74bbb8 Fix pointer op 2021-03-10 01:11:48 +09:00
Masaaki Goshima d42d09f9f9 Add indent test 2021-03-09 21:38:58 +09:00
Masaaki Goshima 1657b0e50e Fix escaped vm 2021-03-06 19:06:11 +09:00
Masaaki Goshima 1342fd2042 Remove anonymous operation 2021-03-06 12:51:09 +09:00
Masaaki Goshima 6034aea48a Add test case for MarshalText type 2021-03-06 11:47:51 +09:00
Masaaki Goshima 3c85aa1ba4 refactor 2021-03-06 11:10:01 +09:00
Masaaki Goshima 9aa2047766 Remove unnecessary code 2021-03-06 00:15:58 +09:00
Masaaki Goshima 88cd2ddd6e Work MarshalJSON test 2021-03-05 23:57:44 +09:00
Masaaki Goshima 734267f9ae Add test case for MarshalJSON type 2021-03-05 18:40:09 +09:00
Masaaki Goshima 249c4da6dd Add nilcheck property 2021-03-05 15:10:31 +09:00
Masaaki Goshima d3b031cef2 WIP: add test case for MarshalJSON type 2021-03-05 13:23:47 +09:00
Masaaki Goshima 174fd57eae Remove unnecessary code 2021-03-04 19:03:18 +09:00
Masaaki Goshima 35816cbf97 Add array test 2021-03-04 18:50:48 +09:00
Masaaki Goshima cc84d92b1e Add map test 2021-03-04 18:50:39 +09:00
Masaaki Goshima bac05564e0 Add array test 2021-03-04 15:40:06 +09:00
Masaaki Goshima 64f98e3161 Add slice test 2021-03-04 00:13:33 +09:00
Masaaki Goshima 2a2a19319d Add slice test 2021-03-03 14:54:40 +09:00
Masaaki Goshima 83122027f5 Refactor encode_vm 2021-03-02 19:52:17 +09:00
Masaaki Goshima 09e9e2dd19 Remove unnecessary code 2021-03-01 00:25:31 +09:00
Masaaki Goshima 40c322590e Add test case for int16 type 2021-02-28 23:24:42 +09:00
Masaaki Goshima c6f40ed546 Remove only operation and add indirect property to opcode 2021-02-28 23:22:46 +09:00
Masaaki Goshima 40cc5ff367 Remove only operation 2021-02-23 16:16:44 +09:00
Masaaki Goshima e3acf4a01b Merge branch 'master' of github.com:goccy/go-json into feature/refactor-only-op 2021-02-23 16:13:39 +09:00
Masaaki Goshima 73fbc98667 Fix test case 2021-02-19 15:08:30 +09:00
Masaaki Goshima a324a29256 Pass test 2021-02-19 15:03:52 +09:00
Masaaki Goshima 413c811e37 WIP: workaround 2021-02-19 03:34:21 +09:00
Masaaki Goshima 0272fd00f4 WIP: Integrate int/uint operation 2021-02-19 03:06:21 +09:00
27 changed files with 23280 additions and 15654 deletions

View File

@ -15,11 +15,8 @@ type opType struct {
Op string
Code string
HeadToPtrHead func() string
HeadToNPtrHead func() string
HeadToAnonymousHead func() string
HeadToOmitEmptyHead func() string
HeadToStringTagHead func() string
HeadToOnlyHead func() string
PtrHeadToHead func() string
FieldToEnd func() string
FieldToOmitEmptyField func() string
@ -30,14 +27,6 @@ func (t opType) IsHeadToPtrHead() bool {
return t.Op != t.HeadToPtrHead()
}
func (t opType) IsHeadToNPtrHead() bool {
return t.Op != t.HeadToNPtrHead()
}
func (t opType) IsHeadToAnonymousHead() bool {
return t.Op != t.HeadToAnonymousHead()
}
func (t opType) IsHeadToOmitEmptyHead() bool {
return t.Op != t.HeadToOmitEmptyHead()
}
@ -50,10 +39,6 @@ func (t opType) IsPtrHeadToHead() bool {
return t.Op != t.PtrHeadToHead()
}
func (t opType) IsHeadToOnlyHead() bool {
return t.Op != t.HeadToOnlyHead()
}
func (t opType) IsFieldToEnd() bool {
return t.Op != t.FieldToEnd()
}
@ -71,11 +56,8 @@ func createOpType(op, code string) opType {
Op: op,
Code: code,
HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op },
HeadToAnonymousHead: func() string { return op },
HeadToOmitEmptyHead: func() string { return op },
HeadToStringTagHead: func() string { return op },
HeadToOnlyHead: func() string { return op },
PtrHeadToHead: func() string { return op },
FieldToEnd: func() string { return op },
FieldToOmitEmptyField: func() string { return op },
@ -127,19 +109,19 @@ func (t opType) codeType() codeType {
}
return codeStructField
}
if strings.Contains(t.String(), "ArrayHead") {
if t.String() == "Array" || t.String() == "ArrayPtr" {
return codeArrayHead
}
if strings.Contains(t.String(), "ArrayElem") {
return codeArrayElem
}
if strings.Contains(t.String(), "SliceHead") {
if t.String() == "Slice" || t.String() == "SlicePtr" {
return codeSliceHead
}
if strings.Contains(t.String(), "SliceElem") {
return codeSliceElem
}
if strings.Contains(t.String(), "MapHead") {
if t.String() == "Map" || t.String() == "MapPtr" {
return codeMapHead
}
if strings.Contains(t.String(), "MapKey") {
@ -159,9 +141,6 @@ func (t opType) headToPtrHead() opType {
if strings.Index(t.String(), "PtrHead") > 0 {
return t
}
if strings.Index(t.String(), "PtrAnonymousHead") > 0 {
return t
}
idx := strings.Index(t.String(), "Field")
if idx == -1 {
@ -169,44 +148,15 @@ func (t opType) headToPtrHead() opType {
}
suffix := "Ptr"+t.String()[idx+len("Field"):]
const toPtrOffset = 12
const toPtrOffset = 3
if strings.Contains(opType(int(t) + toPtrOffset).String(), suffix) {
return opType(int(t) + toPtrOffset)
}
return t
}
func (t opType) headToNPtrHead() opType {
if strings.Index(t.String(), "PtrHead") > 0 {
return t
}
if strings.Index(t.String(), "PtrAnonymousHead") > 0 {
return t
}
idx := strings.Index(t.String(), "Field")
if idx == -1 {
return t
}
suffix := "NPtr"+t.String()[idx+len("Field"):]
const toPtrOffset = 24
if strings.Contains(opType(int(t) + toPtrOffset).String(), suffix) {
return opType(int(t) + toPtrOffset)
}
return t
}
func (t opType) headToAnonymousHead() opType {
const toAnonymousOffset = 6
if strings.Contains(opType(int(t) + toAnonymousOffset).String(), "Anonymous") {
return opType(int(t) + toAnonymousOffset)
}
return t
}
func (t opType) headToOmitEmptyHead() opType {
const toOmitEmptyOffset = 2
const toOmitEmptyOffset = 1
if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
return opType(int(t) + toOmitEmptyOffset)
}
@ -215,25 +165,13 @@ func (t opType) headToOmitEmptyHead() opType {
}
func (t opType) headToStringTagHead() opType {
const toStringTagOffset = 4
const toStringTagOffset = 2
if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") {
return opType(int(t) + toStringTagOffset)
}
return t
}
func (t opType) headToOnlyHead() opType {
if strings.HasSuffix(t.String(), "Head") || strings.HasSuffix(t.String(), "HeadOmitEmpty") || strings.HasSuffix(t.String(), "HeadStringTag") {
return t
}
const toOnlyOffset = 1
if opType(int(t) + toOnlyOffset).String() == t.String() + "Only" {
return opType(int(t) + toOnlyOffset)
}
return t
}
func (t opType) ptrHeadToHead() opType {
idx := strings.Index(t.String(), "Ptr")
if idx == -1 {
@ -241,7 +179,7 @@ func (t opType) ptrHeadToHead() opType {
}
suffix := t.String()[idx+len("Ptr"):]
const toPtrOffset = 12
const toPtrOffset = 3
if strings.Contains(opType(int(t) - toPtrOffset).String(), suffix) {
return opType(int(t) - toPtrOffset)
}
@ -295,11 +233,11 @@ func (t opType) fieldToStringTagField() opType {
"StructEnd",
}
primitiveTypes := []string{
"int", "uint", "float32", "float64", "bool", "string", "bytes",
"array", "map", "mapLoad", "slice", "struct", "MarshalJSON", "MarshalText", "recursive",
"int", "uint", "float32", "float64", "bool", "string", "bytes", "number",
"array", "map", "slice", "struct", "MarshalJSON", "MarshalText", "recursive",
"intString", "uintString",
"intPtr", "uintPtr", "float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "bytesPtr",
"intNPtr", "uintNPtr", "float32NPtr", "float64NPtr", "boolNPtr", "stringNPtr", "bytesNPtr",
"intPtr", "uintPtr", "float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "bytesPtr", "numberPtr",
"arrayPtr", "mapPtr", "slicePtr", "marshalJSONPtr", "marshalTextPtr", "interfacePtr", "recursivePtr",
}
primitiveTypesUpper := []string{}
for _, typ := range primitiveTypes {
@ -309,17 +247,12 @@ func (t opType) fieldToStringTagField() opType {
createOpType("End", "Op"),
createOpType("Interface", "Op"),
createOpType("Ptr", "Op"),
createOpType("NPtr", "Op"),
createOpType("SliceHead", "SliceHead"),
createOpType("RootSliceHead", "SliceHead"),
createOpType("SliceElem", "SliceElem"),
createOpType("RootSliceElem", "SliceElem"),
createOpType("SliceEnd", "Op"),
createOpType("ArrayHead", "ArrayHead"),
createOpType("ArrayElem", "ArrayElem"),
createOpType("ArrayEnd", "Op"),
createOpType("MapHead", "MapHead"),
createOpType("MapHeadLoad", "MapHead"),
createOpType("MapKey", "MapKey"),
createOpType("MapValue", "MapValue"),
createOpType("MapEnd", "Op"),
@ -331,102 +264,53 @@ func (t opType) fieldToStringTagField() opType {
opTypes = append(opTypes, createOpType(typ, "Op"))
}
for _, typ := range append(primitiveTypesUpper, "") {
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
for _, headType := range []string{"", "Anonymous"} {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
for _, onlyOrNot := range []string{"", "Only"} {
ptrOrNot := ptrOrNot
headType := headType
opt := opt
typ := typ
onlyOrNot := onlyOrNot
for _, ptrOrNot := range []string{"", "Ptr"} {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
ptrOrNot := ptrOrNot
opt := opt
typ := typ
op := fmt.Sprintf(
"StructField%s%sHead%s%s%s",
ptrOrNot,
headType,
op := fmt.Sprintf(
"StructField%sHead%s%s",
ptrOrNot,
opt,
typ,
)
opTypes = append(opTypes, opType{
Op: op,
Code: "StructField",
HeadToPtrHead: func() string {
return fmt.Sprintf(
"StructFieldPtrHead%s%s",
opt,
typ,
onlyOrNot,
)
opTypes = append(opTypes, opType{
Op: op,
Code: "StructField",
HeadToPtrHead: func() string {
return fmt.Sprintf(
"StructFieldPtr%sHead%s%s%s",
headType,
opt,
typ,
onlyOrNot,
)
},
HeadToNPtrHead: func() string {
return fmt.Sprintf(
"StructFieldNPtr%sHead%s%s%s",
headType,
opt,
typ,
onlyOrNot,
)
},
HeadToAnonymousHead: func() string {
return fmt.Sprintf(
"StructField%sAnonymousHead%s%s%s",
ptrOrNot,
opt,
typ,
onlyOrNot,
)
},
HeadToOmitEmptyHead: func() string {
return fmt.Sprintf(
"StructField%s%sHeadOmitEmpty%s%s",
ptrOrNot,
headType,
typ,
onlyOrNot,
)
},
HeadToStringTagHead: func() string {
return fmt.Sprintf(
"StructField%s%sHeadStringTag%s%s",
ptrOrNot,
headType,
typ,
onlyOrNot,
)
},
HeadToOnlyHead: func() string {
switch typ {
case "", "Array", "Map", "MapLoad", "Slice",
"Struct", "Recursive", "MarshalJSON", "MarshalText",
"IntString", "UintString":
return op
}
return fmt.Sprintf(
"StructField%s%sHead%s%sOnly",
ptrOrNot,
headType,
opt,
typ,
)
},
PtrHeadToHead: func() string {
return fmt.Sprintf(
"StructField%sHead%s%s%s",
headType,
opt,
typ,
onlyOrNot,
)
},
FieldToEnd: func() string { return op },
FieldToOmitEmptyField: func() string { return op },
FieldToStringTagField: func() string { return op },
})
}
}
},
HeadToOmitEmptyHead: func() string {
return fmt.Sprintf(
"StructField%sHeadOmitEmpty%s",
ptrOrNot,
typ,
)
},
HeadToStringTagHead: func() string {
return fmt.Sprintf(
"StructField%sHeadStringTag%s",
ptrOrNot,
typ,
)
},
PtrHeadToHead: func() string {
return fmt.Sprintf(
"StructFieldHead%s%s",
opt,
typ,
)
},
FieldToEnd: func() string { return op },
FieldToOmitEmptyField: func() string { return op },
FieldToStringTagField: func() string { return op },
})
}
}
}
@ -444,15 +328,12 @@ func (t opType) fieldToStringTagField() opType {
Op: op,
Code: "StructField",
HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op },
HeadToAnonymousHead: func() string { return op },
HeadToOmitEmptyHead: func() string { return op },
HeadToStringTagHead: func() string { return op },
HeadToOnlyHead: func() string { return op },
PtrHeadToHead: func() string { return op },
FieldToEnd: func() string {
switch typ {
case "", "Array", "Map", "MapLoad", "Slice", "Struct", "Recursive":
case "", "Array", "Map", "Slice", "Struct", "Recursive":
return op
}
return fmt.Sprintf(
@ -490,11 +371,8 @@ func (t opType) fieldToStringTagField() opType {
Op: op,
Code: "StructEnd",
HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op },
HeadToAnonymousHead: func() string { return op },
HeadToOmitEmptyHead: func() string { return op },
HeadToStringTagHead: func() string { return op },
HeadToOnlyHead: func() string { return op },
PtrHeadToHead: func() string { return op },
FieldToEnd: func() string { return op },
FieldToOmitEmptyField: func() string { return op },

View File

@ -5,6 +5,9 @@ import (
)
func compact(dst *bytes.Buffer, src []byte, escape bool) error {
if len(src) == 0 {
return errUnexpectedEndOfJSON("", 0)
}
length := len(src)
for cursor := 0; cursor < length; cursor++ {
c := src[cursor]

1859
cover_array_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1774,11 +1774,11 @@ func TestCoverBool(t *testing.T) {
enc.SetIndent("", " ")
}
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %+v: %s", test.name, htmlEscape, test.data, err)
t.Fatalf("%s(htmlEscape:%v,indent:%v): %+v: %s", test.name, htmlEscape, indent, test.data, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
t.Errorf("%s(htmlEscape:%v,indent:%v): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, indent, stdresult, buf.String())
}
}
}

View File

@ -5,20 +5,23 @@ import (
stdjson "encoding/json"
)
func intptr(v int) *int { return &v }
func int8ptr(v int8) *int8 { return &v }
func int16ptr(v int16) *int16 { return &v }
func int32ptr(v int32) *int32 { return &v }
func int64ptr(v int64) *int64 { return &v }
func uptr(v uint) *uint { return &v }
func uint8ptr(v uint8) *uint8 { return &v }
func uint16ptr(v uint16) *uint16 { return &v }
func uint32ptr(v uint32) *uint32 { return &v }
func uint64ptr(v uint64) *uint64 { return &v }
func float32ptr(v float32) *float32 { return &v }
func float64ptr(v float64) *float64 { return &v }
func stringptr(v string) *string { return &v }
func boolptr(v bool) *bool { return &v }
func intptr(v int) *int { return &v }
func int8ptr(v int8) *int8 { return &v }
func int16ptr(v int16) *int16 { return &v }
func int32ptr(v int32) *int32 { return &v }
func int64ptr(v int64) *int64 { return &v }
func uptr(v uint) *uint { return &v }
func uint8ptr(v uint8) *uint8 { return &v }
func uint16ptr(v uint16) *uint16 { return &v }
func uint32ptr(v uint32) *uint32 { return &v }
func uint64ptr(v uint64) *uint64 { return &v }
func float32ptr(v float32) *float32 { return &v }
func float64ptr(v float64) *float64 { return &v }
func stringptr(v string) *string { return &v }
func boolptr(v bool) *bool { return &v }
func sliceptr(v []int) *[]int { return &v }
func arrayptr(v [2]int) *[2]int { return &v }
func mapptr(v map[string]int) *map[string]int { return &v }
func encodeByEncodingJSON(data interface{}, indent, escape bool) string {
var buf bytes.Buffer

View File

@ -408,6 +408,29 @@ func TestCoverInt16(t *testing.T) {
}{A: nil, B: nil},
},
// PtrHeadInt16NilMultiFields
{
name: "PtrHeadInt16NilMultiFields",
data: (*struct {
A int16 `json:"a"`
B int16 `json:"b"`
})(nil),
},
{
name: "PtrHeadInt16NilMultiFieldsOmitEmpty",
data: (*struct {
A int16 `json:"a,omitempty"`
B int16 `json:"b,omitempty"`
})(nil),
},
{
name: "PtrHeadInt16NilMultiFieldsString",
data: (*struct {
A int16 `json:"a,string"`
B int16 `json:"b,string"`
})(nil),
},
// PtrHeadInt16NilMultiFields
{
name: "PtrHeadInt16NilMultiFields",

View File

@ -1774,11 +1774,11 @@ func TestCoverInt(t *testing.T) {
enc.SetIndent("", " ")
}
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %+v: %s", test.name, htmlEscape, test.data, err)
t.Fatalf("%s(htmlEscape:%v,indent:%v): %+v: %s", test.name, htmlEscape, indent, test.data, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
t.Errorf("%s(htmlEscape:%v,indent:%v): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, indent, stdresult, buf.String())
}
}
}

1859
cover_map_test.go Normal file

File diff suppressed because it is too large Load Diff

3546
cover_marshal_json_test.go Normal file

File diff suppressed because it is too large Load Diff

3546
cover_marshal_text_test.go Normal file

File diff suppressed because it is too large Load Diff

1859
cover_slice_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1793,11 +1793,11 @@ func TestCoverString(t *testing.T) {
enc.SetIndent("", " ")
}
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %v: %s", test.name, htmlEscape, test.data, err)
t.Fatalf("%s(htmlEscape:%v,indent:%v): %v: %s", test.name, htmlEscape, indent, test.data, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
t.Errorf("%s(htmlEscape:%v,indent:%v): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, indent, stdresult, buf.String())
}
}
}

View File

@ -1774,11 +1774,11 @@ func TestCoverUint16(t *testing.T) {
enc.SetIndent("", " ")
}
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %+v: %s", test.name, htmlEscape, test.data, err)
t.Fatalf("%s(htmlEscape:%v,indent:%v): %+v: %s", test.name, htmlEscape, indent, test.data, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
t.Errorf("%s(htmlEscape:%v,indent:%v): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, indent, stdresult, buf.String())
}
}
}

View File

@ -80,7 +80,7 @@ func decodeCompile(typ *rtype, structName, fieldName string, structTypeToDecoder
case reflect.Uint64:
return decodeCompileUint64(typ, structName, fieldName)
case reflect.String:
return decodeCompileString(structName, fieldName)
return decodeCompileString(typ, structName, fieldName)
case reflect.Bool:
return decodeCompileBool(structName, fieldName)
case reflect.Float32:
@ -203,7 +203,12 @@ func decodeCompileFloat64(structName, fieldName string) (decoder, error) {
}), nil
}
func decodeCompileString(structName, fieldName string) (decoder, error) {
func decodeCompileString(typ *rtype, structName, fieldName string) (decoder, error) {
if typ == type2rtype(jsonNumberType) {
return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v Number) {
*(*Number)(p) = v
}), nil
}
return newStringDecoder(structName, fieldName), nil
}

View File

@ -1,42 +1,118 @@
package json
import (
"strconv"
"unsafe"
)
type numberDecoder struct {
*floatDecoder
op func(unsafe.Pointer, Number)
structName string
fieldName string
stringDecoder *stringDecoder
op func(unsafe.Pointer, Number)
structName string
fieldName string
}
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, Number)) *numberDecoder {
return &numberDecoder{
floatDecoder: newFloatDecoder(structName, fieldName, nil),
op: op,
structName: structName,
fieldName: fieldName,
stringDecoder: newStringDecoder(structName, fieldName),
op: op,
structName: structName,
fieldName: fieldName,
}
}
func (d *numberDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.floatDecoder.decodeStreamByte(s)
bytes, err := d.decodeStreamByte(s)
if err != nil {
return err
}
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
return &SyntaxError{msg: err.Error(), Offset: s.totalOffset()}
}
d.op(p, Number(string(bytes)))
s.reset()
return nil
}
func (d *numberDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.floatDecoder.decodeByte(buf, cursor)
bytes, c, err := d.decodeByte(buf, cursor)
if err != nil {
return 0, err
}
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
return 0, &SyntaxError{msg: err.Error(), Offset: c}
}
cursor = c
s := *(*string)(unsafe.Pointer(&bytes))
d.op(p, Number(s))
return cursor, nil
}
func (d *numberDecoder) decodeStreamByte(s *stream) ([]byte, error) {
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
s.cursor++
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return floatBytes(s), nil
case 'n':
if err := nullBytes(s); err != nil {
return nil, err
}
return nil, nil
case '"':
return d.stringDecoder.decodeStreamByte(s)
case nul:
if s.read() {
continue
}
goto ERROR
default:
goto ERROR
}
}
ERROR:
return nil, errUnexpectedEndOfJSON("json.Number", s.totalOffset())
}
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
buflen := int64(len(buf))
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
if floatTable[buf[cursor]] {
continue
}
break
}
num := buf[start:cursor]
return num, cursor, nil
case 'n':
if cursor+3 >= buflen {
return nil, 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return nil, 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return nil, cursor, nil
case '"':
return d.stringDecoder.decodeByte(buf, cursor)
default:
return nil, 0, errUnexpectedEndOfJSON("json.Number", cursor)
}
}
return nil, 0, errUnexpectedEndOfJSON("json.Number", cursor)
}

View File

@ -1251,31 +1251,31 @@ var unmarshalTests = []unmarshalTest{
in: `invalid`, // 143
ptr: new(json.Number),
err: json.NewSyntaxError(
`json: invalid character v as null`,
`json: json.Number unexpected end of JSON input`,
1,
),
},
{
in: `"invalid"`, // 144
ptr: new(json.Number),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`),
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
},
{
in: `{"A":"invalid"}`, // 145
ptr: new(struct{ A json.Number }),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`),
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
},
{
in: `{"A":"invalid"}`, // 146
ptr: new(struct {
A json.Number `json:",string"`
}),
err: fmt.Errorf(`json: null unexpected end of JSON input`),
err: fmt.Errorf(`json: json.Number unexpected end of JSON input`),
},
{
in: `{"A":"invalid"}`, // 147
ptr: new(map[string]json.Number),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`),
err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
},
/*
// invalid UTF-8 is coerced to valid UTF-8.
@ -1836,6 +1836,7 @@ func TestUnmarshal(t *testing.T) {
// Check round trip also decodes correctly.
if tt.err == nil {
//fmt.Println("iface = ", v.Interface(), v.Type())
enc, err := json.Marshal(v.Interface())
if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err)

144
encode.go
View File

@ -2,10 +2,14 @@ package json
import (
"bytes"
"encoding"
"encoding/base64"
"fmt"
"io"
"math"
"reflect"
"strconv"
"strings"
"sync"
"unsafe"
)
@ -358,7 +362,147 @@ func encodeByteSlice(b []byte, src []byte) []byte {
return append(append(b, buf...), '"')
}
func encodeNumber(b []byte, n Number) ([]byte, error) {
if len(n) == 0 {
return append(b, '0'), nil
}
for i := 0; i < len(n); i++ {
if !floatTable[n[i]] {
return nil, fmt.Errorf("json: invalid number literal %q", n)
}
}
b = append(b, n...)
return b, nil
}
func appendIndent(ctx *encodeRuntimeContext, b []byte, indent int) []byte {
b = append(b, ctx.prefix...)
return append(b, bytes.Repeat(ctx.indentStr, ctx.baseIndent+indent)...)
}
func encodeIsNilForMarshaler(v interface{}) bool {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Interface, reflect.Map, reflect.Ptr:
return rv.IsNil()
case reflect.Slice:
return rv.IsNil() || rv.Len() == 0
}
return false
}
func encodeMarshalJSON(code *opcode, b []byte, v interface{}, escape bool) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type
if code.addrForMarshaler {
if rv.CanAddr() {
rv = rv.Addr()
} else {
newV := reflect.New(rv.Type())
newV.Elem().Set(rv)
rv = newV
}
}
v = rv.Interface()
marshaler, ok := v.(Marshaler)
if !ok {
return encodeNull(b), nil
}
bb, err := marshaler.MarshalJSON()
if err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, escape); err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
return buf.Bytes(), nil
}
func encodeMarshalJSONIndent(ctx *encodeRuntimeContext, code *opcode, b []byte, v interface{}, indent int, escape bool) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type
if code.addrForMarshaler {
if rv.CanAddr() {
rv = rv.Addr()
} else {
newV := reflect.New(rv.Type())
newV.Elem().Set(rv)
rv = newV
}
}
v = rv.Interface()
marshaler, ok := v.(Marshaler)
if !ok {
return encodeNull(b), nil
}
bb, err := marshaler.MarshalJSON()
if err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, escape); err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(ctx.prefix)+strings.Repeat(string(ctx.indentStr), ctx.baseIndent+indent+1),
string(ctx.indentStr),
); err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
return append(b, indentBuf.Bytes()...), nil
}
func encodeMarshalText(code *opcode, b []byte, v interface{}, escape bool) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type
if code.addrForMarshaler {
if rv.CanAddr() {
rv = rv.Addr()
} else {
newV := reflect.New(rv.Type())
newV.Elem().Set(rv)
rv = newV
}
}
v = rv.Interface()
marshaler, ok := v.(encoding.TextMarshaler)
if !ok {
return encodeNull(b), nil
}
bytes, err := marshaler.MarshalText()
if err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
if escape {
return encodeEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}
return encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}
func encodeMarshalTextIndent(code *opcode, b []byte, v interface{}, escape bool) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type
if code.addrForMarshaler {
if rv.CanAddr() {
rv = rv.Addr()
} else {
newV := reflect.New(rv.Type())
newV.Elem().Set(rv)
rv = newV
}
}
v = rv.Interface()
marshaler, ok := v.(encoding.TextMarshaler)
if !ok {
return encodeNull(b), nil
}
bytes, err := marshaler.MarshalText()
if err != nil {
return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
}
if escape {
return encodeEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}
return encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}

View File

@ -24,6 +24,7 @@ type opcodeSet struct {
var (
marshalJSONType = reflect.TypeOf((*Marshaler)(nil)).Elem()
marshalTextType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
jsonNumberType = reflect.TypeOf(Number(""))
)
func encodeCompileToGetCodeSetSlowPath(typeptr uintptr) (*opcodeSet, error) {
@ -56,42 +57,44 @@ func encodeCompileToGetCodeSetSlowPath(typeptr uintptr) (*opcodeSet, error) {
func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case typ.Implements(marshalJSONType):
case encodeImplementsMarshalJSON(typ):
return encodeCompileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
case encodeImplementsMarshalText(typ):
return encodeCompileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
}
isPtr := false
orgType := typ
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
isPtr = true
}
switch {
case encodeImplementsMarshalJSON(typ):
return encodeCompileMarshalJSON(ctx)
case encodeImplementsMarshalText(typ):
return encodeCompileMarshalText(ctx)
}
if typ.Kind() == reflect.Map {
return encodeCompileMap(ctx.withType(typ), isPtr)
if isPtr {
return encodeCompilePtr(ctx.withType(rtype_ptrTo(typ)))
}
return encodeCompileMap(ctx.withType(typ))
} else if typ.Kind() == reflect.Struct {
code, err := encodeCompileStruct(ctx.withType(typ), isPtr)
if err != nil {
return nil, err
}
encodeConvertHeadOnlyCode(code, isPtr)
encodeOptimizeStructEnd(code)
encodeLinkRecursiveCode(code)
return code, nil
} else if isPtr && typ.Implements(marshalTextType) {
typ = orgType
} else if isPtr && typ.Implements(marshalJSONType) {
typ = orgType
}
code, err := encodeCompile(ctx.withType(typ))
code, err := encodeCompile(ctx.withType(typ), isPtr)
if err != nil {
return nil, err
}
encodeConvertHeadOnlyCode(code, isPtr)
encodeOptimizeStructEnd(code)
encodeLinkRecursiveCode(code)
return code, nil
@ -100,9 +103,7 @@ func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
func encodeLinkRecursiveCode(c *opcode) {
for code := c; code.op != opEnd && code.op != opStructFieldRecursiveEnd; {
switch code.op {
case opStructFieldRecursive,
opStructFieldPtrAnonymousHeadRecursive,
opStructFieldAnonymousHeadRecursive:
case opStructFieldRecursive, opStructFieldRecursivePtr:
if code.jmp.linked {
code = code.next
continue
@ -143,7 +144,7 @@ func encodeLinkRecursiveCode(c *opcode) {
func encodeOptimizeStructEnd(c *opcode) {
for code := c; code.op != opEnd; {
if code.op == opStructFieldRecursive {
if code.op == opStructFieldRecursive || code.op == opStructFieldRecursivePtr {
// ignore if exists recursive operation
return
}
@ -163,7 +164,13 @@ func encodeOptimizeStructEnd(c *opcode) {
switch code.op {
case opStructEnd:
prev := code.prevField
if strings.Contains(prev.op.String(), "Head") {
prevOp := prev.op.String()
if strings.Contains(prevOp, "Head") ||
strings.Contains(prevOp, "Slice") ||
strings.Contains(prevOp, "Array") ||
strings.Contains(prevOp, "Map") ||
strings.Contains(prevOp, "MarshalJSON") ||
strings.Contains(prevOp, "MarshalText") {
// not exists field
code = code.next
break
@ -182,96 +189,62 @@ func encodeOptimizeStructEnd(c *opcode) {
}
}
func encodeConvertHeadOnlyCode(c *opcode, isPtrHead bool) {
if c.nextField == nil {
return
func encodeImplementsMarshalJSON(typ *rtype) bool {
if !typ.Implements(marshalJSONType) {
return false
}
if c.nextField.op.codeType() != codeStructEnd {
return
}
switch c.op {
case opStructFieldHead:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadOnly
case opStructFieldHeadOmitEmpty:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadOmitEmptyOnly
case opStructFieldHeadStringTag:
encodeConvertHeadOnlyCode(c.next, false)
if !strings.Contains(c.next.op.String(), "Only") {
return
}
c.op = opStructFieldHeadStringTagOnly
case opStructFieldPtrHead:
}
if strings.Contains(c.op.String(), "Marshal") {
return
}
if strings.Contains(c.op.String(), "Slice") {
return
}
if strings.Contains(c.op.String(), "Map") {
return
}
isPtrOp := strings.Contains(c.op.String(), "Ptr")
if isPtrOp && !isPtrHead {
c.op = c.op.headToOnlyHead()
} else if !isPtrOp && isPtrHead {
c.op = c.op.headToPtrHead().headToOnlyHead()
} else if isPtrOp && isPtrHead {
c.op = c.op.headToPtrHead().headToOnlyHead()
}
}
func encodeImplementsMarshaler(typ *rtype) bool {
switch {
case typ.Implements(marshalJSONType):
return true
case rtype_ptrTo(typ).Implements(marshalJSONType):
return true
case typ.Implements(marshalTextType):
return true
case rtype_ptrTo(typ).Implements(marshalTextType):
if typ.Kind() != reflect.Ptr {
return true
}
// type kind is reflect.Ptr
if !typ.Elem().Implements(marshalJSONType) {
return true
}
// needs to dereference
return false
}
func encodeCompile(ctx *encodeCompileContext) (*opcode, error) {
func encodeImplementsMarshalText(typ *rtype) bool {
if !typ.Implements(marshalTextType) {
return false
}
if typ.Kind() != reflect.Ptr {
return true
}
// type kind is reflect.Ptr
if !typ.Elem().Implements(marshalTextType) {
return true
}
// needs to dereference
return false
}
func encodeCompile(ctx *encodeCompileContext, isPtr bool) (*opcode, error) {
typ := ctx.typ
switch {
case typ.Implements(marshalJSONType):
case encodeImplementsMarshalJSON(typ):
return encodeCompileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
case encodeImplementsMarshalText(typ):
return encodeCompileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
}
switch typ.Kind() {
case reflect.Ptr:
return encodeCompilePtr(ctx)
case reflect.Slice:
elem := typ.Elem()
if !encodeImplementsMarshaler(elem) && elem.Kind() == reflect.Uint8 {
return encodeCompileBytes(ctx)
if elem.Kind() == reflect.Uint8 {
p := rtype_ptrTo(elem)
if !p.Implements(marshalJSONType) && !p.Implements(marshalTextType) {
return encodeCompileBytes(ctx)
}
}
return encodeCompileSlice(ctx)
case reflect.Array:
return encodeCompileArray(ctx)
case reflect.Map:
return encodeCompileMap(ctx, true)
return encodeCompileMap(ctx)
case reflect.Struct:
return encodeCompileStruct(ctx, false)
return encodeCompileStruct(ctx, isPtr)
case reflect.Interface:
return encodeCompileInterface(ctx)
case reflect.Int:
@ -308,13 +281,51 @@ func encodeCompile(ctx *encodeCompileContext) (*opcode, error) {
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
}
func encodeConvertPtrOp(code *opcode) opType {
ptrHeadOp := code.op.headToPtrHead()
if code.op != ptrHeadOp {
return ptrHeadOp
}
switch code.op {
case opInt:
return opIntPtr
case opUint:
return opUintPtr
case opFloat32:
return opFloat32Ptr
case opFloat64:
return opFloat64Ptr
case opString:
return opStringPtr
case opBool:
return opBoolPtr
case opBytes:
return opBytesPtr
case opArray:
return opArrayPtr
case opSlice:
return opSlicePtr
case opMap:
return opMapPtr
case opMarshalJSON:
return opMarshalJSONPtr
case opMarshalText:
return opMarshalTextPtr
case opInterface:
return opInterfacePtr
case opStructFieldRecursive:
return opStructFieldRecursivePtr
}
return code.op
}
func encodeCompileKey(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSONPtr(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalTextPtr(ctx)
case encodeImplementsMarshalJSON(typ):
return encodeCompileMarshalJSON(ctx)
case encodeImplementsMarshalText(typ):
return encodeCompileMarshalText(ctx)
}
switch typ.Kind() {
case reflect.Ptr:
@ -350,46 +361,31 @@ func encodeCompileKey(ctx *encodeCompileContext) (*opcode, error) {
}
func encodeCompilePtr(ctx *encodeCompileContext) (*opcode, error) {
ptrOpcodeIndex := ctx.opcodeIndex
ptrIndex := ctx.ptrIndex
ctx.incIndex()
code, err := encodeCompile(ctx.withType(ctx.typ.Elem()))
code, err := encodeCompile(ctx.withType(ctx.typ.Elem()), true)
if err != nil {
return nil, err
}
ptrHeadOp := code.op.headToPtrHead()
if code.op != ptrHeadOp {
code.op = ptrHeadOp
code.decOpcodeIndex()
ctx.decIndex()
return code, nil
}
c := ctx.context()
c.opcodeIndex = ptrOpcodeIndex
c.ptrIndex = ptrIndex
return newOpCodeWithNext(c, opPtr, code), nil
code.op = encodeConvertPtrOp(code)
code.ptrNum++
return code, nil
}
func encodeCompileMarshalJSON(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opMarshalJSON)
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalJSONPtr(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx.withType(rtype_ptrTo(ctx.typ)), opMarshalJSON)
typ := ctx.typ
if !typ.Implements(marshalJSONType) && rtype_ptrTo(typ).Implements(marshalJSONType) {
code.addrForMarshaler = true
}
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalText(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opMarshalText)
ctx.incIndex()
return code, nil
}
func encodeCompileMarshalTextPtr(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx.withType(rtype_ptrTo(ctx.typ)), opMarshalText)
typ := ctx.typ
if !typ.Implements(marshalTextType) && rtype_ptrTo(typ).Implements(marshalTextType) {
code.addrForMarshaler = true
}
ctx.incIndex()
return code, nil
}
@ -593,7 +589,13 @@ func encodeCompileFloat64(ctx *encodeCompileContext) (*opcode, error) {
}
func encodeCompileString(ctx *encodeCompileContext) (*opcode, error) {
code := newOpCode(ctx, opString)
var op opType
if ctx.typ == type2rtype(jsonNumberType) {
op = opNumber
} else {
op = opString
}
code := newOpCode(ctx, op)
ctx.incIndex()
return code, nil
}
@ -624,7 +626,7 @@ func encodeCompileSlice(ctx *encodeCompileContext) (*opcode, error) {
header := newSliceHeaderCode(ctx)
ctx.incIndex()
code, err := encodeCompile(ctx.withType(ctx.typ.Elem()).incIndent())
code, err := encodeCompileSliceElem(ctx.withType(elem).incIndent())
if err != nil {
return nil, err
}
@ -648,6 +650,18 @@ func encodeCompileSlice(ctx *encodeCompileContext) (*opcode, error) {
return (*opcode)(unsafe.Pointer(header)), nil
}
func encodeCompileSliceElem(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case !typ.Implements(marshalJSONType) && rtype_ptrTo(typ).Implements(marshalJSONType):
return encodeCompileMarshalJSON(ctx)
case !typ.Implements(marshalTextType) && rtype_ptrTo(typ).Implements(marshalTextType):
return encodeCompileMarshalText(ctx)
default:
return encodeCompile(ctx, false)
}
}
func encodeCompileArray(ctx *encodeCompileContext) (*opcode, error) {
ctx.root = false
typ := ctx.typ
@ -658,7 +672,7 @@ func encodeCompileArray(ctx *encodeCompileContext) (*opcode, error) {
header := newArrayHeaderCode(ctx, alen)
ctx.incIndex()
code, err := encodeCompile(ctx.withType(elem).incIndent())
code, err := encodeCompile(ctx.withType(elem).incIndent(), false)
if err != nil {
return nil, err
}
@ -697,12 +711,12 @@ func mapiternext(it unsafe.Pointer)
//go:noescape
func maplen(m unsafe.Pointer) int
func encodeCompileMap(ctx *encodeCompileContext, withLoad bool) (*opcode, error) {
func encodeCompileMap(ctx *encodeCompileContext) (*opcode, error) {
// header => code => value => code => key => code => value => code => end
// ^ |
// |_______________________|
ctx = ctx.incIndent()
header := newMapHeaderCode(ctx, withLoad)
header := newMapHeaderCode(ctx)
ctx.incIndex()
typ := ctx.typ
@ -716,7 +730,7 @@ func encodeCompileMap(ctx *encodeCompileContext, withLoad bool) (*opcode, error)
ctx.incIndex()
valueType := typ.Elem()
valueCode, err := encodeCompile(ctx.withType(valueType))
valueCode, err := encodeCompile(ctx.withType(valueType), false)
if err != nil {
return nil, err
}
@ -745,174 +759,122 @@ func encodeCompileMap(ctx *encodeCompileContext, withLoad bool) (*opcode, error)
return (*opcode)(unsafe.Pointer(header)), nil
}
func encodeTypeToHeaderType(ctx *encodeCompileContext, code *opcode) opType {
func encodeTypeToHeaderType(code *opcode) opType {
switch code.op {
case opPtr:
ptrNum := 1
c := code
ctx.decIndex()
for {
if code.next.op == opPtr {
ptrNum++
code = code.next
ctx.decIndex()
continue
}
break
}
c.ptrNum = ptrNum
if ptrNum > 1 {
switch code.next.op {
case opInt:
c.mask = code.next.mask
c.rshiftNum = code.next.rshiftNum
return opStructFieldHeadIntNPtr
case opUint:
c.mask = code.next.mask
return opStructFieldHeadUintNPtr
case opFloat32:
return opStructFieldHeadFloat32NPtr
case opFloat64:
return opStructFieldHeadFloat64NPtr
case opString:
return opStructFieldHeadStringNPtr
case opBool:
return opStructFieldHeadBoolNPtr
}
} else {
switch code.next.op {
case opInt:
c.mask = code.next.mask
c.rshiftNum = code.next.rshiftNum
return opStructFieldHeadIntPtr
case opUint:
c.mask = code.next.mask
return opStructFieldHeadUintPtr
case opFloat32:
return opStructFieldHeadFloat32Ptr
case opFloat64:
return opStructFieldHeadFloat64Ptr
case opString:
return opStructFieldHeadStringPtr
case opBool:
return opStructFieldHeadBoolPtr
}
}
case opInt:
return opStructFieldHeadInt
case opIntPtr:
return opStructFieldHeadIntPtr
case opUint:
return opStructFieldHeadUint
case opUintPtr:
return opStructFieldHeadUintPtr
case opFloat32:
return opStructFieldHeadFloat32
case opFloat32Ptr:
return opStructFieldHeadFloat32Ptr
case opFloat64:
return opStructFieldHeadFloat64
case opFloat64Ptr:
return opStructFieldHeadFloat64Ptr
case opString:
return opStructFieldHeadString
case opStringPtr:
return opStructFieldHeadStringPtr
case opNumber:
return opStructFieldHeadNumber
case opNumberPtr:
return opStructFieldHeadNumberPtr
case opBool:
return opStructFieldHeadBool
case opMapHead:
case opBoolPtr:
return opStructFieldHeadBoolPtr
case opMap:
return opStructFieldHeadMap
case opMapHeadLoad:
return opStructFieldHeadMapLoad
case opArrayHead:
case opMapPtr:
code.op = opMap
return opStructFieldHeadMapPtr
case opArray:
return opStructFieldHeadArray
case opSliceHead:
case opArrayPtr:
code.op = opArray
return opStructFieldHeadArrayPtr
case opSlice:
return opStructFieldHeadSlice
case opStructFieldHead:
return opStructFieldHeadStruct
case opSlicePtr:
code.op = opSlice
return opStructFieldHeadSlicePtr
case opMarshalJSON:
return opStructFieldHeadMarshalJSON
case opMarshalJSONPtr:
return opStructFieldHeadMarshalJSONPtr
case opMarshalText:
return opStructFieldHeadMarshalText
case opMarshalTextPtr:
return opStructFieldHeadMarshalTextPtr
}
return opStructFieldHead
}
func encodeTypeToFieldType(ctx *encodeCompileContext, code *opcode) opType {
func encodeTypeToFieldType(code *opcode) opType {
switch code.op {
case opPtr:
ptrNum := 1
ctx.decIndex()
c := code
for {
if code.next.op == opPtr {
ptrNum++
code = code.next
ctx.decIndex()
continue
}
break
}
c.ptrNum = ptrNum
if ptrNum > 1 {
switch code.next.op {
case opInt:
c.mask = code.next.mask
c.rshiftNum = code.next.rshiftNum
return opStructFieldIntNPtr
case opUint:
c.mask = code.next.mask
return opStructFieldUintNPtr
case opFloat32:
return opStructFieldFloat32NPtr
case opFloat64:
return opStructFieldFloat64NPtr
case opString:
return opStructFieldStringNPtr
case opBool:
return opStructFieldBoolNPtr
}
} else {
switch code.next.op {
case opInt:
c.mask = code.next.mask
c.rshiftNum = code.next.rshiftNum
return opStructFieldIntPtr
case opUint:
c.mask = code.next.mask
return opStructFieldUintPtr
case opFloat32:
return opStructFieldFloat32Ptr
case opFloat64:
return opStructFieldFloat64Ptr
case opString:
return opStructFieldStringPtr
case opBool:
return opStructFieldBoolPtr
}
}
case opInt:
return opStructFieldInt
case opIntPtr:
return opStructFieldIntPtr
case opUint:
return opStructFieldUint
case opUintPtr:
return opStructFieldUintPtr
case opFloat32:
return opStructFieldFloat32
case opFloat32Ptr:
return opStructFieldFloat32Ptr
case opFloat64:
return opStructFieldFloat64
case opFloat64Ptr:
return opStructFieldFloat64Ptr
case opString:
return opStructFieldString
case opStringPtr:
return opStructFieldStringPtr
case opNumber:
return opStructFieldNumber
case opNumberPtr:
return opStructFieldNumberPtr
case opBool:
return opStructFieldBool
case opMapHead:
case opBoolPtr:
return opStructFieldBoolPtr
case opMap:
return opStructFieldMap
case opMapHeadLoad:
return opStructFieldMapLoad
case opArrayHead:
case opMapPtr:
code.op = opMap
return opStructFieldMapPtr
case opArray:
return opStructFieldArray
case opSliceHead:
case opArrayPtr:
code.op = opArray
return opStructFieldArrayPtr
case opSlice:
return opStructFieldSlice
case opStructFieldHead:
return opStructFieldStruct
case opSlicePtr:
code.op = opSlice
return opStructFieldSlicePtr
case opMarshalJSON:
return opStructFieldMarshalJSON
case opMarshalJSONPtr:
return opStructFieldMarshalJSONPtr
case opMarshalText:
return opStructFieldMarshalText
case opMarshalTextPtr:
return opStructFieldMarshalTextPtr
}
return opStructField
}
func encodeOptimizeStructHeader(ctx *encodeCompileContext, code *opcode, tag *structTag) opType {
headType := encodeTypeToHeaderType(ctx, code)
func encodeOptimizeStructHeader(code *opcode, tag *structTag) opType {
headType := encodeTypeToHeaderType(code)
switch {
case tag.isOmitEmpty:
headType = headType.headToOmitEmptyHead()
@ -922,8 +884,8 @@ func encodeOptimizeStructHeader(ctx *encodeCompileContext, code *opcode, tag *st
return headType
}
func encodeOptimizeStructField(ctx *encodeCompileContext, code *opcode, tag *structTag) opType {
fieldType := encodeTypeToFieldType(ctx, code)
func encodeOptimizeStructField(code *opcode, tag *structTag) opType {
fieldType := encodeTypeToFieldType(code)
switch {
case tag.isOmitEmpty:
fieldType = fieldType.fieldToOmitEmptyField()
@ -950,7 +912,7 @@ func encodeCompiledCode(ctx *encodeCompileContext) *opcode {
func encodeStructHeader(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
fieldCode.indent--
op := encodeOptimizeStructHeader(ctx, valueCode, tag)
op := encodeOptimizeStructHeader(valueCode, tag)
fieldCode.op = op
fieldCode.mask = valueCode.mask
fieldCode.rshiftNum = valueCode.rshiftNum
@ -960,16 +922,35 @@ func encodeStructHeader(ctx *encodeCompileContext, fieldCode *opcode, valueCode
opStructFieldHeadSlice,
opStructFieldHeadArray,
opStructFieldHeadMap,
opStructFieldHeadMapLoad,
opStructFieldHeadStruct,
opStructFieldHeadOmitEmpty,
opStructFieldHeadOmitEmptySlice,
opStructFieldHeadStringTagSlice,
opStructFieldHeadOmitEmptyArray,
opStructFieldHeadStringTagArray,
opStructFieldHeadOmitEmptyMap,
opStructFieldHeadOmitEmptyMapLoad,
opStructFieldHeadStringTagMap,
opStructFieldHeadOmitEmptyStruct,
opStructFieldHeadStringTag:
return valueCode.beforeLastCode()
case opStructFieldHeadSlicePtr,
opStructFieldHeadOmitEmptySlicePtr,
opStructFieldHeadStringTagSlicePtr,
opStructFieldHeadArrayPtr,
opStructFieldHeadOmitEmptyArrayPtr,
opStructFieldHeadStringTagArrayPtr,
opStructFieldHeadMapPtr,
opStructFieldHeadOmitEmptyMapPtr,
opStructFieldHeadStringTagMapPtr:
return valueCode.beforeLastCode()
case opStructFieldHeadMarshalJSONPtr,
opStructFieldHeadOmitEmptyMarshalJSONPtr,
opStructFieldHeadStringTagMarshalJSONPtr,
opStructFieldHeadMarshalTextPtr,
opStructFieldHeadOmitEmptyMarshalTextPtr,
opStructFieldHeadStringTagMarshalTextPtr:
ctx.decOpcodeIndex()
return (*opcode)(unsafe.Pointer(fieldCode))
}
ctx.decOpcodeIndex()
return (*opcode)(unsafe.Pointer(fieldCode))
@ -977,26 +958,38 @@ func encodeStructHeader(ctx *encodeCompileContext, fieldCode *opcode, valueCode
func encodeStructField(ctx *encodeCompileContext, fieldCode *opcode, valueCode *opcode, tag *structTag) *opcode {
code := (*opcode)(unsafe.Pointer(fieldCode))
op := encodeOptimizeStructField(ctx, valueCode, tag)
op := encodeOptimizeStructField(valueCode, tag)
fieldCode.op = op
fieldCode.ptrNum = valueCode.ptrNum
fieldCode.mask = valueCode.mask
fieldCode.rshiftNum = valueCode.rshiftNum
fieldCode.jmp = valueCode.jmp
switch op {
case opStructField,
opStructFieldSlice,
opStructFieldArray,
opStructFieldMap,
opStructFieldMapLoad,
opStructFieldStruct,
opStructFieldOmitEmpty,
opStructFieldOmitEmptySlice,
opStructFieldStringTagSlice,
opStructFieldOmitEmptyArray,
opStructFieldStringTagArray,
opStructFieldOmitEmptyMap,
opStructFieldOmitEmptyMapLoad,
opStructFieldStringTagMap,
opStructFieldOmitEmptyStruct,
opStructFieldStringTag:
return valueCode.beforeLastCode()
case opStructFieldSlicePtr,
opStructFieldOmitEmptySlicePtr,
opStructFieldStringTagSlicePtr,
opStructFieldArrayPtr,
opStructFieldOmitEmptyArrayPtr,
opStructFieldStringTagArrayPtr,
opStructFieldMapPtr,
opStructFieldOmitEmptyMapPtr,
opStructFieldStringTagMapPtr:
return valueCode.beforeLastCode()
}
ctx.decIndex()
return code
@ -1006,7 +999,10 @@ func encodeIsNotExistsField(head *opcode) bool {
if head == nil {
return false
}
if head.op != opStructFieldAnonymousHead {
if head.op != opStructFieldHead {
return false
}
if !head.anonymousHead {
return false
}
if head.next == nil {
@ -1068,14 +1064,17 @@ func encodeAnonymousStructFieldPairMap(tags structTags, named string, valueCode
removedFields := map[*opcode]struct{}{}
for {
existsKey := tags.existsKey(f.displayKey)
op := f.op.headToAnonymousHead()
if existsKey && (f.next.op == opStructFieldPtrAnonymousHeadRecursive || f.next.op == opStructFieldAnonymousHeadRecursive) {
isHeadOp := strings.Contains(f.op.String(), "Head")
if existsKey && strings.Contains(f.op.String(), "Recursive") {
// through
} else if op != f.op {
} else if isHeadOp && !f.anonymousHead {
if existsKey {
f.op = opStructFieldAnonymousHead
// TODO: need to remove this head
f.op = opStructFieldHead
f.anonymousKey = true
f.anonymousHead = true
} else if named == "" {
f.op = op
f.anonymousHead = true
}
} else if named == "" && f.op == opStructEnd {
f.op = opStructAnonymousEnd
@ -1121,7 +1120,7 @@ func encodeAnonymousFieldPairRecursively(named string, valueCode *opcode) map[st
f := valueCode
var prevAnonymousField *opcode
for {
if f.displayKey != "" && strings.Contains(f.op.String(), "Anonymous") {
if f.displayKey != "" && f.anonymousHead {
key := fmt.Sprintf("%s.%s", named, f.displayKey)
anonymousFields[key] = append(anonymousFields[key], structFieldPair{
prevField: prevAnonymousField,
@ -1158,7 +1157,9 @@ func encodeOptimizeConflictAnonymousFields(anonymousFields map[string][]structFi
if !fieldPair.linked {
if fieldPair.prevField == nil {
// head operation
fieldPair.curField.op = opStructFieldAnonymousHead
fieldPair.curField.op = opStructFieldHead
fieldPair.curField.anonymousHead = true
fieldPair.curField.anonymousKey = true
} else {
diff := fieldPair.curField.nextField.displayIdx - fieldPair.curField.displayIdx
for i := 0; i < diff; i++ {
@ -1176,7 +1177,9 @@ func encodeOptimizeConflictAnonymousFields(anonymousFields map[string][]structFi
if !fieldPair.linked {
if fieldPair.prevField == nil {
// head operation
fieldPair.curField.op = opStructFieldAnonymousHead
fieldPair.curField.op = opStructFieldHead
fieldPair.curField.anonymousHead = true
fieldPair.curField.anonymousKey = true
} else {
diff := fieldPair.curField.nextField.displayIdx - fieldPair.curField.displayIdx
removedFields[fieldPair.curField] = struct{}{}
@ -1209,7 +1212,10 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
// ^ |
// |__________|
fieldNum := typ.NumField()
indirect := ifaceIndir(typ)
//fmt.Println("indirect = ", indirect, "type = ", typ, "isPtr = ", isPtr)
fieldIdx := 0
disableIndirectConversion := false
var (
head *opcode
code *opcode
@ -1228,30 +1234,66 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
for i, tag := range tags {
field := tag.field
fieldType := type2rtype(field.Type)
if isPtr && i == 0 {
// head field of pointer structure at top level
// if field type is pointer and implements MarshalJSON or MarshalText,
// it need to operation of dereference of pointer.
if field.Type.Kind() == reflect.Ptr &&
(field.Type.Implements(marshalJSONType) || field.Type.Implements(marshalTextType)) {
fieldType = rtype_ptrTo(fieldType)
}
}
fieldOpcodeIndex := ctx.opcodeIndex
fieldPtrIndex := ctx.ptrIndex
ctx.incIndex()
valueCode, err := encodeCompile(ctx.withType(fieldType))
if err != nil {
return nil, err
nilcheck := true
var valueCode *opcode
isNilValue := fieldType.Kind() == reflect.Ptr || fieldType.Kind() == reflect.Interface || fieldType.Kind() == reflect.Slice || fieldType.Kind() == reflect.Map
addrForMarshaler := false
if i == 0 && fieldNum == 1 && isPtr && !isNilValue && rtype_ptrTo(fieldType).Implements(marshalJSONType) && !fieldType.Implements(marshalJSONType) {
// *struct{ field T } => struct { field *T }
// func (*T) MarshalJSON() ([]byte, error)
// move pointer position from head to first field
code, err := encodeCompileMarshalJSON(ctx.withType(rtype_ptrTo(fieldType)))
if err != nil {
return nil, err
}
valueCode = code
nilcheck = false
indirect = false
disableIndirectConversion = true
} else if i == 0 && fieldNum == 1 && isPtr && !isNilValue && rtype_ptrTo(fieldType).Implements(marshalTextType) && !fieldType.Implements(marshalTextType) {
// *struct{ field T } => struct { field *T }
// func (*T) MarshalText() ([]byte, error)
// move pointer position from head to first field
code, err := encodeCompileMarshalText(ctx.withType(rtype_ptrTo(fieldType)))
if err != nil {
return nil, err
}
valueCode = code
nilcheck = false
indirect = false
disableIndirectConversion = true
} else if isPtr && !fieldType.Implements(marshalJSONType) && rtype_ptrTo(fieldType).Implements(marshalJSONType) {
// *struct{ field T }
// func (*T) MarshalJSON() ([]byte, error)
code, err := encodeCompileMarshalJSON(ctx.withType(fieldType))
if err != nil {
return nil, err
}
addrForMarshaler = true
nilcheck = false
valueCode = code
} else if isPtr && !fieldType.Implements(marshalTextType) && rtype_ptrTo(fieldType).Implements(marshalTextType) {
// *struct{ field T }
// func (*T) MarshalText() ([]byte, error)
code, err := encodeCompileMarshalText(ctx.withType(fieldType))
if err != nil {
return nil, err
}
addrForMarshaler = true
nilcheck = false
valueCode = code
} else {
code, err := encodeCompile(ctx.withType(fieldType), isPtr)
if err != nil {
return nil, err
}
valueCode = code
}
if field.Anonymous {
if valueCode.op == opPtr && valueCode.next.op == opStructFieldRecursive {
valueCode = valueCode.next
valueCode.decOpcodeIndex()
ctx.decIndex()
valueCode.op = opStructFieldPtrHeadRecursive
}
tagKey := ""
if tag.isTaggedKey {
tagKey = tag.key
@ -1259,21 +1301,32 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
for k, v := range encodeAnonymousStructFieldPairMap(tags, tagKey, valueCode) {
anonymousFields[k] = append(anonymousFields[k], v...)
}
valueCode.decIndent()
// fix issue144
if !(isPtr && strings.Contains(valueCode.op.String(), "Marshal")) {
valueCode.indirect = indirect
}
} else {
valueCode.indirect = indirect
}
key := fmt.Sprintf(`"%s":`, tag.key)
escapedKey := fmt.Sprintf(`%s:`, string(encodeEscapedString([]byte{}, tag.key)))
fieldCode := &opcode{
typ: valueCode.typ,
displayIdx: fieldOpcodeIndex,
idx: opcodeOffset(fieldPtrIndex),
next: valueCode,
indent: ctx.indent,
anonymousKey: field.Anonymous,
key: []byte(key),
escapedKey: []byte(escapedKey),
isTaggedKey: tag.isTaggedKey,
displayKey: tag.key,
offset: field.Offset,
typ: valueCode.typ,
displayIdx: fieldOpcodeIndex,
idx: opcodeOffset(fieldPtrIndex),
next: valueCode,
indent: ctx.indent,
anonymousKey: field.Anonymous,
key: []byte(key),
escapedKey: []byte(escapedKey),
isTaggedKey: tag.isTaggedKey,
displayKey: tag.key,
offset: field.Offset,
indirect: indirect,
nilcheck: nilcheck,
addrForMarshaler: addrForMarshaler,
}
if fieldIdx == 0 {
fieldCode.headIdx = fieldCode.idx
@ -1333,5 +1386,9 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
delete(ctx.structTypeToCompiledCode, typeptr)
if !disableIndirectConversion && !head.indirect && isPtr {
head.indirect = true
}
return ret, nil
}

View File

@ -9,19 +9,23 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
type opcode struct {
op opType // operation type
typ *rtype // go type
displayIdx int // opcode index
key []byte // struct field key
escapedKey []byte // struct field key ( HTML escaped )
ptrNum int // pointer number: e.g. double pointer is 2.
displayKey string // key text to display
isTaggedKey bool // whether tagged key
anonymousKey bool // whether anonymous key
root bool // whether root
rshiftNum uint8 // use to take bit for judging whether negative integer or not
mask uint64 // mask for number
indent int // indent number
op opType // operation type
typ *rtype // go type
displayIdx int // opcode index
key []byte // struct field key
escapedKey []byte // struct field key ( HTML escaped )
ptrNum int // pointer number: e.g. double pointer is 2.
displayKey string // key text to display
isTaggedKey bool // whether tagged key
anonymousKey bool // whether anonymous key
anonymousHead bool // whether anonymous head or not
root bool // whether root
indirect bool // whether indirect or not
nilcheck bool // whether needs to nilcheck or not
addrForMarshaler bool // whether needs to addr for marshaler or not
rshiftNum uint8 // use to take bit for judging whether negative integer or not
mask uint64 // mask for number
indent int // indent number
idx uintptr // offset to access ptr
headIdx uintptr // offset to access slice/struct head
@ -79,27 +83,31 @@ func (c *opcode) copy(codeMap map[uintptr]*opcode) *opcode {
return code
}
copied := &opcode{
op: c.op,
typ: c.typ,
displayIdx: c.displayIdx,
key: c.key,
escapedKey: c.escapedKey,
displayKey: c.displayKey,
ptrNum: c.ptrNum,
mask: c.mask,
rshiftNum: c.rshiftNum,
isTaggedKey: c.isTaggedKey,
anonymousKey: c.anonymousKey,
root: c.root,
indent: c.indent,
idx: c.idx,
headIdx: c.headIdx,
elemIdx: c.elemIdx,
length: c.length,
mapIter: c.mapIter,
mapPos: c.mapPos,
offset: c.offset,
size: c.size,
op: c.op,
typ: c.typ,
displayIdx: c.displayIdx,
key: c.key,
escapedKey: c.escapedKey,
displayKey: c.displayKey,
ptrNum: c.ptrNum,
mask: c.mask,
rshiftNum: c.rshiftNum,
isTaggedKey: c.isTaggedKey,
anonymousKey: c.anonymousKey,
anonymousHead: c.anonymousHead,
root: c.root,
indirect: c.indirect,
nilcheck: c.nilcheck,
addrForMarshaler: c.addrForMarshaler,
indent: c.indent,
idx: c.idx,
headIdx: c.headIdx,
elemIdx: c.elemIdx,
length: c.length,
mapIter: c.mapIter,
mapPos: c.mapPos,
offset: c.offset,
size: c.size,
}
codeMap[addr] = copied
copied.mapKey = c.mapKey.copy(codeMap)
@ -172,6 +180,18 @@ func (c *opcode) decOpcodeIndex() {
}
}
func (c *opcode) decIndent() {
for code := c; code.op != opEnd; {
code.indent--
switch code.op.codeType() {
case codeArrayElem, codeSliceElem, codeMapKey:
code = code.end
default:
code = code.next
}
}
}
func (c *opcode) dumpHead(code *opcode) string {
var length uintptr
if code.op.codeType() == codeArrayHead {
@ -360,7 +380,7 @@ func newSliceHeaderCode(ctx *encodeCompileContext) *opcode {
ctx.incPtrIndex()
length := opcodeOffset(ctx.ptrIndex)
return &opcode{
op: opSliceHead,
op: opSlice,
displayIdx: ctx.opcodeIndex,
idx: idx,
headIdx: idx,
@ -388,7 +408,7 @@ func newArrayHeaderCode(ctx *encodeCompileContext, alen int) *opcode {
ctx.incPtrIndex()
elemIdx := opcodeOffset(ctx.ptrIndex)
return &opcode{
op: opArrayHead,
op: opArray,
displayIdx: ctx.opcodeIndex,
idx: idx,
headIdx: idx,
@ -406,17 +426,12 @@ func newArrayElemCode(ctx *encodeCompileContext, head *opcode, length int, size
elemIdx: head.elemIdx,
headIdx: head.headIdx,
length: uintptr(length),
indent: ctx.indent,
size: size,
}
}
func newMapHeaderCode(ctx *encodeCompileContext, withLoad bool) *opcode {
var op opType
if withLoad {
op = opMapHeadLoad
} else {
op = opMapHead
}
func newMapHeaderCode(ctx *encodeCompileContext) *opcode {
idx := opcodeOffset(ctx.ptrIndex)
ctx.incPtrIndex()
elemIdx := opcodeOffset(ctx.ptrIndex)
@ -425,7 +440,7 @@ func newMapHeaderCode(ctx *encodeCompileContext, withLoad bool) *opcode {
ctx.incPtrIndex()
mapIter := opcodeOffset(ctx.ptrIndex)
return &opcode{
op: op,
op: opMap,
typ: ctx.typ,
displayIdx: ctx.opcodeIndex,
idx: idx,

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ package json_test
import (
"bytes"
"encoding"
stdjson "encoding/json"
"errors"
"fmt"
"log"
@ -1674,3 +1675,117 @@ func TestOmitEmpty(t *testing.T) {
t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
}
}
type testNullStr string
func (v *testNullStr) MarshalJSON() ([]byte, error) {
if *v == "" {
return []byte("null"), nil
}
return []byte(*v), nil
}
func TestIssue147(t *testing.T) {
type T struct {
Field1 string `json:"field1"`
Field2 testNullStr `json:"field2,omitempty"`
}
got, err := json.Marshal(T{
Field1: "a",
Field2: "b",
})
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(T{
Field1: "a",
Field2: "b",
})
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
type testIssue144 struct {
name string
number int64
}
func (v *testIssue144) MarshalJSON() ([]byte, error) {
if v.name != "" {
return json.Marshal(v.name)
}
return json.Marshal(v.number)
}
func TestIssue144(t *testing.T) {
type Embeded struct {
Field *testIssue144 `json:"field,omitempty"`
}
type T struct {
Embeded
}
{
v := T{
Embeded: Embeded{Field: &testIssue144{name: "hoge"}},
}
got, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(v)
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
{
v := &T{
Embeded: Embeded{Field: &testIssue144{name: "hoge"}},
}
got, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(v)
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
}
func TestIssue118(t *testing.T) {
type data struct {
Columns []string `json:"columns"`
Rows1 [][]string `json:"rows1"`
Rows2 [][]string `json:"rows2"`
}
v := data{Columns: []string{"1", "2", "3"}}
got, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.MarshalIndent(v, "", " ")
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
func TestIssue104(t *testing.T) {
type A struct {
Field1 string
Field2 int
Field3 float64
}
type T struct {
Field A
}
got, err := json.Marshal(T{})
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(T{})
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

36
json.go
View File

@ -2,8 +2,8 @@ package json
import (
"bytes"
"encoding/json"
"errors"
"strconv"
)
// Marshaler is the interface implemented by types that
@ -281,39 +281,7 @@ func UnmarshalNoEscape(data []byte, v interface{}) error {
type Token interface{}
// A Number represents a JSON number literal.
type Number string
// String returns the literal text of the number.
func (n Number) String() string { return string(n) }
// Float64 returns the number as a float64.
func (n Number) Float64() (float64, error) {
return strconv.ParseFloat(string(n), 64)
}
// Int64 returns the number as an int64.
func (n Number) Int64() (int64, error) {
return strconv.ParseInt(string(n), 10, 64)
}
func (n Number) MarshalJSON() ([]byte, error) {
if n == "" {
return []byte("0"), nil
}
if _, err := n.Float64(); err != nil {
return nil, err
}
return []byte(n), nil
}
func (n *Number) UnmarshalJSON(b []byte) error {
s := string(b)
if _, err := strconv.ParseFloat(s, 64); err != nil {
return &SyntaxError{msg: err.Error()}
}
*n = Number(s)
return nil
}
type Number = json.Number
// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can

View File

@ -244,6 +244,10 @@ func (t *rtype) Out(i int) reflect.Type {
return rtype_Out(t, i)
}
//go:linkname ifaceIndir reflect.ifaceIndir
//go:noescape
func ifaceIndir(*rtype) bool
//go:linkname rtype2type reflect.toType
//go:noescape
func rtype2type(t *rtype) reflect.Type