Skip to content

Commit

Permalink
Use regexp to determine if json bytes is array
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jul 3, 2024
1 parent 87574dc commit 9a99d3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package client
import (
"encoding/json"
"errors"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -117,11 +118,11 @@ func NewDocFromMap(data map[string]any, collectionDefinition CollectionDefinitio
return doc, nil
}

var jsonArrayPattern = regexp.MustCompile(`(?s)^\s*\[.*\]\s*$`)

// IsJSONArray returns true if the given byte array is a JSON Array.
func IsJSONArray(obj []byte) bool {
var js []any
err := json.Unmarshal(obj, &js)
return err == nil
return jsonArrayPattern.Match(obj)
}

// NewFromJSON creates a new instance of a Document from a raw JSON object byte array.
Expand Down
7 changes: 4 additions & 3 deletions client/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ func TestIsJSONArray(t *testing.T) {
name: "Valid JSON Array with Whitespace",
input: []byte(`
[
{"name": "John", "age": 21},
{"name": "Islam", "age": 33}
]`),
{ "name": "John", "age": 21 },
{ "name": "Islam", "age": 33 }
]
`),
expected: true,
},
}
Expand Down

0 comments on commit 9a99d3f

Please sign in to comment.