Storage: why are JSON arrays not allowed?

When saving data using the storage engine in a Go module, why must the data be a JSON object? JSON arrays like [1, 2, 3] are perfectly valid JSON too. Looking at the source at nakama/api_storage.go at master · heroiclabs/nakama · GitHub the writing function explicitly checks for a starting {. This had me confused for a while, especially as the docs are somewhat severely lacking in this regard. As the result of json.Valid() is not enough there must be some other reason here?

if maybeJSON := []byte(object.GetValue()); !json.Valid(maybeJSON) || bytes.TrimSpace(maybeJSON)[0] != byteBracket {
	return nil, status.Error(codes.InvalidArgument, "Value must be a JSON object.")
}

Having to add an extra top level JSON object around data introduces unnecessary code.

Nakama’s storage engine currently requires top-level JSON objects for data storage, querying, and API contract reasons. This requirement may be dropped in the future to allow top-level arrays or primitives, but it’s not on the immediate roadmap.

This had me confused for a while, especially as the docs are somewhat severely lacking in this regard.

We’re happy to receive pull requests or at least issue reports on the documentation repository, and we’re always working on improvements to the docs.

1 Like

Does this mean that Nakama parses and does something with the data added with StorageWrite()? Why would it do that as it can’t know what has been stored?

@chakie the server uses the JSONB column type of the database to store the data, and it will validate if it is valid JSON before attempting to store it.