added result.Time() function

This commit is contained in:
Josh Baker 2017-04-14 17:58:25 -07:00
parent 6e0babc7e8
commit 039b641eab
3 changed files with 13 additions and 0 deletions

View File

@ -126,6 +126,7 @@ result.Uint() uint64
result.Float() float64
result.String() string
result.Bool() bool
result.Time() time.Time
result.Array() []gjson.Result
result.Map() map[string]gjson.Result
result.Get(path string) Result

View File

@ -4,6 +4,7 @@ package gjson
import (
"reflect"
"strconv"
"time"
"unicode/utf16"
"unicode/utf8"
"unsafe"
@ -140,6 +141,12 @@ func (t Result) Float() float64 {
}
}
// Time returns a time.Time representation.
func (t Result) Time() time.Time {
res, _ := time.Parse(time.RFC3339, t.String())
return res
}
// Array returns back an array of values.
// If the result represents a non-existent value, then an empty array will be returned.
// If the result is not a JSON array, the return value will be an array containing one result.

View File

@ -114,6 +114,7 @@ var basicJSON = `{"age":100, "name":{"here":"B\\\"R"},
"items":[1,2,3,{"tags":[1,2,3],"points":[[1,2],[3,4]]},4,5,6,7],
"arr":["1",2,"3",{"hello":"world"},"4",5],
"vals":[1,2,3,{"sadf":sdf"asdf"}],"name":{"first":"tom","last":null},
"created":"2014-05-16T08:28:06.989Z",
"loggy":{
"programmers": [
{
@ -144,6 +145,10 @@ var basicJSON = `{"age":100, "name":{"here":"B\\\"R"},
}`
var basicJSONB = []byte(basicJSON)
func TestTimeResult(t *testing.T) {
assert(t, Get(basicJSON, "created").String() == Get(basicJSON, "created").Time().Format(time.RFC3339Nano))
}
func TestParseAny(t *testing.T) {
assert(t, Parse("100").Float() == 100)
assert(t, Parse("true").Bool())