import "strconv"
+// Valid reports whether data is a valid JSON encoding.
+func Valid(data []byte) bool {
+ return checkValid(data, &scanner{}) == nil
+}
+
// checkValid verifies that data is valid JSON-encoded data.
// scan is passed in for use by checkValid to avoid an allocation.
func checkValid(data []byte, scan *scanner) error {
"testing"
)
+var validTests = []struct {
+ data string
+ ok bool
+}{
+ {`foo`, false},
+ {`}{`, false},
+ {`{]`, false},
+ {`{}`, true},
+ {`{"foo":"bar"}`, true},
+ {`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
+}
+
+func TestValid(t *testing.T) {
+ for _, tt := range validTests {
+ if ok := Valid([]byte(tt.data)); ok != tt.ok {
+ t.Errorf("Valid(%#q) = %v, want %v", tt.data, ok, tt.ok)
+ }
+ }
+}
+
// Tests of simple examples.
type example struct {