2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
25 jsoniter "github.com/json-iterator/go"
26 "github.com/modern-go/reflect2"
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"
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.
38 func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
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.
51 func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
60 type Serializer struct {
62 creater runtime.ObjectCreater
63 typer runtime.ObjectTyper
68 // Serializer implements Serializer
69 var _ runtime.Serializer = &Serializer{}
70 var _ recognizer.RecognizingDecoder = &Serializer{}
72 type customNumberExtension struct {
73 jsoniter.DummyExtension
76 func (cne *customNumberExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
77 if typ.String() == "interface {}" {
78 return customNumberDecoder{}
83 type customNumberDecoder struct {
86 func (customNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
87 switch iter.WhatIsNext() {
88 case jsoniter.NumberValue:
89 var number jsoniter.Number
91 i64, err := strconv.ParseInt(string(number), 10, 64)
93 *(*interface{})(ptr) = i64
96 f64, err := strconv.ParseFloat(string(number), 64)
98 *(*interface{})(ptr) = f64
101 iter.ReportError("DecodeNumber", err.Error())
103 *(*interface{})(ptr) = iter.Read()
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.
110 func CaseSensitiveJsonIterator() jsoniter.API {
111 config := jsoniter.Config{
114 ValidateJsonRawMessage: true,
117 // Force jsoniter to decode number to interface{} via int64/float64, if possible.
118 config.RegisterExtension(&customNumberExtension{})
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
126 var caseSensitiveJsonIterator = CaseSensitiveJsonIterator()
128 // gvkWithDefaults returns group kind and version defaulting from provided default
129 func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
130 if len(actual.Kind) == 0 {
131 actual.Kind = defaultGVK.Kind
133 if len(actual.Version) == 0 && len(actual.Group) == 0 {
134 actual.Group = defaultGVK.Group
135 actual.Version = defaultGVK.Version
137 if len(actual.Version) == 0 && actual.Group == defaultGVK.Group {
138 actual.Version = defaultGVK.Version
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
151 func (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)
156 return nil, actual, err
158 versioned.Objects = []runtime.Object{obj}
159 return versioned, actual, nil
164 altered, err := yaml.YAMLToJSON(data)
171 actual, err := s.meta.Interpret(data)
177 *actual = gvkWithDefaults(*actual, *gvk)
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
188 _, isUnstructured := into.(runtime.Unstructured)
189 types, _, err := s.typer.ObjectKinds(into)
191 case runtime.IsNotRegisteredError(err), isUnstructured:
192 if err := caseSensitiveJsonIterator.Unmarshal(data, into); err != nil {
193 return nil, actual, err
195 return into, actual, nil
197 return nil, actual, err
199 *actual = gvkWithDefaults(*actual, types[0])
203 if len(actual.Kind) == 0 {
204 return nil, actual, runtime.NewMissingKindErr(string(originalData))
206 if len(actual.Version) == 0 {
207 return nil, actual, runtime.NewMissingVersionErr(string(originalData))
210 // use the target if necessary
211 obj, err := runtime.UseOrCreateObject(s.typer, s.creater, *actual, into)
213 return nil, actual, err
216 if err := caseSensitiveJsonIterator.Unmarshal(data, obj); err != nil {
217 return nil, actual, err
219 return obj, actual, nil
222 // Encode serializes the provided object to the given writer.
223 func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
225 json, err := caseSensitiveJsonIterator.Marshal(obj)
229 data, err := yaml.JSONToYAML(json)
233 _, err = w.Write(data)
238 data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", " ")
242 _, err = w.Write(data)
245 encoder := json.NewEncoder(w)
246 return encoder.Encode(obj)
249 // RecognizesData implements the RecognizingDecoder interface.
250 func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
252 // we could potentially look for '---'
253 return false, true, nil
255 _, _, ok = utilyaml.GuessJSONStream(peek, 2048)
256 return ok, false, nil
259 // Framer is the default JSON framing behavior, with newlines delimiting individual objects.
260 var Framer = jsonFramer{}
262 type jsonFramer struct{}
264 // NewFrameWriter implements stream framing for this serializer
265 func (jsonFramer) NewFrameWriter(w io.Writer) io.Writer {
266 // we can write JSON objects directly to the writer, because they are self-framing
270 // NewFrameReader implements stream framing for this serializer
271 func (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)
276 // YAMLFramer is the default JSON framing behavior, with newlines delimiting individual objects.
277 var YAMLFramer = yamlFramer{}
279 type yamlFramer struct{}
281 // NewFrameWriter implements stream framing for this serializer
282 func (yamlFramer) NewFrameWriter(w io.Writer) io.Writer {
283 return yamlFrameWriter{w}
286 // NewFrameReader implements stream framing for this serializer
287 func (yamlFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser {
288 // extract the YAML document chunks directly
289 return utilyaml.NewDocumentDecoder(r)
292 type yamlFrameWriter struct {
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).
298 func (w yamlFrameWriter) Write(data []byte) (n int, err error) {
299 if _, err := w.w.Write([]byte("---\n")); err != nil {
302 return w.w.Write(data)