Convert json numbers to numbers (#5496)

This commit is contained in:
WithoutPants
2024-11-22 08:27:23 +11:00
committed by GitHub
parent 5f690d96bd
commit 6c5bf5f052
4 changed files with 104 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package utils
import (
"encoding/json"
"strings"
)
@@ -79,3 +80,19 @@ func MergeMaps(dest map[string]interface{}, src map[string]interface{}) {
dest[k] = v
}
}
// ConvertMapJSONNumbers converts all JSON numbers in a map to either float64 or int64.
func ConvertMapJSONNumbers(m map[string]interface{}) (ret map[string]interface{}) {
ret = make(map[string]interface{})
for k, v := range m {
if n, ok := v.(json.Number); ok {
ret[k] = JSONNumberToNumber(n)
} else if mm, ok := v.(map[string]interface{}); ok {
ret[k] = ConvertMapJSONNumbers(mm)
} else {
ret[k] = v
}
}
return ret
}