From 361f13ddf00cae87ed0e2ae57d4ba963297e9ab1 Mon Sep 17 00:00:00 2001 From: Kris Date: Mon, 15 Aug 2016 22:06:11 -0700 Subject: [PATCH] Add JSON encoding handlers to unstructured objects --- pkg/runtime/types.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/runtime/types.go b/pkg/runtime/types.go index 5033c0dc2c9..239c65b9d87 100644 --- a/pkg/runtime/types.go +++ b/pkg/runtime/types.go @@ -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{})