diff --git a/json.go b/json.go index 0e90caf..725a26c 100644 --- a/json.go +++ b/json.go @@ -40,6 +40,13 @@ func (r Raw) AsNumber() float64 { return v } +// AsBool returns a boolean contained in the Raw or false in case there is not a boolean. +func (r Raw) AsBool() bool { + v, _ := r.payload.(bool) + + return v +} + // AsList returns an array (actually slice) contained in the Raw or nil in case there is not an array. func (r Raw) AsList() List { l, ok := r.payload.([]interface{}) diff --git a/json_test.go b/json_test.go index 94a0096..8954b27 100644 --- a/json_test.go +++ b/json_test.go @@ -38,4 +38,7 @@ func TestRaw(t *testing.T) { require.Nil(t, raw.AsMap().Get("mess").AsMap().Get("nonexistent").AsMap()) require.Equal(t, freeform.Raw{}, raw.AsMap().Get("mess").AsMap().Get("nonexistent").AsMap().Get("")) require.Equal(t, "", raw.AsString()) + require.False(t, raw.AsBool()) + require.True(t, raw.AsMap().Get("bool true").AsBool()) + require.False(t, raw.AsMap().Get("bool false").AsBool()) } diff --git a/testdata/good.json b/testdata/good.json index 9bd0dc6..a312be2 100644 --- a/testdata/good.json +++ b/testdata/good.json @@ -6,6 +6,8 @@ 200, 400 ], + "bool false": false, + "bool true": true, "map": { "0": 100, "1": 500,