-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcast.go
More file actions
38 lines (34 loc) · 841 Bytes
/
cast.go
File metadata and controls
38 lines (34 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cast
import (
"github.com/ohler55/ojg/oj"
"github.com/spf13/cast"
)
func isFloatAnInteger(val float64) bool {
return val == float64(int(val))
}
// ToCorrectType will convert the given type to correct type.
// We are only worried about types that are supported by JSON.
func ToCorrectType(input any) any {
if input == nil {
return nil
}
switch input.(type) {
case bool:
return cast.ToBool(input)
case int:
return cast.ToInt(input)
case float64:
value := cast.ToFloat64(input)
// When parsing JSON without struct, the actual int will be marked as float64
// To check if float64 is actually int we have to add this check
if isFloatAnInteger(value) {
return cast.ToInt(input)
}
return value
case string:
return cast.ToString(input)
default:
return oj.JSON(input)
}
return cast.ToString(input)
}