Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add string-based WriteAsProperty overload to JsonElement.#37789

Merged
ahsonkhan merged 2 commits intodotnet:masterfrom
ahsonkhan:WriteAsProperty
May 20, 2019
Merged

Add string-based WriteAsProperty overload to JsonElement.#37789
ahsonkhan merged 2 commits intodotnet:masterfrom
ahsonkhan:WriteAsProperty

Conversation

@ahsonkhan
Copy link
Copy Markdown

@ahsonkhan ahsonkhan commented May 19, 2019

This API is useful internally within JsonSerializer as well. See #37690 (comment)

/// <summary>
/// Write the element into the provided writer as a named object property.
/// </summary>
/// <param name="propertyName">The name for this value within the JSON object, as UTF-16 text.</param>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a string, the encoding shouldn't be mentioned. So ditch everything after the comma.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or, thanks to the way the diff lined up, copy the one for ROS<char>, which similarly doesn't need to mention the encoding, since it's not bytes)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the format in the writer APIs, where ROS<byte> states UTF-8 encoding, ROS<char> states UTF-16 encoding, and the string overload doesn't specify at all.

For the string case, I don't specify the encodings, but kept it for the ROS<char> one.

/// Writes the property name and string text value (as a JSON string) as part of a name/value pair of a JSON object.

/// Writes the UTF-16 property name and UTF-16 text value (as a JSON string) as part of a name/value pair of a JSON object.

/// Writes the property name and UTF-8 text value (as a JSON string) as part of a name/value pair of a JSON object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"UTF-16" really only applies to bytes. With char it's relevant where surrogates are concerned, but in that case char means UTF-16... so mentioning the encoding is unnecessary to the point that it's probably confusing.

I strongly advocate not having it for the ROS<char>, which is why it does not currently have it.

Copy link
Copy Markdown
Author

@ahsonkhan ahsonkhan May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will fix it here for consistency. I filed an issue for the writer APIs, though I am not fully convinced that it is necessary. Presumably, this applies to summary and param docs as well, correct (for both ROS<char> and string overloads)?

https://github.com/dotnet/corefx/issues/37794

/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public void WriteAsProperty(string propertyName, Utf8JsonWriter writer)
=> WriteAsProperty(propertyName.AsSpan(), writer);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent to let null succeed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. null == "" (consistent with how the writer works today).

We need to agree on the semantics as part of https://github.com/dotnet/corefx/issues/34632, but I wanted to keep it as is until then.


string propertyName = (string)entry.Key;
element.WriteAsProperty(propertyName.AsSpan(), writer);
element.WriteAsProperty(propertyName, writer);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveharter, updated this from #37690 (comment)

@ahsonkhan ahsonkhan merged commit 1897667 into dotnet:master May 20, 2019
@ahsonkhan ahsonkhan deleted the WriteAsProperty branch May 20, 2019 09:57
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…efx#37789)

* Add string-based WriteAsProperty overload to JsonElement.

* Address PR feedback - remove UTF-16 from the comments.


Commit migrated from dotnet/corefx@1897667
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants