forked from mirror/go-json
update Valid to return false when there is additional data after the validated value
This commit is contained in:
parent
a39bc30ed9
commit
4cae8f7bc7
7
json.go
7
json.go
|
@ -333,8 +333,13 @@ func HTMLEscape(dst *bytes.Buffer, src []byte) {
|
|||
// Valid reports whether data is a valid JSON encoding.
|
||||
func Valid(data []byte) bool {
|
||||
var v interface{}
|
||||
if err := Unmarshal(data, &v); err != nil {
|
||||
decoder := NewDecoder(bytes.NewReader(data))
|
||||
err := decoder.Decode(&v)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if !decoder.More() {
|
||||
return true
|
||||
}
|
||||
return decoder.InputOffset() >= int64(len(data))
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ var validTests = []struct {
|
|||
{`{}`, true},
|
||||
{`{"foo":"bar"}`, true},
|
||||
{`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
|
||||
{`[""],`, false},
|
||||
}
|
||||
|
||||
func TestValid(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue