Commit 361f13dd authored by Kris's avatar Kris

Add JSON encoding handlers to unstructured objects

parent 69e0740b
...@@ -17,6 +17,7 @@ limitations under the License. ...@@ -17,6 +17,7 @@ limitations under the License.
package runtime package runtime
import ( import (
"bytes"
"fmt" "fmt"
"github.com/golang/glog" "github.com/golang/glog"
...@@ -138,6 +139,21 @@ type Unstructured struct { ...@@ -138,6 +139,21 @@ type Unstructured struct {
Object map[string]interface{} 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{} { func getNestedField(obj map[string]interface{}, fields ...string) interface{} {
var val interface{} = obj var val interface{} = obj
for _, field := range fields { for _, field := range fields {
...@@ -450,6 +466,21 @@ type UnstructuredList struct { ...@@ -450,6 +466,21 @@ type UnstructuredList struct {
Items []*Unstructured `json:"items"` 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) { func (u *UnstructuredList) setNestedField(value interface{}, fields ...string) {
if u.Object == nil { if u.Object == nil {
u.Object = make(map[string]interface{}) u.Object = make(map[string]interface{})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment