2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
25 // NewEncoder delegates to json.NewEncoder
26 // It is only here so this package can be a drop-in for common encoding/json uses
27 func NewEncoder(w io.Writer) *json.Encoder {
28 return json.NewEncoder(w)
31 // Marshal delegates to json.Marshal
32 // It is only here so this package can be a drop-in for common encoding/json uses
33 func Marshal(v interface{}) ([]byte, error) {
34 return json.Marshal(v)
37 // Unmarshal unmarshals the given data
38 // If v is a *map[string]interface{}, numbers are converted to int64 or float64
39 func Unmarshal(data []byte, v interface{}) error {
40 switch v := v.(type) {
41 case *map[string]interface{}:
42 // Build a decoder from the given data
43 decoder := json.NewDecoder(bytes.NewBuffer(data))
44 // Preserve numbers, rather than casting to float64 automatically
47 if err := decoder.Decode(v); err != nil {
50 // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
51 return convertMapNumbers(*v)
54 // Build a decoder from the given data
55 decoder := json.NewDecoder(bytes.NewBuffer(data))
56 // Preserve numbers, rather than casting to float64 automatically
59 if err := decoder.Decode(v); err != nil {
62 // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
63 return convertSliceNumbers(*v)
66 return json.Unmarshal(data, v)
70 // convertMapNumbers traverses the map, converting any json.Number values to int64 or float64.
71 // values which are map[string]interface{} or []interface{} are recursively visited
72 func convertMapNumbers(m map[string]interface{}) error {
75 switch v := v.(type) {
77 m[k], err = convertNumber(v)
78 case map[string]interface{}:
79 err = convertMapNumbers(v)
81 err = convertSliceNumbers(v)
90 // convertSliceNumbers traverses the slice, converting any json.Number values to int64 or float64.
91 // values which are map[string]interface{} or []interface{} are recursively visited
92 func convertSliceNumbers(s []interface{}) error {
95 switch v := v.(type) {
97 s[i], err = convertNumber(v)
98 case map[string]interface{}:
99 err = convertMapNumbers(v)
101 err = convertSliceNumbers(v)
110 // convertNumber converts a json.Number to an int64 or float64, or returns an error
111 func convertNumber(n json.Number) (interface{}, error) {
112 // Attempt to convert to an int64 first
113 if i, err := n.Int64(); err == nil {
116 // Return a float64 (default json.Decode() behavior)
117 // An overflow will return an error