Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 The Kubernetes Authors. |
| 3 | |
| 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 |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package json |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | ) |
| 25 | |
| 26 | // NewEncoder delegates to json.NewEncoder |
| 27 | // It is only here so this package can be a drop-in for common encoding/json uses |
| 28 | func NewEncoder(w io.Writer) *json.Encoder { |
| 29 | return json.NewEncoder(w) |
| 30 | } |
| 31 | |
| 32 | // Marshal delegates to json.Marshal |
| 33 | // It is only here so this package can be a drop-in for common encoding/json uses |
| 34 | func Marshal(v interface{}) ([]byte, error) { |
| 35 | return json.Marshal(v) |
| 36 | } |
| 37 | |
| 38 | // limit recursive depth to prevent stack overflow errors |
| 39 | const maxDepth = 10000 |
| 40 | |
| 41 | // Unmarshal unmarshals the given data |
| 42 | // If v is a *map[string]interface{}, numbers are converted to int64 or float64 |
| 43 | func Unmarshal(data []byte, v interface{}) error { |
| 44 | switch v := v.(type) { |
| 45 | case *map[string]interface{}: |
| 46 | // Build a decoder from the given data |
| 47 | decoder := json.NewDecoder(bytes.NewBuffer(data)) |
| 48 | // Preserve numbers, rather than casting to float64 automatically |
| 49 | decoder.UseNumber() |
| 50 | // Run the decode |
| 51 | if err := decoder.Decode(v); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64 |
| 55 | return convertMapNumbers(*v, 0) |
| 56 | |
| 57 | case *[]interface{}: |
| 58 | // Build a decoder from the given data |
| 59 | decoder := json.NewDecoder(bytes.NewBuffer(data)) |
| 60 | // Preserve numbers, rather than casting to float64 automatically |
| 61 | decoder.UseNumber() |
| 62 | // Run the decode |
| 63 | if err := decoder.Decode(v); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64 |
| 67 | return convertSliceNumbers(*v, 0) |
| 68 | |
| 69 | case *interface{}: |
| 70 | // Build a decoder from the given data |
| 71 | decoder := json.NewDecoder(bytes.NewBuffer(data)) |
| 72 | // Preserve numbers, rather than casting to float64 automatically |
| 73 | decoder.UseNumber() |
| 74 | // Run the decode |
| 75 | if err := decoder.Decode(v); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64 |
| 79 | return convertInterfaceNumbers(v, 0) |
| 80 | |
| 81 | default: |
| 82 | return json.Unmarshal(data, v) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func convertInterfaceNumbers(v *interface{}, depth int) error { |
| 87 | var err error |
| 88 | switch v2 := (*v).(type) { |
| 89 | case json.Number: |
| 90 | *v, err = convertNumber(v2) |
| 91 | case map[string]interface{}: |
| 92 | err = convertMapNumbers(v2, depth+1) |
| 93 | case []interface{}: |
| 94 | err = convertSliceNumbers(v2, depth+1) |
| 95 | } |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | // convertMapNumbers traverses the map, converting any json.Number values to int64 or float64. |
| 100 | // values which are map[string]interface{} or []interface{} are recursively visited |
| 101 | func convertMapNumbers(m map[string]interface{}, depth int) error { |
| 102 | if depth > maxDepth { |
| 103 | return fmt.Errorf("exceeded max depth of %d", maxDepth) |
| 104 | } |
| 105 | |
| 106 | var err error |
| 107 | for k, v := range m { |
| 108 | switch v := v.(type) { |
| 109 | case json.Number: |
| 110 | m[k], err = convertNumber(v) |
| 111 | case map[string]interface{}: |
| 112 | err = convertMapNumbers(v, depth+1) |
| 113 | case []interface{}: |
| 114 | err = convertSliceNumbers(v, depth+1) |
| 115 | } |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | } |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // convertSliceNumbers traverses the slice, converting any json.Number values to int64 or float64. |
| 124 | // values which are map[string]interface{} or []interface{} are recursively visited |
| 125 | func convertSliceNumbers(s []interface{}, depth int) error { |
| 126 | if depth > maxDepth { |
| 127 | return fmt.Errorf("exceeded max depth of %d", maxDepth) |
| 128 | } |
| 129 | |
| 130 | var err error |
| 131 | for i, v := range s { |
| 132 | switch v := v.(type) { |
| 133 | case json.Number: |
| 134 | s[i], err = convertNumber(v) |
| 135 | case map[string]interface{}: |
| 136 | err = convertMapNumbers(v, depth+1) |
| 137 | case []interface{}: |
| 138 | err = convertSliceNumbers(v, depth+1) |
| 139 | } |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | } |
| 144 | return nil |
| 145 | } |
| 146 | |
| 147 | // convertNumber converts a json.Number to an int64 or float64, or returns an error |
| 148 | func convertNumber(n json.Number) (interface{}, error) { |
| 149 | // Attempt to convert to an int64 first |
| 150 | if i, err := n.Int64(); err == nil { |
| 151 | return i, nil |
| 152 | } |
| 153 | // Return a float64 (default json.Decode() behavior) |
| 154 | // An overflow will return an error |
| 155 | return n.Float64() |
| 156 | } |