MySqlConnector
MySqlConnector copied to clipboard
Add `MySqlDataReader.GetUtf8JsonReader` method
From https://github.com/mysql-net/MySqlConnector/issues/1451#issuecomment-1944973581:
Would it make sense to add a similar option to read JSON columns as byte[] (instead of a string)?
On .NET 6.0 and later, MySqlDataReader could expose a GetUtf8JsonReader method that returns a Utf8JsonReader. This would allow users to call JsonNode.Parse or JsonSerializer.Deserialize to read straight from MySqlConnector's buffer.
Reasons not to do this could be:
Utf8JsonReaderis aref structthat can't be used in async methods- Lifetime concerns about reading the JSON before
MySqlDataReader.Read(Async)is called again; see discussion at https://github.com/dotnet/runtime/issues/24899
Exposing ReadOnlyMemory<byte> would allow JsonDocument.Parse to be called, but that could introduce serious lifetime issues because "the ReadOnlyMemory<T> value will be used for the entire lifetime of the JsonDocument object, and the caller must ensure that the data therein does not change during the object lifetime." (ReadOnlyMemory<byte> would also allow Utf8JsonReader to be constructed from its .Span, enabling the scenarios above.)
The existing MySqlDataReader.GetStream() API creates a Stream on the already-in-memory data, and can be used with JsonNode.Parse, JsonDocument.Parse, and JsonSerializer.Deserialize. It's probably the best approach to recommend; maybe some documentation showing samples of how to use it should be added.