-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add WritePropertyName standlone API on the Utf8JsonWriter. #38864
Conversation
/// The property name should already be escaped when the instance of <see cref="JsonEncodedText"/> was created. | ||
/// </remarks> | ||
/// <exception cref="InvalidOperationException"> | ||
/// Thrown if this would result in an invalid JSON to be written (while validation is enabled). |
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.
"would result in an invalid JSON to be written" is strange grammatically. How about "would result in invalid JSON being written"?
/// <param name="propertyName">The JSON encoded property name of the JSON object to be transcoded and written as UTF-8.</param> | ||
/// </summary> | ||
/// <remarks> | ||
/// The property name should already be escaped when the instance of <see cref="JsonEncodedText"/> was created. |
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.
"The property name should already be escaped when the instance of JsonEncodedText was created." is strange . What is it trying to convey? Is there something the user needed to do but didn't? Is this stating a fact about something that must be the case?
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 agree this remark is weird. It's like taking an int
and remarking the value has to be between int.MinValue
and int.MaxValue
inclusive... it's a truth provided by the type system.
} | ||
else | ||
{ | ||
// Cannot create a span directly since it gets assigned to parameter and passed down. |
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.
Are we not able to work around this now with readonly members?
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.
Copy-pasted from existing code, so will have to fix this across the board. Out-of-scope for this PR.
|
||
Span<byte> output = _memory.Span; | ||
|
||
if (_currentDepth < 0) |
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 had to go find the comment on _currentDepth to understand how a depth could be negative. Consider instead creating a private property that does this check and is appropriately named to describe what it's actually checking.
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.
Copy-pasted from existing code, so will have to fix this across the board. Out-of-scope for this PR.
src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.SignedNumber.cs
Show resolved
Hide resolved
/// <summary> | ||
/// Writes the property name (as a JSON string) as the first part of a name/value pair of a JSON object. | ||
/// </summary> | ||
/// <param name="propertyName">The property name of the JSON object to be transcoded and written as UTF-8.</param> |
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.
transcoded and written as UTF-8
Does it really make sense to mention this detail everywhere?
src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs
Show resolved
Hide resolved
@@ -9,6 +9,353 @@ namespace System.Text.Json | |||
{ | |||
public sealed partial class Utf8JsonWriter | |||
{ | |||
/// <summary> | |||
/// Writes the pre-encoded property name (as a JSON string) as the first part of a name/value pair of a JSON object. | |||
/// <param name="propertyName">The JSON encoded property name of the JSON object to be transcoded and written as UTF-8.</param> |
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.
Who said anything about transcoding? That's an irrelevant implementation detail (if the JsonEncodedText doesn't pre-transcode, then the only way to avoid transcoding is to use the ROS-byte overload... which runs the escaper. So it's not worth mentioning, especially in the param tag... remarks if you must.)
/// <param name="propertyName">The JSON encoded property name of the JSON object to be transcoded and written as UTF-8.</param> | |
/// <param name="propertyName">The name of the property to write.</param> |
A lot of the feedback here was around comments/documentation re-wording which was primarily copy-pasted from existing APIs. I am going to defer addressing that feedback to scope this PR down to what is relevant just to this change and do a larger comments re-wording in the future, especially given the time constraint (and to ublock other work around converters - cc @steveharter). Appreciate the input. |
Hello @ahsonkhan! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
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.
Waiting on tests to show that WriteBoolean(propertyName, value) and friends are not valid (when validation is not suppressed) after WritePropertyName
Up-stack consumers are waiting on this change and this PR is blocking some of the converter work. I would like to get this in and will address adding more tests for those cases separately. I do have very similar tests for other "WriteX(propertyName, value)", but not for all TokenTypes, so I am fairly confident this works as expected. I would like to avoid resetting CI at this point. |
…refx#38864) * Add WritePropertyName standlone API on the Utf8JsonWriter. * Fix write indented, add more tests, and more debug.asserts. * Remove _isProperty field and rely on _tokenType == JsonTokenType.PropertyName * Auto-gen the ref. * Address PR feedback. Commit migrated from dotnet/corefx@a5b35f5
Fixes https://github.com/dotnet/corefx/issues/36639#issuecomment-500748970
cc @steveharter, @bartonjs, @JeremyKuhne, @rynowak