mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Add JSON encoding handlers to unstructured objects
This commit is contained in:
parent
69e0740b93
commit
361f13ddf0
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
@ -138,6 +139,21 @@ type Unstructured struct {
|
||||
Object map[string]interface{}
|
||||
}
|
||||
|
||||
// MarshalJSON ensures that the unstructured object produces proper
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *Unstructured) MarshalJSON() ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
err := UnstructuredJSONScheme.Encode(u, &buf)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
// UnmarshalJSON ensures that the unstructured object properly decodes
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *Unstructured) UnmarshalJSON(b []byte) error {
|
||||
_, _, err := UnstructuredJSONScheme.Decode(b, nil, u)
|
||||
return err
|
||||
}
|
||||
|
||||
func getNestedField(obj map[string]interface{}, fields ...string) interface{} {
|
||||
var val interface{} = obj
|
||||
for _, field := range fields {
|
||||
@ -450,6 +466,21 @@ type UnstructuredList struct {
|
||||
Items []*Unstructured `json:"items"`
|
||||
}
|
||||
|
||||
// MarshalJSON ensures that the unstructured list object produces proper
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *UnstructuredList) MarshalJSON() ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
err := UnstructuredJSONScheme.Encode(u, &buf)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
// UnmarshalJSON ensures that the unstructured list object properly
|
||||
// decodes JSON when passed to Go's standard JSON library.
|
||||
func (u *UnstructuredList) UnmarshalJSON(b []byte) error {
|
||||
_, _, err := UnstructuredJSONScheme.Decode(b, nil, u)
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) setNestedField(value interface{}, fields ...string) {
|
||||
if u.Object == nil {
|
||||
u.Object = make(map[string]interface{})
|
||||
|
Loading…
Reference in New Issue
Block a user