-
Notifications
You must be signed in to change notification settings - Fork 4
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 support for using TypeId as dictionary keys with system.text.json #17
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting the PR and covering it with tests 👍
The change looks good overall. I only suggested some minor improvements, let me know what you think about those.
@@ -15,4 +16,15 @@ public override void Write(Utf8JsonWriter writer, TypeId value, JsonSerializerOp | |||
{ | |||
writer.WriteStringValue(value.ToString()); | |||
} | |||
|
|||
public override void WriteAsPropertyName(Utf8JsonWriter writer, [DisallowNull] TypeId value, JsonSerializerOptions options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[DisallowNull]
attribute is redundant here because TypeId
is a struct and can't be null. I'd suggest removing it with using System.Diagnostics.CodeAnalysis
.
return ReadTypeId(ref reader); | ||
} | ||
|
||
public override void WriteAsPropertyName(Utf8JsonWriter writer, [DisallowNull] TypeIdDecoded value, JsonSerializerOptions options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly to TypeId
, [DisallowNull]
attribute is redundant here because TypeIdDecoded
is a struct and can't be null. I'd suggest removing it with using System.Diagnostics.CodeAnalysis
.
return val is not null ? TypeId.Parse(val).Decode() : default; | ||
} | ||
|
||
private static void CopyValueToBuffer(TypeIdDecoded value, Span<char> buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid producing copies of parameters let's consider using in
modifier. Since both are greater or equal to pointer size it should be more performant than passing a copy.
private static void CopyValueToBuffer(TypeIdDecoded value, Span<char> buffer) | |
private static void CopyValueToBuffer(in TypeIdDecoded value, in Span<char> buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmarked all options here and it turns out that it only makes sense to use in
for TypeIdDecoded value
parameter.
@danspam I've decided to remove redundant attributes myself to get this PR merged. Thanks again for your great contribution! |
See #16