@@ -24,7 +24,7 @@ type emptyInterface struct {
24
24
ptr unsafe.Pointer
25
25
}
26
26
27
- func unmarshal (data []byte , v interface {}) error {
27
+ func unmarshal (data []byte , v interface {}, optFuncs ... DecodeOptionFunc ) error {
28
28
src := make ([]byte , len (data )+ 1 ) // append nul byte to the end
29
29
copy (src , data )
30
30
@@ -37,14 +37,22 @@ func unmarshal(data []byte, v interface{}) error {
37
37
if err != nil {
38
38
return err
39
39
}
40
- cursor , err := dec .Decode (src , 0 , 0 , header .ptr )
40
+ ctx := decoder .TakeRuntimeContext ()
41
+ ctx .Buf = src
42
+ ctx .Option .Flag = 0
43
+ for _ , optFunc := range optFuncs {
44
+ optFunc (ctx .Option )
45
+ }
46
+ cursor , err := dec .Decode (ctx , 0 , 0 , header .ptr )
41
47
if err != nil {
48
+ decoder .ReleaseRuntimeContext (ctx )
42
49
return err
43
50
}
51
+ decoder .ReleaseRuntimeContext (ctx )
44
52
return validateEndBuf (src , cursor )
45
53
}
46
54
47
- func unmarshalNoEscape (data []byte , v interface {}) error {
55
+ func unmarshalNoEscape (data []byte , v interface {}, optFuncs ... DecodeOptionFunc ) error {
48
56
src := make ([]byte , len (data )+ 1 ) // append nul byte to the end
49
57
copy (src , data )
50
58
@@ -57,10 +65,19 @@ func unmarshalNoEscape(data []byte, v interface{}) error {
57
65
if err != nil {
58
66
return err
59
67
}
60
- cursor , err := dec .Decode (src , 0 , 0 , noescape (header .ptr ))
68
+
69
+ ctx := decoder .TakeRuntimeContext ()
70
+ ctx .Buf = src
71
+ ctx .Option .Flag = 0
72
+ for _ , optFunc := range optFuncs {
73
+ optFunc (ctx .Option )
74
+ }
75
+ cursor , err := dec .Decode (ctx , 0 , 0 , noescape (header .ptr ))
61
76
if err != nil {
77
+ decoder .ReleaseRuntimeContext (ctx )
62
78
return err
63
79
}
80
+ decoder .ReleaseRuntimeContext (ctx )
64
81
return validateEndBuf (src , cursor )
65
82
}
66
83
@@ -117,6 +134,10 @@ func (d *Decoder) Buffered() io.Reader {
117
134
// See the documentation for Unmarshal for details about
118
135
// the conversion of JSON into a Go value.
119
136
func (d * Decoder ) Decode (v interface {}) error {
137
+ return d .DecodeWithOption (v )
138
+ }
139
+
140
+ func (d * Decoder ) DecodeWithOption (v interface {}, optFuncs ... DecodeOptionFunc ) error {
120
141
header := (* emptyInterface )(unsafe .Pointer (& v ))
121
142
typ := header .typ
122
143
ptr := uintptr (header .ptr )
@@ -136,6 +157,9 @@ func (d *Decoder) Decode(v interface{}) error {
136
157
return err
137
158
}
138
159
s := d .s
160
+ for _ , optFunc := range optFuncs {
161
+ optFunc (s .Option )
162
+ }
139
163
if err := dec .DecodeStream (s , 0 , header .ptr ); err != nil {
140
164
return err
141
165
}
0 commit comments