[System.Text.Json] Utf8JsonReader/Writer should support Char #32536
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Text.Json
backlog-cleanup-candidate
An inactive issue that has been marked for automated closure.
Milestone
As explained in the related issue #32429, I'm currently writing my own (de)serializer because I need to support creating the object without calling a constructor (using System.Runtime.Serialization.FormatterServices.GetUninitializedObject) and getting/setting private and read-only fields (using System.Reflection.FieldInfo.SetValue).
Just like JsonSerializer and JsonDocument, I am building on top of Utf8JsonReader and Utf8JsonWriter.
That decision was made because it's encouraged:
and because I want to generate the exact same JSON and if possible also respect all options (JsonWriterOptions, JsonReaderOptions, JsonSerializerOptions).
However, I find that I'm not able to just focus on mapping from C# fields to JSON properties (and vise versa), because I have to duplicate functionality/behavior. I think these methods are missing from the current API:
In my opinion, it's the domain of Utf8JsonReader and Utf8JsonWriter to know how to (de)serialize the primitive types to whatever data types JSON happens to support. They already know how to do this for most types, but inconsistently Char is left out. By that logic (the arguments for not adding them), I don't see why DateTime and DateTimeOffset are handled in the reader and writer, but not Char? The challenge is the same: We have a C#-type and a JSON string, do some magic.
In .NET Core 3.1 you can deserialize a string to a char. The char will be the first character of the string. That is an implementation I have to duplicate in my serializer. When .NET 5 comes, this logic changes to throwing an exception and then I have to duplicate that behavior.
If that logic was instead pushed down into Utf8JsonReader, Utf8JsonWriter and JsonElement, all users (and people like me creating serializers) could be free'ed from dealing with that lower-level logic.
We should just pass through the wish to get a Char and not care what JSON data types the authors of Utf8JsonReader/Writer chose for it and what mapping logic there currently is.
The text was updated successfully, but these errors were encountered: