Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue 464 #465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4050,3 +4050,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)
}
14 changes: 13 additions & 1 deletion helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package json_test

import "testing"
import (
"reflect"
"testing"
)

func assertErr(t *testing.T, err error) {
t.Helper()
Expand All @@ -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)
}
}
24 changes: 22 additions & 2 deletions internal/decoder/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -805,7 +806,15 @@ 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 {
return 0, err
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++
Expand All @@ -817,7 +826,15 @@ 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 {
return 0, err
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
}
Expand All @@ -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) != ',' {
Expand Down