Compare commits

...

5 Commits

Author SHA1 Message Date
Fabio Nascimento Brandão 42a5eaf686
Merge b29e1ad08b into 3e9769d637 2024-11-22 06:54:47 +00:00
Masaaki Goshima 3e9769d637
Update go.yml 2024-11-11 12:13:39 +09:00
Andrey Grazhdankov 65c8b28ca1
Fix encode []*time.Time - check nil (#524) 2024-11-11 11:10:21 +09:00
Fabio Nascimento Brandão b29e1ad08b
Update struct.go 2023-08-21 12:33:52 -03:00
fbrandao 4bce081dac Fix for issue 464 2023-08-21 15:19:19 +00:00
6 changed files with 71 additions and 4 deletions

View File

@ -12,7 +12,7 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: build
run: docker-compose run go-json
run: docker compose run go-json
test:
name: Test

View File

@ -4057,3 +4057,28 @@ func TestIssue429(t *testing.T) {
}
}
}
func TestIssue464(t *testing.T) {
type testStruct struct {
Name string
Value int
Description string
}
jsonValue := []byte("{\"Name\":\"Test Name\",\"Value\":\"incorrect\",\"Description\":\"Test Description\"}")
jsonData := testStruct{}
err := stdjson.Unmarshal(jsonValue, &jsonData)
assertIsType(t, "Incorrect error type stdjson", &stdjson.UnmarshalTypeError{}, err)
gojsonData := testStruct{}
err = json.Unmarshal(jsonValue, &gojsonData)
assertIsType(t, "Incorrect error type go-json", &json.UnmarshalTypeError{}, err)
assertEq(t, "Incorrect name stdjson", "Test Name", jsonData.Name)
assertEq(t, "Incorrect name go-json", "Test Name", gojsonData.Name)
assertEq(t, "Incorrect value stdjson", 0, jsonData.Value)
assertEq(t, "Incorrect value go-json", 0, gojsonData.Value)
assertEq(t, "Incorrect description stdjson", "Test Description", jsonData.Description)
assertEq(t, "Incorrect description go-json", "Test Description", gojsonData.Description)
}

View File

@ -426,6 +426,11 @@ func Test_Marshal(t *testing.T) {
assertErr(t, err)
assertEq(t, "[]interface{}", `[1,2.1,"hello"]`, string(bytes))
})
t.Run("[]*time.Time", func(t *testing.T) {
bytes, err := json.Marshal([]*time.Time{nil})
assertErr(t, err)
assertEq(t, "[]*time.Time", `[null]`, string(bytes))
})
})
t.Run("array", func(t *testing.T) {

View File

@ -1,6 +1,9 @@
package json_test
import "testing"
import (
"reflect"
"testing"
)
func assertErr(t *testing.T, err error) {
t.Helper()
@ -22,3 +25,12 @@ func assertNeq(t *testing.T, msg string, exp interface{}, act interface{}) {
t.Fatalf("failed to test for %s. expected value is not [%v] but got same value", msg, act)
}
}
func assertIsType(t *testing.T, msg string, exp interface{}, act interface{}) {
t.Helper()
expType := reflect.TypeOf(exp)
actType := reflect.TypeOf(act)
if expType != actType {
t.Fatalf("failed to test for %s. exp[%v] bug act=[%v]", msg, expType, actType)
}
}

View File

@ -778,6 +778,7 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf
if firstWin {
seenFields = make(map[int]struct{}, d.fieldUniqueNameNum)
}
var lastTypeMismatchError error
for {
c, field, err := d.keyDecoder(d, buf, cursor)
if err != nil {
@ -804,9 +805,17 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf
cursor = c
} else {
c, err := field.dec.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil {
if _, ok := err.(*errors.UnmarshalTypeError); ok {
lastTypeMismatchError = err
c, err = skipValue(buf, cursor, depth)
if err != nil {
return 0, err
}
} else {
return 0, err
}
}
cursor = c
seenFieldNum++
if d.fieldUniqueNameNum <= seenFieldNum {
@ -816,9 +825,17 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf
}
} else {
c, err := field.dec.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil {
if _, ok := err.(*errors.UnmarshalTypeError); ok {
lastTypeMismatchError = err
c, err = skipValue(buf, cursor, depth)
if err != nil {
return 0, err
}
} else {
return 0, err
}
}
cursor = c
}
} else {
@ -831,6 +848,9 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf
cursor = skipWhiteSpace(buf, cursor)
if char(b, cursor) == '}' {
cursor++
if lastTypeMismatchError != nil {
return 0, lastTypeMismatchError
}
return cursor, nil
}
if char(b, cursor) != ',' {

View File

@ -406,6 +406,11 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
rv = newV
}
}
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return AppendNull(ctx, b), nil
}
v = rv.Interface()
var bb []byte
if (code.Flags & MarshalerContextFlags) != 0 {