diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index 93f7143a55844b..17e189cf9ff689 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -874,6 +874,15 @@ private static void ClearAndReturn(ArraySegment rented) } } + internal void WritePropertyName(int index, Utf8JsonWriter writer) + { + CheckNotDisposed(); + + DbRow row = _parsedData.Get(index - DbRow.Size); + Debug.Assert(row.TokenType == JsonTokenType.PropertyName); + WritePropertyName(row, writer); + } + private void WritePropertyName(in DbRow row, Utf8JsonWriter writer) { ArraySegment rented = default; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs index 77732b69548d6e..c5d687892335aa 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs @@ -1316,6 +1316,13 @@ public void WriteTo(Utf8JsonWriter writer) _parent.WriteElementTo(_idx, writer); } + internal void WritePropertyNameTo(Utf8JsonWriter writer) + { + CheckValidInstance(); + + _parent.WritePropertyName(_idx, writer); + } + /// /// Get an enumerator to enumerate the values in the JSON array represented by this JsonElement. /// diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs index 61a66b689ebcbf..0d4ad75cbcadf7 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs @@ -117,7 +117,15 @@ public void WriteTo(Utf8JsonWriter writer) ThrowHelper.ThrowArgumentNullException(nameof(writer)); } - writer.WritePropertyName(Name); + if (_name is null) + { + Value.WritePropertyNameTo(writer); + } + else + { + writer.WritePropertyName(_name); + } + Value.WriteTo(writer); }