...
1
16
17 package spec3
18
19 import (
20 "encoding/json"
21
22 "github.com/go-openapi/swag"
23 "k8s.io/kube-openapi/pkg/internal"
24 jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
25 "k8s.io/kube-openapi/pkg/validation/spec"
26 )
27
28 type Encoding struct {
29 EncodingProps
30 spec.VendorExtensible
31 }
32
33
34 func (e *Encoding) MarshalJSON() ([]byte, error) {
35 if internal.UseOptimizedJSONMarshalingV3 {
36 return internal.DeterministicMarshal(e)
37 }
38 b1, err := json.Marshal(e.EncodingProps)
39 if err != nil {
40 return nil, err
41 }
42 b2, err := json.Marshal(e.VendorExtensible)
43 if err != nil {
44 return nil, err
45 }
46 return swag.ConcatJSON(b1, b2), nil
47 }
48
49 func (e *Encoding) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
50 var x struct {
51 EncodingProps encodingPropsOmitZero `json:",inline"`
52 spec.Extensions
53 }
54 x.Extensions = internal.SanitizeExtensions(e.Extensions)
55 x.EncodingProps = encodingPropsOmitZero(e.EncodingProps)
56 return opts.MarshalNext(enc, x)
57 }
58
59 func (e *Encoding) UnmarshalJSON(data []byte) error {
60 if internal.UseOptimizedJSONUnmarshalingV3 {
61 return jsonv2.Unmarshal(data, e)
62 }
63 if err := json.Unmarshal(data, &e.EncodingProps); err != nil {
64 return err
65 }
66 if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
67 return err
68 }
69 return nil
70 }
71
72 func (e *Encoding) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
73 var x struct {
74 spec.Extensions
75 EncodingProps
76 }
77 if err := opts.UnmarshalNext(dec, &x); err != nil {
78 return err
79 }
80
81 e.Extensions = internal.SanitizeExtensions(x.Extensions)
82 e.EncodingProps = x.EncodingProps
83 return nil
84 }
85
86 type EncodingProps struct {
87
88 ContentType string `json:"contentType,omitempty"`
89
90 Headers map[string]*Header `json:"headers,omitempty"`
91
92 Style string `json:"style,omitempty"`
93
94 Explode bool `json:"explode,omitempty"`
95
96 AllowReserved bool `json:"allowReserved,omitempty"`
97 }
98
99 type encodingPropsOmitZero struct {
100 ContentType string `json:"contentType,omitempty"`
101 Headers map[string]*Header `json:"headers,omitempty"`
102 Style string `json:"style,omitempty"`
103 Explode bool `json:"explode,omitzero"`
104 AllowReserved bool `json:"allowReserved,omitzero"`
105 }
106
View as plain text