From 7db3b02e3f802ec019d7c85ed0b09f016e18a3e7 Mon Sep 17 00:00:00 2001 From: deef Date: Sat, 30 May 2020 06:16:06 -0400 Subject: [PATCH] update Result.Bool to use golang bool parsing golang has several tools that already parse boolean values in the standard library. Specifically strconv.ParseBool(string) https://golang.org/pkg/strconv/#ParseBool "ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error." This change returns any matching ParseBool string to that boolean value, or if the string is not one of those values, the error being thrown, returns a false. --- gjson.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gjson.go b/gjson.go index 0b6dcb0..6c80729 100644 --- a/gjson.go +++ b/gjson.go @@ -106,7 +106,8 @@ func (t Result) Bool() bool { case True: return true case String: - return t.Str != "" && t.Str != "0" && t.Str != "false" + b, err := strconv.ParseBool(t.Str) + return !(!b || err != nil) case Number: return t.Num != 0 }