Skip to content

Commit

Permalink
Merge pull request #177 from WillAbides/valid
Browse files Browse the repository at this point in the history
Make Valid more consistent with "encoding/json"
  • Loading branch information
goccy authored Apr 10, 2021
2 parents ca141f3 + 4cae8f7 commit 8bc5727
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
return true
if !decoder.More() {
return true
}
return decoder.InputOffset() >= int64(len(data))
}
1 change: 1 addition & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var validTests = []struct {
{`{}`, true},
{`{"foo":"bar"}`, true},
{`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
{`[""],`, false},
}

func TestValid(t *testing.T) {
Expand Down

0 comments on commit 8bc5727

Please sign in to comment.