-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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.JsonSerializer should't deserialize string to char #32429
Comments
Agreed. This has been fixed in 5.0 already in #114. See #114 (comment). Feel free to try it by adding a package reference to the latest S.T.Json NuGet package from our nightly feed: https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json Since it would be a behavioral breaking change for some, we decided against porting the fix in servicing for 3.1. If waiting for 5.0 isn't feasible, the workaround for you would be to copy the current char converter implementation from master and register that in the Lines 9 to 31 in 7c7e8ed
Is this a blocking issue for you? Can you please explain your scenario in which you happen to use char properties? I am curious to know.
This was discussed here and we ultimately decided against adding these to the If there is significant need for the |
Hi @ahsonkhan Great to hear that it has already been fixed! It's not a blocking issue for me and I can wait for .NET 5.
My scenario is that I am writing a serializer using Utf8JsonReader and Utf8JsonWriter. I need to support creating the object without calling a constructor (using System.Runtime.Serialization.FormatterServices.GetUninitializedObject) and getting/setting even private and read-only fields (using System.Reflection.FieldInfo.SetValue).
I would love not to duplicate the behavior of JsonSerializer and/or JsonDocument. The same goes for the missing WriteString(string propertyName, TimeSpan value) and GetTimeSpan. I could just implement it by parsing a string, but that is not what I want. I just want to create a new serializer, building on the "lower-level" rules of dealing with TimeSpan and Char. Does that make sense? |
Sure. But do you actually need support for
What do you expect the read/write behavior to be for chars?
string json = "\"a\"";
// excluding steps to transcode to UTF-8 and create reader
char c = reader.GetChar(); // Returning c == 'a' makes sense here
json = "\"ab\"";
c = reader.GetChar(); // throws?
json = "\"\u0064\"";
c = reader.GetChar(); // Returning c == 'd' makes sense here (unescape the JSON string)
json = "100"; // Notes: this is a single, unquoted, JSON number.
c = reader.GetChar(); // should this throw? should this be 'd'?
json = "\"\uD801\""; // high-surrogate, no low-surrogate
c = reader.GetChar(); // should this throw? should this be ushort equivalent of 0xD801?
json = "\"\uD8010\uDC37\""; // valid surrogate pair string
c = reader.GetChar(); // should this throw?
Here's the previous API review link from #29000 (comment) (for context): Video Regarding TimeSpan, that's covered by #29932 |
Yes, we have applications using char fields and properties. It's not common and I can't remember if I have ever created one myself, but we have them in our codebase.
Well, my point is actually what I don't really care, but with the current implementation I have to care. |
Gotcha. Is the code open source/available on GitHub (and if so, can you share the link to the model that contains char)? What is the semantic meaning of those char properties (as an example)? For example, I can see one use case for
Can you please file a new issue with the proposal to add the char-based API overloads to Btw, I was asking for details on the expected behavior because we previously deferred adding such APIs due to lack of clarity on the behavior. The objective was to use the motivating scenario to provide that clarity. Given the primary concern of this issue has been resolved (deserializing string to char will throw in 5.0), closing this one. |
Unlike Newtonsoft.Json, when deserializing a string to a char, it will not throw an exception.
Let me give you an example.
This is our json:
This is our class:
Using Newtonsoft.Json (version 12.0.3) I get an exception when trying to deserialize. The code below will throw this exception:
But when using System.Text.Json.JsonSerializer (.NET Core 3.1), I get an instance of MyClass where "Char" is 'T'.
I think the default behavior should match that of Newtonsoft.Json. If you feel otherwise, then please at least provide a way to overwrite it via JsonSerializerOptions and JsonReaderOptions.
I am writing my own (de)serializer (because I need support for serializing/deserializing private and readonly fields) and therefore I would really prefer that System.Text.Json.Utf8JsonReader is extended with these two new methods:
public char GetChar();
public bool TryGetChar(out char value);
The text was updated successfully, but these errors were encountered: