-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updates to System.Text.Json samples #1871
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
Conversation
snippets/core/system-text-json/csharp/ImmutablePointConverter.cs
Outdated
Show resolved
Hide resolved
snippets/core/system-text-json/csharp/ImmutablePointConverter.cs
Outdated
Show resolved
Hide resolved
snippets/core/system-text-json/csharp/WeatherForecastCallbacksConverter.cs
Show resolved
Hide resolved
ahsonkhan
left a comment
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.
Otherwise, looks good.
BillWagner
left a comment
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.
These look great @tdykstra
I had a couple suggestions that you can choose to add, if you'd like. Then, you can
when ready.
| namespace SystemTextJsonSamples | ||
| { | ||
| // <SnippetImmutablePoint> | ||
| public struct ImmutablePoint |
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.
I'd consider making this a readonly struct:
| public struct ImmutablePoint | |
| public readonly struct ImmutablePoint |
| Person value; | ||
| Person person; | ||
| TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); | ||
| switch (typeDiscriminator) |
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.
This would be a great use for the pattern matching switch expression:
Person person = typeDiscriminator switch
{
TypeDiscriminator.Customer => new Customer();
TypeDiscriminator.Employee => new Employee();
_ => throw new JsonException();
};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.
Done, except I had to comma-separate the lines instead of terminating them with semicolon. 😄
BTW, this pattern doesn't seem to be in the C# guide switch article , but it is in the C# 8 what's new.
|
I didn't realize there was a json file checked-in here. Why did it show up as binary? In any case, should we point to the reference of where we got this snippet from (either in the comment within the JSON or as a note in the text before/after it)? Lines 18 to 19 in 9ce7c1b
|
|
It's UTF-16 LE, and it only has fictional data. |
Ah gotcha. Didn't realize that. |

Related to dotnet/docs#16225