-
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 - Add overloads for reading a Guid using a custom "standard format" #30692
Comments
|
|
A workaround here is to use a custom converter to read/write with |
But then we have to do this for every parse, whereas there is an existing Guid parsing method in Utf8JsonReader, which can be made extended but backwards compatible due to its default 'D' format value, and which then makes life easier for a lot of other libraries. I don't think we should expect devs to write custom parsers to read Guids. |
It's only if you're using a non-standard Guid format 😄
This issue is about adding these APIs to make it easier to parse non-standard Guid formats, using |
No, I'm suggesting we could add a format parameter with a default value. That would keep it backwards compatible and then default behaviour would be identical to what it is now. The performance hit would be negligible because further downstream we are already using a format parameter with a default value. The standard Guid format is only standard in the .NET scope - plenty of other frameworks use different formats, and combining stacks is rather common. It is so common that the Guid parser in .NET supports these other formats. If we were super concerned about performance then we could overload the method, meaning the original is unaffected. |
That's exactly what this issue is proposing though? 🤔
No, it's absolutely not. The canonical 8-4-4-4-12 format string is based on the record layout for the 16 bytes of the UUID, as specified by RFC4122. The RFC even contains an example implementation for this format. |
Are you arguing against your own proposal? Im confused by what you're suggesting (not trying to be awkward!) Adding an optional parameter with a default value won't cause a big performance hit.
You're right, that by standards, it had a default format. The existence of different format options in .net is evidence enough that others use different formats, and that other formats are de facto standards for their own frameworks. |
I'm asking what your comment was about, since I don't see what value it added if it was basically saying exactly what the proposal says 😅
Sure, and if you're using something non-standard, I think it's only reasonable that you have to tweak the (de-)serializer to fit your needs, i.e. write a custom converter 😄 Anyway, @layomia's comment was a workaround, not a solution. This issue tracks the API additions needed to tweak the behavior. |
Can someone add a CustomConverter that works?
|
From @AntonIOIOIO in #59474:
|
We should consider this issue a general one for extended |
A concern I have about the proposal is that the new Guid overloads in Utf8JsonWriter/Utf8JsonReader:
I'm wondering if instead it might make more sense to:
|
As I read in #77020, this issue seems won't be solved in .NET 8? |
It is quite silly considering that most other places, N variants of GUIDs work just fine. |
Still an issue as of .NET 8, I'd love to see this getting more attention, as I don't see any real reason why only standard format of Guid is supported. internal sealed class GuidJsonConverter : JsonConverter<Guid> {
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
try {
return Guid.Parse(reader.GetString()!);
} catch {
// Throw JsonException instead, which will be converted into standard message by STJ
throw new JsonException();
}
}
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options) {
writer.WriteStringValue(value.ToString());
}
} This custom converter does the trick, even if naturally it could be done better with underlying implementation. |
Hello, I've the same issue to use FromBodyAttributes on AzureFunction. For now i'm using @JustArchi solution (thanks). |
Rationale
Currently, it's a bit of a hassle to properly read a
Guid
using a custom format. You basically have to copy the implementation ofTryGetGuid
just to (more or less) change a single character (the format):https://github.com/dotnet/corefx/blob/7055b496a30dfe0f66a2f555cad31502473d144b/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs#L942-L993
And because
JsonReaderHelper
is internal, you also have to copy the implementation ofTryGetEscapedGuid
:https://github.com/dotnet/corefx/blob/7055b496a30dfe0f66a2f555cad31502473d144b/src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs#L315-L339
Proposal
I'd ❤️ to see the following overloads added to System.Text.Json:
This would make it really easy to write a custom converter to work around #1567:
Notes
standardFormat
to mirror theUtf8Parser
parameter names. Dunno whether that's a good name or not.// @ahsonkhan @layomia @steveharter
The text was updated successfully, but these errors were encountered: