ConvertJSONNumbers converts json.Number values to javascript number objects for use in the sync function. Integers that would lose precision are left as json.Number, as are floats that can't be converted to float64.
(value interface{})
| 81 | // function. Integers that would lose precision are left as json.Number, as are floats that can't be |
| 82 | // converted to float64. |
| 83 | func ConvertJSONNumbers(value interface{}) interface{} { |
| 84 | switch value := value.(type) { |
| 85 | case json.Number: |
| 86 | if asInt, err := value.Int64(); err == nil { |
| 87 | if asInt > JavascriptMaxSafeInt || asInt < JavascriptMinSafeInt { |
| 88 | // Integer will lose precision when used in javascript - leave as json.Number |
| 89 | return value |
| 90 | } |
| 91 | return asInt |
| 92 | } else { |
| 93 | numErr, _ := err.(*strconv.NumError) |
| 94 | if numErr.Err == strconv.ErrRange { |
| 95 | return value |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if asFloat, err := value.Float64(); err == nil { |
| 100 | // Can't reliably detect loss of precision in float, due to number of variations in input float format |
| 101 | return asFloat |
| 102 | } |
| 103 | return value |
| 104 | case map[string]interface{}: |
| 105 | for k, v := range value { |
| 106 | value[k] = ConvertJSONNumbers(v) |
| 107 | } |
| 108 | case []interface{}: |
| 109 | for i, v := range value { |
| 110 | value[i] = ConvertJSONNumbers(v) |
| 111 | } |
| 112 | default: |
| 113 | } |
| 114 | return value |
| 115 | } |
| 116 | |
| 117 | //////// UTILITY FUNCTIONS: |
| 118 |
no outgoing calls
no test coverage detected