From 725f3caa55d5c0cf1ed244101b02732f85bd7fc2 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Fri, 12 Aug 2016 08:39:08 -0700 Subject: [PATCH] exists function --- README.md | 15 ++++++++++++--- gjson.go | 11 +++++++++++ gjson_test.go | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 527eddc..f9a3397 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,10 @@ This will print: Prichard ``` -A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array use the '#' character. +A path is a series of keys separated by a dot. +A key may contain special wildcard characters '\*' and '?'. +To access an array value use the index as the key. +To get the number of elements in an array use the '#' character. The dot and wildcard character can be escaped with '\'. ``` { @@ -69,14 +72,14 @@ The dot and wildcard character can be escaped with '\'. ## Result Type -GJSON supports the json types `string`, `number`, `bool`, and `null`. Arrays and Objects are returned as their raw json types. +GJSON supports the json types `string`, `number`, `bool`, and `null`. +Arrays and Objects are returned as their raw json types. The `Result` type holds one of these types: ``` bool, for JSON booleans float64, for JSON numbers -Number, for JSON numbers string, for JSON string literals nil, for JSON null ``` @@ -89,6 +92,12 @@ result.Value() // interface{} which may be nil, string, float64, or bool // Or just get the value in one step. gjson.Get(json, "name.last").Value() + +// Check for the existence of a value. +if gjson.Get(json, "name.last").Exists(){ + println("value exists") +} + ``` To directly access the value from its original type: diff --git a/gjson.go b/gjson.go index e6dad09..abc693d 100644 --- a/gjson.go +++ b/gjson.go @@ -51,6 +51,15 @@ func (t Result) String() string { } } +// Exists returns true if value exists. +// +// if gjson.Get(json, "name.last").Exists(){ +// println("value exists") +// } +func (t Result) Exists() bool { + return t.Type != Null && len(t.Raw) != 0 +} + // Value returns one of these types: // // bool, for JSON booleans @@ -97,6 +106,8 @@ type frame struct { // A key may contain special wildcard characters '*' and '?'. // To access an array value use the index as the key. // To get the number of elements in an array use the '#' character. +// The dot and wildcard character can be escaped with '\'. +// // { // "name": {"first": "Tom", "last": "Anderson"}, // "age":37, diff --git a/gjson_test.go b/gjson_test.go index a44cb5e..8b058f7 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -103,6 +103,9 @@ var basicJSON = `{"age":100, "name":{"here":"B\\\"R"}, func TestBasic(t *testing.T) { var token Result + if !Get(basicJSON, "name.last").Exists() { + t.Fatal("expected true, got false") + } token = Get(basicJSON, "name.here") if token.String() != "B\\\"R" { t.Fatal("expecting 'B\\\"R'", "got", token.String())