added IsObject IsArray helper functions

This commit is contained in:
Josh Baker 2017-08-14 08:23:21 -07:00
parent 6daf3373dc
commit ccc7f39b3a
2 changed files with 27 additions and 0 deletions

View File

@ -187,6 +187,16 @@ func (t Result) Array() []Result {
return r.a
}
// IsObject returns true if the result value is a JSON object.
func (t Result) IsObject() bool {
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{'
}
// IsObject returns true if the result value is a JSON array.
func (t Result) IsArray() bool {
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '['
}
// ForEach iterates through values.
// If the result represents a non-existent value, then no values will be iterated.
// If the result is an Object, the iterator will pass the key and value of each item.

View File

@ -223,6 +223,23 @@ func TestBasic(t *testing.T) {
}
}
func TestIsArrayIsObject(t *testing.T) {
mtok := get(basicJSON, "loggy")
assert(t, mtok.IsObject())
assert(t, !mtok.IsArray())
mtok = get(basicJSON, "loggy.programmers")
assert(t, !mtok.IsObject())
assert(t, mtok.IsArray())
mtok = get(basicJSON, `loggy.programmers.#[tag="good"]#.firstName`)
assert(t, mtok.IsArray())
mtok = get(basicJSON, `loggy.programmers.0.firstName`)
assert(t, !mtok.IsObject())
assert(t, !mtok.IsArray())
}
func TestPlus53BitInts(t *testing.T) {
json := `{"IdentityData":{"GameInstanceId":634866135153775564}}`
value := Get(json, "IdentityData.GameInstanceId")