forked from clbanning/checkjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unknownfieldserr_test.go
45 lines (39 loc) · 954 Bytes
/
unknownfieldserr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// +build go1.10
package checkjson
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
// Per go1.10 (*Decoder)DisallowUnknownFields seems to mimic
// checkjson.UnknownKeys() in its net effect. What does it
// really do?
func TestDisallowUnknFields(t *testing.T) {
fmt.Println("===================== TestDisallowUnknFields ...")
type test2 struct {
Maybe bool
}
type test struct {
Ok bool
Why test2
}
tst := test{}
data := []byte(`{"ok":true, "why":{"maybe":true,"maybenot":false}, "not":"I don't know"}`)
r := bytes.NewReader(data)
d := json.NewDecoder(r)
d.DisallowUnknownFields()
if err := d.Decode(&tst); err != nil {
fmt.Println("err ok:", err)
} else {
t.Fatal("no err on decode with DisallowUnknownFields")
}
tst = test{}
data = []byte(`{"ok":true, "why":{"maybe":true}}`)
r = bytes.NewReader(data)
d = json.NewDecoder(r)
d.DisallowUnknownFields()
if err := d.Decode(&tst); err != nil {
t.Fatal(err)
}
}