add bool support

This commit is contained in:
白茶清欢 2025-05-06 14:43:22 +08:00
parent 6c2c68c53e
commit c7ce590e3b
2 changed files with 10 additions and 0 deletions

View File

@ -23,6 +23,8 @@ type IJsonRead interface {
Float(dataPath string) (float64, error)
// String 转换为string类型
String(dataPath string) (string, error)
// Bool 转换为bool类型
Bool(dataPath string) (bool, error)
// Map 转换为map
Map(dataPath string) (map[string]any, error)
// MapWithReceiver 通过指针接收

View File

@ -74,6 +74,14 @@ func (g *gjsonRead) Float(dataPath string) (float64, error) {
return gjson_hack.Float64(g.gjsonResult.Get(dataPath))
}
func (g *gjsonRead) Bool(dataPath string) (bool, error) {
res := g.gjsonResult.Get(dataPath)
if !res.IsBool() {
return false, errors.New(dataPath + ": is not a bool")
}
return res.Bool(), nil
}
func (g *gjsonRead) String(dataPath string) (string, error) {
if !g.Exist(dataPath) {
return "", errors.New(dataPath + ": not found")