diff --git a/client/document.go b/client/document.go index c5db61855a..ad928452c1 100644 --- a/client/document.go +++ b/client/document.go @@ -12,6 +12,7 @@ package client import ( "encoding/json" + "regexp" "strings" "sync" "time" @@ -116,14 +117,11 @@ func NewDocFromMap(data map[string]any, sd SchemaDescription) (*Document, error) return doc, nil } +var jsonArrayPattern = regexp.MustCompile(`^\s*\[.*\]\s*$`) + // IsJSONArray returns true if the given byte array is a JSON Array. func IsJSONArray(obj []byte) bool { - v, err := fastjson.ParseBytes(obj) - if err != nil { - return false - } - _, err = v.Array() - return err == nil + return jsonArrayPattern.Match(obj) } // NewFromJSON creates a new instance of a Document from a raw JSON object byte array. @@ -320,8 +318,10 @@ func getArray[T any]( } return arr, nil + case []T: + return val, nil default: - return val.([]T), nil + return []T{}, nil } } @@ -354,6 +354,8 @@ func getNillableArray[T any]( } return arr, nil + case []immutable.Option[T]: + return val, nil default: return []immutable.Option[T]{}, nil } diff --git a/http/client_collection.go b/http/client_collection.go index 06fcd16877..651cafe01e 100644 --- a/http/client_collection.go +++ b/http/client_collection.go @@ -317,7 +317,8 @@ func (c *Collection) Get(ctx context.Context, key client.DocKey, showDeleted boo if err != nil { return nil, err } - doc, err := client.NewDocFromJSON(data, c.def.Schema) + doc := client.NewDocWithKey(key, c.def.Schema) + err = doc.SetWithJSON(data) if err != nil { return nil, err }