blob: 382c4858e7fbe5de02fd7a646f30bb7f456dc433 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package json
18
19import (
20 "encoding/json"
21 "io"
22 "strconv"
23 "unsafe"
24
25 "github.com/ghodss/yaml"
26 jsoniter "github.com/json-iterator/go"
27 "github.com/modern-go/reflect2"
28
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/apimachinery/pkg/runtime/schema"
31 "k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
32 "k8s.io/apimachinery/pkg/util/framer"
33 utilyaml "k8s.io/apimachinery/pkg/util/yaml"
34)
35
36// NewSerializer creates a JSON serializer that handles encoding versioned objects into the proper JSON form. If typer
37// is not nil, the object has the group, version, and kind fields set.
38func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
39 return &Serializer{
40 meta: meta,
41 creater: creater,
42 typer: typer,
43 yaml: false,
44 pretty: pretty,
45 }
46}
47
48// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
49// is not nil, the object has the group, version, and kind fields set. This serializer supports only the subset of YAML that
50// matches JSON, and will error if constructs are used that do not serialize to JSON.
51func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
52 return &Serializer{
53 meta: meta,
54 creater: creater,
55 typer: typer,
56 yaml: true,
57 }
58}
59
60type Serializer struct {
61 meta MetaFactory
62 creater runtime.ObjectCreater
63 typer runtime.ObjectTyper
64 yaml bool
65 pretty bool
66}
67
68// Serializer implements Serializer
69var _ runtime.Serializer = &Serializer{}
70var _ recognizer.RecognizingDecoder = &Serializer{}
71
72type customNumberExtension struct {
73 jsoniter.DummyExtension
74}
75
76func (cne *customNumberExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
77 if typ.String() == "interface {}" {
78 return customNumberDecoder{}
79 }
80 return nil
81}
82
83type customNumberDecoder struct {
84}
85
86func (customNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
87 switch iter.WhatIsNext() {
88 case jsoniter.NumberValue:
89 var number jsoniter.Number
90 iter.ReadVal(&number)
91 i64, err := strconv.ParseInt(string(number), 10, 64)
92 if err == nil {
93 *(*interface{})(ptr) = i64
94 return
95 }
96 f64, err := strconv.ParseFloat(string(number), 64)
97 if err == nil {
98 *(*interface{})(ptr) = f64
99 return
100 }
101 iter.ReportError("DecodeNumber", err.Error())
102 default:
103 *(*interface{})(ptr) = iter.Read()
104 }
105}
106
107// CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
108// case-sensitive when unmarshalling, and otherwise compatible with
109// the encoding/json standard library.
110func CaseSensitiveJsonIterator() jsoniter.API {
111 config := jsoniter.Config{
112 EscapeHTML: true,
113 SortMapKeys: true,
114 ValidateJsonRawMessage: true,
115 CaseSensitive: true,
116 }.Froze()
117 // Force jsoniter to decode number to interface{} via int64/float64, if possible.
118 config.RegisterExtension(&customNumberExtension{})
119 return config
120}
121
122// Private copy of jsoniter to try to shield against possible mutations
123// from outside. Still does not protect from package level jsoniter.Register*() functions - someone calling them
124// in some other library will mess with every usage of the jsoniter library in the whole program.
125// See https://github.com/json-iterator/go/issues/265
126var caseSensitiveJsonIterator = CaseSensitiveJsonIterator()
127
128// gvkWithDefaults returns group kind and version defaulting from provided default
129func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
130 if len(actual.Kind) == 0 {
131 actual.Kind = defaultGVK.Kind
132 }
133 if len(actual.Version) == 0 && len(actual.Group) == 0 {
134 actual.Group = defaultGVK.Group
135 actual.Version = defaultGVK.Version
136 }
137 if len(actual.Version) == 0 && actual.Group == defaultGVK.Group {
138 actual.Version = defaultGVK.Version
139 }
140 return actual
141}
142
143// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
144// load that data into an object matching the desired schema kind or the provided into.
145// If into is *runtime.Unknown, the raw data will be extracted and no decoding will be performed.
146// If into is not registered with the typer, then the object will be straight decoded using normal JSON/YAML unmarshalling.
147// If into is provided and the original data is not fully qualified with kind/version/group, the type of the into will be used to alter the returned gvk.
148// If into is nil or data's gvk different from into's gvk, it will generate a new Object with ObjectCreater.New(gvk)
149// On success or most errors, the method will return the calculated schema kind.
150// The gvk calculate priority will be originalData > default gvk > into
151func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
152 if versioned, ok := into.(*runtime.VersionedObjects); ok {
153 into = versioned.Last()
154 obj, actual, err := s.Decode(originalData, gvk, into)
155 if err != nil {
156 return nil, actual, err
157 }
158 versioned.Objects = []runtime.Object{obj}
159 return versioned, actual, nil
160 }
161
162 data := originalData
163 if s.yaml {
164 altered, err := yaml.YAMLToJSON(data)
165 if err != nil {
166 return nil, nil, err
167 }
168 data = altered
169 }
170
171 actual, err := s.meta.Interpret(data)
172 if err != nil {
173 return nil, nil, err
174 }
175
176 if gvk != nil {
177 *actual = gvkWithDefaults(*actual, *gvk)
178 }
179
180 if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
181 unk.Raw = originalData
182 unk.ContentType = runtime.ContentTypeJSON
183 unk.GetObjectKind().SetGroupVersionKind(*actual)
184 return unk, actual, nil
185 }
186
187 if into != nil {
188 _, isUnstructured := into.(runtime.Unstructured)
189 types, _, err := s.typer.ObjectKinds(into)
190 switch {
191 case runtime.IsNotRegisteredError(err), isUnstructured:
192 if err := caseSensitiveJsonIterator.Unmarshal(data, into); err != nil {
193 return nil, actual, err
194 }
195 return into, actual, nil
196 case err != nil:
197 return nil, actual, err
198 default:
199 *actual = gvkWithDefaults(*actual, types[0])
200 }
201 }
202
203 if len(actual.Kind) == 0 {
204 return nil, actual, runtime.NewMissingKindErr(string(originalData))
205 }
206 if len(actual.Version) == 0 {
207 return nil, actual, runtime.NewMissingVersionErr(string(originalData))
208 }
209
210 // use the target if necessary
211 obj, err := runtime.UseOrCreateObject(s.typer, s.creater, *actual, into)
212 if err != nil {
213 return nil, actual, err
214 }
215
216 if err := caseSensitiveJsonIterator.Unmarshal(data, obj); err != nil {
217 return nil, actual, err
218 }
219 return obj, actual, nil
220}
221
222// Encode serializes the provided object to the given writer.
223func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
224 if s.yaml {
225 json, err := caseSensitiveJsonIterator.Marshal(obj)
226 if err != nil {
227 return err
228 }
229 data, err := yaml.JSONToYAML(json)
230 if err != nil {
231 return err
232 }
233 _, err = w.Write(data)
234 return err
235 }
236
237 if s.pretty {
238 data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", " ")
239 if err != nil {
240 return err
241 }
242 _, err = w.Write(data)
243 return err
244 }
245 encoder := json.NewEncoder(w)
246 return encoder.Encode(obj)
247}
248
249// RecognizesData implements the RecognizingDecoder interface.
250func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
251 if s.yaml {
252 // we could potentially look for '---'
253 return false, true, nil
254 }
255 _, _, ok = utilyaml.GuessJSONStream(peek, 2048)
256 return ok, false, nil
257}
258
259// Framer is the default JSON framing behavior, with newlines delimiting individual objects.
260var Framer = jsonFramer{}
261
262type jsonFramer struct{}
263
264// NewFrameWriter implements stream framing for this serializer
265func (jsonFramer) NewFrameWriter(w io.Writer) io.Writer {
266 // we can write JSON objects directly to the writer, because they are self-framing
267 return w
268}
269
270// NewFrameReader implements stream framing for this serializer
271func (jsonFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser {
272 // we need to extract the JSON chunks of data to pass to Decode()
273 return framer.NewJSONFramedReader(r)
274}
275
276// YAMLFramer is the default JSON framing behavior, with newlines delimiting individual objects.
277var YAMLFramer = yamlFramer{}
278
279type yamlFramer struct{}
280
281// NewFrameWriter implements stream framing for this serializer
282func (yamlFramer) NewFrameWriter(w io.Writer) io.Writer {
283 return yamlFrameWriter{w}
284}
285
286// NewFrameReader implements stream framing for this serializer
287func (yamlFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser {
288 // extract the YAML document chunks directly
289 return utilyaml.NewDocumentDecoder(r)
290}
291
292type yamlFrameWriter struct {
293 w io.Writer
294}
295
296// Write separates each document with the YAML document separator (`---` followed by line
297// break). Writers must write well formed YAML documents (include a final line break).
298func (w yamlFrameWriter) Write(data []byte) (n int, err error) {
299 if _, err := w.w.Write([]byte("---\n")); err != nil {
300 return 0, err
301 }
302 return w.w.Write(data)
303}