summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/util/json/json.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/util/json/json.go b/vendor/k8s.io/apimachinery/pkg/util/json/json.go
index 0e2e30175..204834883 100644
--- a/vendor/k8s.io/apimachinery/pkg/util/json/json.go
+++ b/vendor/k8s.io/apimachinery/pkg/util/json/json.go
@@ -66,11 +66,36 @@ func Unmarshal(data []byte, v interface{}) error {
// If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
return convertSliceNumbers(*v, 0)
+ case *interface{}:
+ // Build a decoder from the given data
+ decoder := json.NewDecoder(bytes.NewBuffer(data))
+ // Preserve numbers, rather than casting to float64 automatically
+ decoder.UseNumber()
+ // Run the decode
+ if err := decoder.Decode(v); err != nil {
+ return err
+ }
+ // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
+ return convertInterfaceNumbers(v, 0)
+
default:
return json.Unmarshal(data, v)
}
}
+func convertInterfaceNumbers(v *interface{}, depth int) error {
+ var err error
+ switch v2 := (*v).(type) {
+ case json.Number:
+ *v, err = convertNumber(v2)
+ case map[string]interface{}:
+ err = convertMapNumbers(v2, depth+1)
+ case []interface{}:
+ err = convertSliceNumbers(v2, depth+1)
+ }
+ return err
+}
+
// convertMapNumbers traverses the map, converting any json.Number values to int64 or float64.
// values which are map[string]interface{} or []interface{} are recursively visited
func convertMapNumbers(m map[string]interface{}, depth int) error {