@@ -17,16 +17,16 @@ limitations under the License.
17
17
package json
18
18
19
19
import (
20
- gojson "encoding/json"
20
+ "encoding/json"
21
21
"io"
22
22
23
23
"github.com/ghodss/yaml"
24
+ "github.com/ugorji/go/codec"
24
25
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/runtime/schema"
27
28
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
28
29
"k8s.io/apimachinery/pkg/util/framer"
29
- "k8s.io/apimachinery/pkg/util/json"
30
30
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
31
31
)
32
32
@@ -66,6 +66,36 @@ type Serializer struct {
66
66
var _ runtime.Serializer = & Serializer {}
67
67
var _ recognizer.RecognizingDecoder = & Serializer {}
68
68
69
+ func init () {
70
+ // Force jsoniter to decode number to interface{} via ints, if possible.
71
+ // decodeNumberAsInt64IfPossible := func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
72
+ // switch iter.WhatIsNext() {
73
+ // case jsoniter.NumberValue:
74
+ // var number json.Number
75
+ // iter.ReadVal(&number)
76
+ // u64, err := strconv.ParseUint(string(number), 10, 64)
77
+ // if err == nil {
78
+ // *(*interface{})(ptr) = u64
79
+ // return
80
+ // }
81
+ // i64, err := strconv.ParseInt(string(number), 10, 64)
82
+ // if err == nil {
83
+ // *(*interface{})(ptr) = i64
84
+ // return
85
+ // }
86
+ // f64, err := strconv.ParseFloat(string(number), 64)
87
+ // if err == nil {
88
+ // *(*interface{})(ptr) = f64
89
+ // return
90
+ // }
91
+ // // Not much we can do here.
92
+ // default:
93
+ // *(*interface{})(ptr) = iter.Read()
94
+ // }
95
+ // }
96
+ // jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
97
+ }
98
+
69
99
// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
70
100
// load that data into an object matching the desired schema kind or the provided into. If into is *runtime.Unknown, the raw data will be
71
101
// extracted and no decoding will be performed. If into is not registered with the typer, then the object will be straight decoded using
@@ -123,7 +153,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
123
153
switch {
124
154
case runtime .IsNotRegisteredError (err ), isUnstructured :
125
155
//if err := jsoniter.ConfigFastest.Unmarshal(data, into); err != nil {
126
- if err := json . Unmarshal (data , into ); err != nil {
156
+ if err := codec . NewDecoderBytes (data , new (codec. JsonHandle )). Decode ( into ); err != nil {
127
157
return nil , actual , err
128
158
}
129
159
return into , actual , nil
@@ -157,7 +187,8 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
157
187
return nil , actual , err
158
188
}
159
189
160
- if err := json .Unmarshal (data , obj ); err != nil {
190
+ // if err := jsoniter.ConfigFastest.Unmarshal(data, obj); err != nil {
191
+ if err := codec .NewDecoderBytes (data , new (codec.JsonHandle )).Decode (obj ); err != nil {
161
192
return nil , actual , err
162
193
}
163
194
return obj , actual , nil
@@ -179,7 +210,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
179
210
}
180
211
181
212
if s .pretty {
182
- data , err := gojson .MarshalIndent (obj , "" , " " )
213
+ data , err := json .MarshalIndent (obj , "" , " " )
183
214
if err != nil {
184
215
return err
185
216
}
0 commit comments