-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecoders.go
67 lines (58 loc) · 1.33 KB
/
decoders.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dcode
import (
"encoding/json"
)
// Float64 returns a Decoder that can decode JSON directly
// into a float64
func Float64() Decoder[float64] {
return DecoderFunc[float64](
func(dc *json.Decoder) (float64, error) {
return unmarshal[float64](dc)
},
)
}
// Bool returns a decoder that can decode JSON
// into a bool
func Bool() Decoder[bool] {
return DecoderFunc[bool](
func(dc *json.Decoder) (bool, error) {
return unmarshal[bool](dc)
},
)
}
// Int returns a Decoder that can decode any JSON
// into an integer
func Int() Decoder[int] {
return DecoderFunc[int](
func(dc *json.Decoder) (int, error) {
return unmarshal[int](dc)
},
)
}
type MapT map[string]interface{}
func Map() Decoder[MapT] {
return DecoderFunc[MapT](
func(dc *json.Decoder) (MapT, error) {
return unmarshal[MapT](dc)
},
)
}
// String returns a Decoder that can decode JSON
// into a string
func String() Decoder[string] {
return DecoderFunc[string](
func(dc *json.Decoder) (string, error) {
return unmarshal[string](dc)
},
)
}
type IntermediateT json.RawMessage
// Intermediate returns a Decoder that decodes JSON
// into a a yet-to-be-determined type.
func Intermediate() Decoder[IntermediateT] {
return DecoderFunc[IntermediateT](
func(dc *json.Decoder) (IntermediateT, error) {
return unmarshal[IntermediateT](dc)
},
)
}