mirror of https://github.com/goccy/go-json.git
Merge pull request #177 from WillAbides/valid
Make Valid more consistent with "encoding/json"
This commit is contained in:
commit
8bc5727ba4
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.
|
// Valid reports whether data is a valid JSON encoding.
|
||||||
func Valid(data []byte) bool {
|
func Valid(data []byte) bool {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
if err := Unmarshal(data, &v); err != nil {
|
decoder := NewDecoder(bytes.NewReader(data))
|
||||||
|
err := decoder.Decode(&v)
|
||||||
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if !decoder.More() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
return decoder.InputOffset() >= int64(len(data))
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ var validTests = []struct {
|
||||||
{`{}`, true},
|
{`{}`, true},
|
||||||
{`{"foo":"bar"}`, true},
|
{`{"foo":"bar"}`, true},
|
||||||
{`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
|
{`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
|
||||||
|
{`[""],`, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValid(t *testing.T) {
|
func TestValid(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue