Merge 0a2d1d5cf5760cc62a99b8a5f452854cbd234380 into 24f6ab0908418f0a4bd93e9954dfa4c1963592ec

This commit is contained in:
Y.Horie 2018-08-02 15:33:19 +00:00 committed by GitHub
commit 47d6cc005d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -2,9 +2,11 @@
package gjson package gjson
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -1411,6 +1413,15 @@ func Get(json, path string) Result {
return c.value return c.value
} }
// GetReader searches json for the specified path.
// If working with io.Reader, this method preferred over Get(string(data), path)
func GetReader(json io.Reader, path string) Result {
buf := new(bytes.Buffer)
buf.ReadFrom(json)
return Get(buf.String(), path)
}
// GetBytes searches json for the specified path. // GetBytes searches json for the specified path.
// If working with bytes, this method preferred over Get(string(data), path) // If working with bytes, this method preferred over Get(string(data), path)
func GetBytes(json []byte, path string) Result { func GetBytes(json []byte, path string) Result {

View File

@ -37,6 +37,28 @@ func TestRandomData(t *testing.T) {
} }
} }
func TestRandomValidReader(t *testing.T) {
var lstr string
defer func() {
if v := recover(); v != nil {
println("'" + hex.EncodeToString([]byte(lstr)) + "'")
println("'" + lstr + "'")
panic(v)
}
}()
rand.Seed(time.Now().UnixNano())
b := make([]byte, 200)
for i := 0; i < 2000000; i++ {
n, err := rand.Read(b[:rand.Int()%len(b)])
if err != nil {
t.Fatal(err)
}
lstr = string(b[:n])
GetReader(bytes.NewReader([]byte(lstr)), "reader")
Parse(lstr)
}
}
func TestRandomValidStrings(t *testing.T) { func TestRandomValidStrings(t *testing.T) {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
b := make([]byte, 200) b := make([]byte, 200)