Skip to content

Commit c5fba8c

Browse files
authored
Merge pull request #739 from devlights/add-json-disallow-unknown-fields-example
2 parents 5ac87c7 + 66c2f6f commit c5fba8c

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-15
lines changed

examples/basic/jsonop/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
| ------------------------- | --------------------------- | ----------------------------------------------------------------- |
7-
| marshal_nonindent.go | json_marshal_non_indent | json.Marshal() を利用したサンプルです. |
8-
| marshal_indent.go | json_marshal_indent | json.MarshalIndent() を利用したサンプルです. |
9-
| marshal_slice.go | json_marshal_slice | json.Marshal() で スライス をマーシャルした場合のサンプルです. |
10-
| marshal_slice.go | json_marshal_map | json.Marshal() で マップ をマーシャルした場合のサンプルです. |
11-
| marshal_date_rfc3339.go | json_marshal_date_rfc3339 | RFC3339形式の日付を json.Marshal するサンプルです. |
12-
| marshal_date_custom.go | json_marshal_date_custom | json.Marshal にて 独自の日付形式 を利用するサンプルです. |
13-
| unmarshal_struct.go | json_unmarshal_struct | json.Unmarshal() を利用したサンプルです. |
14-
| unmarshal_slice.go | json_unmarshal_slice | json 配列 を json.Unmarshal した場合のサンプルです. |
15-
| unmarshal_map.go | json_unmarshal_map | json マップ を json.Unmarshal した場合のサンプルです. |
16-
| unmarshal_date_rfc3339.go | json_unmarshal_date_rfc3339 | RFC3339形式の日付文字列 を json.Unmarshal した場合のサンプルです. |
17-
| unmarshal_date_custom.go | json_unmarshal_date_custom | 独自の日付文字列 を json.Unmarshal した場合のサンプルです. |
18-
| decoder.go | json_decoder | json.NewDecoder を使ったサンプルです. |
19-
| encoder.go | json_encoder | json.Encoder を使ったサンプルです. |
5+
| file | example name | note |
6+
| -------------------------- | ---------------------------- | ----------------------------------------------------------------- |
7+
| marshal_nonindent.go | json_marshal_non_indent | json.Marshal() を利用したサンプルです. |
8+
| marshal_indent.go | json_marshal_indent | json.MarshalIndent() を利用したサンプルです. |
9+
| marshal_slice.go | json_marshal_slice | json.Marshal() で スライス をマーシャルした場合のサンプルです. |
10+
| marshal_slice.go | json_marshal_map | json.Marshal() で マップ をマーシャルした場合のサンプルです. |
11+
| marshal_date_rfc3339.go | json_marshal_date_rfc3339 | RFC3339形式の日付を json.Marshal するサンプルです. |
12+
| marshal_date_custom.go | json_marshal_date_custom | json.Marshal にて 独自の日付形式 を利用するサンプルです. |
13+
| unmarshal_struct.go | json_unmarshal_struct | json.Unmarshal() を利用したサンプルです. |
14+
| unmarshal_slice.go | json_unmarshal_slice | json 配列 を json.Unmarshal した場合のサンプルです. |
15+
| unmarshal_map.go | json_unmarshal_map | json マップ を json.Unmarshal した場合のサンプルです. |
16+
| unmarshal_date_rfc3339.go | json_unmarshal_date_rfc3339 | RFC3339形式の日付文字列 を json.Unmarshal した場合のサンプルです. |
17+
| unmarshal_date_custom.go | json_unmarshal_date_custom | 独自の日付文字列 を json.Unmarshal した場合のサンプルです. |
18+
| decoder.go | json_decoder | json.NewDecoder を使ったサンプルです. |
19+
| encoder.go | json_encoder | json.Encoder を使ったサンプルです. |
20+
| disallow_unknown_fields.go | json_disallow_unknown_fields | *Decoder.DisallowUnknownFields のサンプルです。 |
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package jsonop
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io"
7+
"strings"
8+
9+
"github.com/devlights/gomy/output"
10+
)
11+
12+
// DisallowUnknownFields は、*Decoder.DisallowUnknownFields のサンプルです。
13+
//
14+
// # REFERENCES
15+
// - https://pkg.go.dev/encoding/json@go1.21.6#Decoder.DisallowUnknownFields
16+
func DisallowUnknownFields() error {
17+
const (
18+
jsonValue = `{"id": 1, "name": "Test1", "age": 99}`
19+
)
20+
21+
type Val struct {
22+
Id int `json:"id"`
23+
Name string `json:"name"`
24+
}
25+
26+
var (
27+
stream = strings.NewReader(jsonValue)
28+
dec = json.NewDecoder(stream)
29+
)
30+
31+
//
32+
// 普通にデコード
33+
//
34+
// *json.Decoder.DisallowUnknownFields() を呼んでいないので
35+
// 存在しないJSONフィールドがあってもエラーにはならない。
36+
//
37+
var (
38+
v Val
39+
err error
40+
)
41+
42+
err = dec.Decode(&v)
43+
if err != nil && !errors.Is(err, io.EOF) {
44+
return err
45+
}
46+
47+
output.Stdoutf("[Normal]", "%v\n", v)
48+
49+
//
50+
// 不明なフィールドは許可しないよう設定
51+
//
52+
// この場合の「不明なフィールド」というのは
53+
// "JSON側に存在しているフィールドが、受け側の構造体に存在しない場合" という意味。
54+
// 構造体側に存在しているフィールドが、JSON側に存在しないのはエラーにならないので注意。
55+
//
56+
// 今度は、ageというJSONフィールドに対応する構造体フィールドが存在しないのでエラーとなる。
57+
//
58+
stream = strings.NewReader(jsonValue)
59+
dec = json.NewDecoder(stream)
60+
61+
dec.DisallowUnknownFields()
62+
63+
err = dec.Decode(&v)
64+
if err != nil && !errors.Is(err, io.EOF) {
65+
output.Stdoutf("[DisallowUnknownFields]", "%v(%T)", err, err)
66+
}
67+
68+
return nil
69+
70+
/*
71+
$ task
72+
task: [build] go build .
73+
task: [run] ./try-golang -onetime
74+
75+
ENTER EXAMPLE NAME: json_disallow_unknown_fields
76+
77+
[Name] "json_disallow_unknown_fields"
78+
[Normal] {1 Test1}
79+
[DisallowUnknownFields] json: unknown field "age"(*errors.errorString)
80+
81+
[Elapsed] 158.11µs
82+
*/
83+
}

examples/basic/jsonop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ func (*register) Regist(m mapping.ExampleMapping) {
2828
m["json_unmarshal_date_custom"] = UnmarshalDateCustom
2929
m["json_decoder"] = Decoder
3030
m["json_encoder"] = Encoder
31+
m["json_disallow_unknown_fields"] = DisallowUnknownFields
3132
}

0 commit comments

Comments
 (0)