Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MySqlDataReader.GetUtf8JsonReader method #1454

Open
bgrainger opened this issue Feb 20, 2024 · 1 comment
Open

Add MySqlDataReader.GetUtf8JsonReader method #1454

bgrainger opened this issue Feb 20, 2024 · 1 comment

Comments

@bgrainger
Copy link
Member

From #1451 (comment):

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:

Exposing ReadOnlyMemory<byte> would allow JsonDocument.Parse to be called, but that could introduce serious lifetime issues because "the ReadOnlyMemory 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.)

@bgrainger
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant