Skip to content

Commit

Permalink
Merge pull request #22 from tenntenn/fix-is-json
Browse files Browse the repository at this point in the history
If a string to be interpreted as JSON begins with a number, it is not considered JSON.
  • Loading branch information
k1LoW authored Mar 4, 2024
2 parents 6ffc072 + 1e17e2c commit 46c2045
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ func (c *Checker) isJSON(s string) bool {
return false
}

if len(s) > 0 {
_, err := strconv.Atoi(s[0:1])
trimed := strings.TrimSpace(s)
if len(trimed) > 0 {
_, err := strconv.Atoi(trimed[0:1])
if err == nil {
return false
}
Expand Down
32 changes: 17 additions & 15 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ func TestDiff(t *testing.T) {
got any
hasDiff bool
}{
"string-nodiff": {"hello", "hello", false},
"bytes-nodiff": {"hello", []byte("hello"), false},
"reader-nodiff": {"hello", strings.NewReader("hello"), false},
"json-nodiff": {"{\"S\":\"hello\"}\n", struct{ S string }{S: "hello"}, false},
"marshaler-nodiff": {"hello", marshaler("hello"), false},
"empty-nodiff": {"", "", false},
"number-nodiff": {"3", "3", false},
"number-start-nodiff": {"3 bytes", "3 bytes", false},
"string-nodiff": {"hello", "hello", false},
"bytes-nodiff": {"hello", []byte("hello"), false},
"reader-nodiff": {"hello", strings.NewReader("hello"), false},
"json-nodiff": {"{\"S\":\"hello\"}\n", struct{ S string }{S: "hello"}, false},
"marshaler-nodiff": {"hello", marshaler("hello"), false},
"empty-nodiff": {"", "", false},
"number-nodiff": {"3", "3", false},
"number-start-nodiff": {"3 bytes", "3 bytes", false},
"number-start-with-space-nodiff": {"\n\n \n\r3 bytes", "\n\n \n\r3 bytes", false},

"string-diff": {"Hello", "hello", true},
"bytes-diff": {"Hello", []byte("hello"), true},
"reader-diff": {"Hello", strings.NewReader("hello"), true},
"json-diff": {"{\"S\":\"Hello\"}\n", struct{ S string }{S: "hello"}, true},
"marshaler-diff": {"Hello", marshaler("hello"), true},
"number-diff": {"3", "4", true},
"number-start-diff": {"3 bytes", "4 bytes", true},
"string-diff": {"Hello", "hello", true},
"bytes-diff": {"Hello", []byte("hello"), true},
"reader-diff": {"Hello", strings.NewReader("hello"), true},
"json-diff": {"{\"S\":\"Hello\"}\n", struct{ S string }{S: "hello"}, true},
"marshaler-diff": {"Hello", marshaler("hello"), true},
"number-diff": {"3", "4", true},
"number-start-diff": {"3 bytes", "4 bytes", true},
"number-start-with-space-diff": {"\n\n \n\r3 bytes", "\n\n \n\r4 bytes", true},
}

for name, tt := range cases {
Expand Down

0 comments on commit 46c2045

Please sign in to comment.