@@ -37,48 +37,54 @@ import (
37
37
// is not nil, the object has the group, version, and kind fields set.
38
38
// Deprecated: use NewSerializerWithOptions instead.
39
39
func NewSerializer (meta MetaFactory , creater runtime.ObjectCreater , typer runtime.ObjectTyper , pretty bool ) * Serializer {
40
- return NewSerializerWithOptions (meta , creater , typer , & SerializerOptions {false , pretty , false })
40
+ return NewSerializerWithOptions (meta , creater , typer , SerializerOptions {false , pretty , false })
41
41
}
42
42
43
43
// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
44
44
// is not nil, the object has the group, version, and kind fields set. This serializer supports only the subset of YAML that
45
45
// matches JSON, and will error if constructs are used that do not serialize to JSON.
46
46
// Deprecated: use NewSerializerWithOptions instead.
47
47
func NewYAMLSerializer (meta MetaFactory , creater runtime.ObjectCreater , typer runtime.ObjectTyper ) * Serializer {
48
- return NewSerializerWithOptions (meta , creater , typer , & SerializerOptions {true , false , false })
48
+ return NewSerializerWithOptions (meta , creater , typer , SerializerOptions {true , false , false })
49
49
}
50
50
51
51
// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
52
- // form. If typer is not nil, the object has the group, version, and kind fields set.
53
- func NewSerializerWithOptions (meta MetaFactory , creater runtime.ObjectCreater , typer runtime.ObjectTyper , serializerOptions * SerializerOptions ) * Serializer {
52
+ // form. If typer is not nil, the object has the group, version, and kind fields set. Options are copied into the Serializer
53
+ // and are immutable.
54
+ func NewSerializerWithOptions (meta MetaFactory , creater runtime.ObjectCreater , typer runtime.ObjectTyper , options SerializerOptions ) * Serializer {
54
55
return & Serializer {
55
- meta : meta ,
56
- creater : creater ,
57
- typer : typer ,
58
- SerializerOptions : serializerOptions ,
56
+ meta : meta ,
57
+ creater : creater ,
58
+ typer : typer ,
59
+ options : options ,
59
60
}
60
61
}
61
62
62
- // SerializerOptions holds the options which are used to creating a JSON/YAML serializer.
63
- // For example:
64
- // (1) we can creates a JSON serializer once we set `Yaml` to `false`.
65
- // (2) we can creates a YAML serializer once we set `Yaml` to `true`. This serializer supports only the subset of YAML that
66
- // matches JSON, and will error if constructs are used that do not serialize to JSON.
67
- // Please note that `Pretty` is silently ignored when `Yaml` is `true`.
68
- // (3) we can creates a strict JSON/YAML serializer that can also return errors of type strictDecodingError, once we set
69
- // `Strict` to `true`. And note that this serializer is not as performant as the non-strict variant, and should not be
70
- // used in fast paths.
63
+ // SerializerOptions holds the options which are used to configure a JSON/YAML serializer.
64
+ // example:
65
+ // (1) To configure a JSON serializer, set `Yaml` to `false`.
66
+ // (2) To configure a YAML serializer, set `Yaml` to `true`.
67
+ // (3) To configure a strict serializer that can return strictDecodingError, set `Strict` to `true`.
71
68
type SerializerOptions struct {
72
- Yaml bool
69
+ // Yaml: configures the Serializer to work with JSON(false) or YAML(true).
70
+ // When `Yaml` is enabled, this serializer only supports the subset of YAML that
71
+ // matches JSON, and will error if constructs are used that do not serialize to JSON.
72
+ Yaml bool
73
+
74
+ // Pretty: configures a JSON enabled Serializer(`Yaml: false`) to produce human-readable output.
75
+ // This option is silently ignored when `Yaml` is `true`.
73
76
Pretty bool
77
+
78
+ // Strict: configures the Serializer to return strictDecodingError's when duplicate fields are present decoding JSON or YAML.
79
+ // Note that enabling this option is not as performant as the non-strict variant, and should not be used in fast paths.
74
80
Strict bool
75
81
}
76
82
77
83
type Serializer struct {
78
84
meta MetaFactory
85
+ options SerializerOptions
79
86
creater runtime.ObjectCreater
80
87
typer runtime.ObjectTyper
81
- * SerializerOptions
82
88
}
83
89
84
90
// Serializer implements Serializer
@@ -193,7 +199,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
193
199
}
194
200
195
201
data := originalData
196
- if s .Yaml {
202
+ if s .options . Yaml {
197
203
altered , err := yaml .YAMLToJSON (data )
198
204
if err != nil {
199
205
return nil , nil , err
@@ -251,7 +257,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
251
257
}
252
258
253
259
// If the deserializer is non-strict, return successfully here.
254
- if ! s .Strict {
260
+ if ! s .options . Strict {
255
261
return obj , actual , nil
256
262
}
257
263
@@ -280,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
280
286
281
287
// Encode serializes the provided object to the given writer.
282
288
func (s * Serializer ) Encode (obj runtime.Object , w io.Writer ) error {
283
- if s .Yaml {
289
+ if s .options . Yaml {
284
290
json , err := caseSensitiveJsonIterator .Marshal (obj )
285
291
if err != nil {
286
292
return err
@@ -293,7 +299,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
293
299
return err
294
300
}
295
301
296
- if s .Pretty {
302
+ if s .options . Pretty {
297
303
data , err := caseSensitiveJsonIterator .MarshalIndent (obj , "" , " " )
298
304
if err != nil {
299
305
return err
@@ -307,7 +313,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
307
313
308
314
// RecognizesData implements the RecognizingDecoder interface.
309
315
func (s * Serializer ) RecognizesData (peek io.Reader ) (ok , unknown bool , err error ) {
310
- if s .Yaml {
316
+ if s .options . Yaml {
311
317
// we could potentially look for '---'
312
318
return false , true , nil
313
319
}
0 commit comments