Skip to content

Commit

Permalink
Merge branch 'deef0000dragon1-deef0000dragon1-ParseBool'
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Nov 4, 2020
2 parents 20b6add + ed7c6c1 commit eeddc54
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion gjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion gjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,25 @@ func TestTypes(t *testing.T) {
assert(t, (Result{Type: JSON}).Type.String() == "JSON")
assert(t, (Result{Type: 100}).Type.String() == "")
// bool
assert(t, (Result{Type: String, Str: "true"}).Bool())
assert(t, (Result{Type: True}).Bool())
assert(t, (Result{Type: False}).Bool() == false)
assert(t, (Result{Type: Number, Num: 1}).Bool())
assert(t, (Result{Type: String, Str: "1"}).Bool())
assert(t, (Result{Type: String, Str: "T"}).Bool())
assert(t, (Result{Type: String, Str: "t"}).Bool())
assert(t, (Result{Type: String, Str: "true"}).Bool())
assert(t, (Result{Type: String, Str: "True"}).Bool())
assert(t, (Result{Type: String, Str: "TRUE"}).Bool())
assert(t, (Result{Type: String, Str: "tRuE"}).Bool() == false)
assert(t, (Result{Type: String, Str: "0"}).Bool() == false)
assert(t, (Result{Type: String, Str: "f"}).Bool() == false)
assert(t, (Result{Type: String, Str: "F"}).Bool() == false)
assert(t, (Result{Type: String, Str: "false"}).Bool() == false)
assert(t, (Result{Type: String, Str: "False"}).Bool() == false)
assert(t, (Result{Type: String, Str: "FALSE"}).Bool() == false)
assert(t, (Result{Type: String, Str: "fAlSe"}).Bool() == false)
assert(t, (Result{Type: String, Str: "random"}).Bool() == false)

// int
assert(t, (Result{Type: String, Str: "1"}).Int() == 1)
assert(t, (Result{Type: True}).Int() == 1)
Expand Down

0 comments on commit eeddc54

Please sign in to comment.