Skip to content

Commit

Permalink
Merge 9fdffeb into 7cdf684
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Nov 11, 2021
2 parents 7cdf684 + 9fdffeb commit d9fb79d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 20 additions & 0 deletions internal/magic/magic_test.go
@@ -0,0 +1,20 @@
package magic

import "testing"

var magicTests = []struct {
raw string
limit uint32
res bool
detector Detector
}{
{`["an incomplete JSON array`, 0, false, JSON},
}

func TestMagic(t *testing.T) {
for i, tt := range magicTests {
if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res {
t.Errorf("Detector %d error: expected: %t; got: %t", i, tt.res, got)
}
}
}
6 changes: 4 additions & 2 deletions internal/magic/text.go
Expand Up @@ -155,12 +155,14 @@ func Php(raw []byte, limit uint32) bool {
// JSON matches a JavaScript Object Notation file.
func JSON(raw []byte, limit uint32) bool {
raw = trimLWS(raw)
if len(raw) == 0 || (raw[0] != '[' && raw[0] != '{') {
// #175 A single JSON string, number or bool is not considered JSON.
// JSON objects and arrays are reported as JSON.
if len(raw) < 2 || (raw[0] != '[' && raw[0] != '{') {
return false
}
parsed, err := json.Scan(raw)
// If the full file content was provided, check there is no error.
if len(raw) < int(limit) {
if limit == 0 || len(raw) < int(limit) {
return err == nil
}

Expand Down

0 comments on commit d9fb79d

Please sign in to comment.