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

[System.Text.Json] Utf8JsonReader/Writer should support Char #32536

Closed
blankensteiner opened this issue Feb 19, 2020 · 4 comments
Closed

[System.Text.Json] Utf8JsonReader/Writer should support Char #32536

blankensteiner opened this issue Feb 19, 2020 · 4 comments
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

Comments

@blankensteiner
Copy link

blankensteiner commented Feb 19, 2020

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:

The Utf8JsonReader is a low-level type that can be used to build custom parsers and deserializers

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:

public ref partial struct Utf8JsonReader
{
    public char GetChar();
    public bool TryGetChar(out char value);
}
public sealed partial class Utf8JsonWriter : System.IAsyncDisposable, System.IDisposable
{
    public void WriteString(string propertyName, char value);
}
public readonly partial struct JsonElement
{
    public char GetChar();
    public bool TryGetChar(out char value);
}

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.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Feb 19, 2020
@ahsonkhan ahsonkhan added this to the 5.0 milestone Feb 20, 2020
@ahsonkhan ahsonkhan added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner labels Feb 20, 2020
@layomia layomia changed the title [System.Text.Json] Utf8JsonReader/Writer should support Char and TimeSpan [System.Text.Json] Utf8JsonReader/Writer should support Char Feb 28, 2020
@layomia
Copy link
Contributor

layomia commented Feb 28, 2020

I've split the request for TimeSpan into a new issue (#32965), as Char and TimeSpan are separate work items for support.

@blankensteiner can you please update the description to reflect this and include the new API proposals in a format ready for review? See #29936 for an example specific to Utf8JsonReader/Writer.

@blankensteiner
Copy link
Author

Hi @layomia
Sure, will the above suffice?

@ghost
Copy link

ghost commented Oct 17, 2021

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of the experimental issue cleanup initiative we are currently trialing in a limited number of areas. Please share any feedback you might have in the linked issue.

@ghost
Copy link

ghost commented Nov 5, 2021

This issue will now be closed since it had been marked no recent activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@ghost ghost closed this as completed Nov 5, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 5, 2021
@eiriktsarpalis eiriktsarpalis added the backlog-cleanup-candidate An inactive issue that has been marked for automated closure. label Feb 18, 2022
@ghost ghost removed the no-recent-activity label Feb 18, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.
Projects
None yet
Development

No branches or pull requests

5 participants